# How to orbit an object around another object in an oval path in Unity?

I would like to create kind of planetary orbit structure for my game. I was used transform.RotateAround(Vector3.zero, Vector3.forward, RotationAngle * Time.deltaTime); and it rotates in a circular shape. But I would like to do it in an oval (Ellipse) shape.

You may translate the orbiting object with the coordinates of an elliptical path. You can get the x and y values from the equations.

x = centerX + (semi-major * sin T)

y = centerY + (semi-minor * cos T)

Use some code like below:

float alpha = 0f;

void Update ()
{
//transform.position = new Vector2(center.x + (semiMajor * Mathf.Sin(AngleX)),
//                                 center.y + (semiMinor * Mathf.Cos(AngleY)));

transform.position = new Vector2(0f + (10f * Mathf.Sin(Mathf.Deg2Rad * alpha)),
0f + ( 5f * Mathf.Cos(Mathf.Deg2Rad * alpha)));
alpha += 5f;//can be used as speed
}


Result with a trail renderer:

More generalized solution:

Formula used:

x = centerX + {semi-major * cos(alpha)*cos(tiltAngle) - semi-major * sin(alpha)*sin(tiltAngle)}

y = centerX + {semi-minor * cos(alpha)*sin(tiltAngle) + semi-minor * sin(alpha)*cos(tiltAngle)}

public float alpha = 0f;

public float tilt = 45f;

void Update ()
{
transform.position = new Vector2(0f + (10f * MCos(alpha) * MCos(tilt)) - ( 5f * MSin(alpha) * MSin(tilt)),
0f + (10f * MCos(alpha) * MSin(tilt)) + ( 5f * MSin(alpha) * MCos(tilt)));
alpha += 5f;
}

float MCos(float value)
{
}

float MSin(float value)
{
}


You can control tilt of the eliptical path with the above code. Constants can be changed as you need.

• Thank you so much, my friend. It works perfectly. But how to do this in vertical or diagonal order? Not just in horizontal order. – Albertkaruna Aug 13 '16 at 7:53
• The equations give you two values which is a coordinate on the the elliptical path. Change of alpha gives you the next coordinate in the path with some interval. Then you visit the coordinates as well as the path. Maths behind these – Sourav Paul Aug 13 '16 at 7:53
• The equations are same for vertical path. You just change the value of semi-major, semi-minor. For diagonals this will help. – Sourav Paul Aug 13 '16 at 8:15
• Vertical axis works great. I'm trying to get the diagonals. Thanks for the answer and reference. – Albertkaruna Aug 13 '16 at 8:26
• I have tried the diagonal axis rotation but I can't find the way. If you know how to do it kindly give me the suggestion. – Albertkaruna Aug 13 '16 at 11:30