2
\$\begingroup\$

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.

\$\endgroup\$
4
\$\begingroup\$

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: enter image description here

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)
{
    return Mathf.Cos(Mathf.Deg2Rad * value);
}

float MSin(float value)
{
    return Mathf.Sin(Mathf.Deg2Rad * value);
}

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

\$\endgroup\$
  • \$\begingroup\$ Thank you so much, my friend. It works perfectly. But how to do this in vertical or diagonal order? Not just in horizontal order. \$\endgroup\$ – Albertkaruna Aug 13 '16 at 7:53
  • \$\begingroup\$ 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 \$\endgroup\$ – Sourav Paul Aug 13 '16 at 7:53
  • \$\begingroup\$ The equations are same for vertical path. You just change the value of semi-major, semi-minor. For diagonals this will help. \$\endgroup\$ – Sourav Paul Aug 13 '16 at 8:15
  • \$\begingroup\$ Vertical axis works great. I'm trying to get the diagonals. Thanks for the answer and reference. \$\endgroup\$ – Albertkaruna Aug 13 '16 at 8:26
  • \$\begingroup\$ 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. \$\endgroup\$ – Albertkaruna Aug 13 '16 at 11:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.