Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unity 5 moving planets in a circular or elliptical path (orbits)

I have:

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)?

like image 917
imperium2335 Avatar asked Nov 28 '25 05:11

imperium2335


1 Answers

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

try to use:

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

the code:

 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);
 }

EDIT:

if you want it to orbit another object use:

     this.gameObject.transform.position = anotherObject.transform.position + Vector3(X,0,Y);
like image 121
Jinjinov Avatar answered Nov 29 '25 19:11

Jinjinov



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!