3

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

Brent Worden
  • 10,624
  • 7
  • 52
  • 57
imperium2335
  • 23,402
  • 38
  • 111
  • 190

1 Answers1

4

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);
Jinjinov
  • 2,554
  • 4
  • 26
  • 45
  • 1
    @MickyDuncan As I see, JinJi has offered a customizeable component that moves its GameObject by an elliptic path. What else should they add? – Nick Volynkin Jun 14 '15 at 22:27
  • I have tried the solution at http://answers.unity3d.com/questions/133373/moving-object-in-a-ellipse-motion.html but it only changes the orbit inclination, changing x and y change the center of rotation. Could this be to do with the fact I want it orbiting it's parent object? If so how do I get it to work properly? – imperium2335 Jun 15 '15 at 06:02
  • @imperium2335 i edited the answer to your new question – Jinjinov Jun 15 '15 at 07:57
  • @JinJi Thanks, I will give it a go once I'm home and accept your answer accordingly. – imperium2335 Jun 15 '15 at 08:32