imperium2335
imperium2335

Reputation: 24122

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

Upvotes: 3

Views: 8255

Answers (1)

Jinjinov
Jinjinov

Reputation: 2683

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

Upvotes: 4

Related Questions