Unity 5在圆形或椭圆形路径(轨道)中移动的行星 [英] Unity 5 moving planets in a circular or elliptical path (orbits)

查看:942
本文介绍了Unity 5在圆形或椭圆形路径(轨道)中移动的行星的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有:

void Update () {
    transform.RotateAround(transform.parent.position, new Vector3(0, 1, 0), orbitSpeed * Time.deltaTime);
}

这给了我一个非常基本的圆形轨道.

Which gives me a very basic circular orbit.

我需要做些什么才能获得各种椭圆形的轨道(行星是每颗恒星随机生成的,所以我也想给它们提供随机的轨道路径)?

What do I need to do to get varied elliptical orbits (planets are generated randomly per star and so I would also like to give them random orbit paths)?

推荐答案

您不能使用RotateAround.您将必须发挥自己的功能

you can't use RotateAround. you will have to make your own function

尝试使用:

http://answers.unity3d. com/questions/133373/moving-object-in-a-ellipse-motion.html

x, y: center of the ellipse
a, b: semimajor and semiminor axes

代码:

 var a : int;
 var b : int;
 var x: int;

 var y : int;
 var alpha : int;
 var X : int;
 var Y : int;

 function Update () {
     alpha += 10;
     X = x + (a * Mathf.Cos(alpha*.005));
     Y= y + (b * Mathf.Sin(alpha*.005));
     this.gameObject.transform.position = Vector3(X,0,Y);
 }

如果您希望它绕另一个物体运行,请使用:

if you want it to orbit another object use:

     this.gameObject.transform.position = anotherObject.transform.position + Vector3(X,0,Y);

这篇关于Unity 5在圆形或椭圆形路径(轨道)中移动的行星的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆