Skip to main content
clearer code
Source Link
public float alpha = 0f;

public float tilt = 45f;

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

float MCos(float value)
{
   (5f *return Mathf.SinCos(Mathf.Deg2Rad * alphavalue) *;
                                          }

float MSin(float value)
{
    return Mathf.CosSin(Mathf.Deg2Rad * tilt))
                                     value);
    alpha += 5f;//use this to control speed
}
public float alpha = 0f;

public float tilt = 45f;

void Update ()
{
    transform.position = new Vector2(0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt)) -
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)),
                                     0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)) +
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt))
                                     );
    alpha += 5f;//use this to control speed
}
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);
}
edited body
Source Link
public float alpha = 0f;

public float tilt = 45f;

void Update ()
{
    transform.position = new Vector3Vector2(0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt)) -
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)),
                                     0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)) +
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt))
                                     );
    alpha += 5f;//use this to control speed
}
public float alpha = 0f;

public float tilt = 45f;

void Update ()
{
    transform.position = new Vector3(0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt)) -
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)),
                                     0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)) +
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt))
                                     );
    alpha += 5f;//use this to control speed
}
public float alpha = 0f;

public float tilt = 45f;

void Update ()
{
    transform.position = new Vector2(0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt)) -
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)),
                                     0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)) +
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt))
                                     );
    alpha += 5f;//use this to control speed
}
generalized solution added
Source Link

Result with a trail renderer: [![enter image description here][1]][1]

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 Vector3(0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt)) -
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)),
                                     0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)) +
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt))
                                     );
    alpha += 5f;//use this to control speed
}

You can control tilt of the eliptical path with the above code. Constants can be changed as you need. [1]: enter image description herehttps://i.sstatic.net/cqmVn.png

Result with a trail renderer: enter image description here

Result with a trail renderer: [![enter image description here][1]][1]

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 Vector3(0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt)) -
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)),
                                     0f + (10f * Mathf.Cos(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Sin(Mathf.Deg2Rad * tilt)) +
                                           (5f * Mathf.Sin(Mathf.Deg2Rad * alpha) *
                                                 Mathf.Cos(Mathf.Deg2Rad * tilt))
                                     );
    alpha += 5f;//use this to control speed
}

You can control tilt of the eliptical path with the above code. Constants can be changed as you need. [1]: https://i.sstatic.net/cqmVn.png

Source Link
Loading