- Home /
How do I move an object along an ellipse path on diagonal?
Hi,
I need to move a game object along an ellipse path on any diagonal: upleft, upright, downleft or downright.
Now I'm getting only with directional movements: up, down, left or right.
For example:
float positionX, positionY, centerX, centerY, semimajorAxes, semiminorAxes, alpha;
void Start() {
positionX = 0f;
positionY = 0f;
centerX = 0f;
centerY = 0f;
alpha = 0f;
//going up
semimajorAxes = 1f;
semiminorAxes = 5f;
//going left
//semimajorAxes = 5f;
//semiminorAxes = 1f;
}
void Update() {
alpha += 1f;
positionX = centerX + (semimajorAxes * Mathf.Cos(Mathf.Deg2Rad * alpha));
positionY = centerY + (semiminorAxes * Mathf.Sin(Mathf.Deg2Rad * alpha));
transform.position = new Vector2(positionX, positionY);
}
Do I need to change the formula? Any suggestion?
Thx!
Answer by Bunny83 · Jun 29, 2019 at 03:36 PM
You have essentially two ways to actually rotate your ellipse:
rotate your position around the center either through a rotation matrix or manually using sin and cos (which essentially would be the same thing)
Compose the final point by using pre-rotated unit coordinate axes.
Usually simpler is the second solution, especially if you use a tranform to define the coordinate space. Though even when you specify the axis yourself for the 2d case it's not that hard. First of all it gets way simpler when you actually use Vector2s for all your given information.
public Vector2 center;
public Vector2 radius;
public float alpha;
public Transform axis;
void Update()
{
alpha += 20f * Time.deltaTime;
float rad = alpha * Mathf.Deg2Rad;
Vector2 p = new Vector2(Mathf.Cos(rad) * radius.x, Mathf.Sin(rad) * radius.y);
transform.position = center + axis.right * p.x + axis.up * p.y;
}
If you want to define the axes manually by a Vector2 you can do this:
public Vector2 axis;
// [ ... ]
// replace the last line in Update with this:
axis = axis.normalized;
Vector2 up = new Vector2(-axis.y, axis.x);
transform.position = center + axis * p.x + up * p.y;
Of course instead of calculating the normal (up vector) of our given axis you could define both axis yourself. However if the two axis are not perpendicular of each other the result would be a skewed ellipse.
Thanks for the answers!
But in the first example, the object moves only from right to left in a straight line, not diagonally
And in the second example, the object does not move
:-(
Are you sure you actuall set both values of the radius in the inspector to non zero values?
Also when you use the second example you also need to define a non zero axis. I normalize the axis in code, so you could set it to (1, 1) which would be 45°
Your answer
Follow this Question
Related Questions
Unity 2D movement and rotate sprite to the direction it is moving in 1 Answer
Player Movement doesn't work, but Debug.Log shows that it should 1 Answer
How do i make a cube move (Continuosly without stopping) when i press a button once in unity 2D 2 Answers
How to make slippery/smooth movement in Unity 2D? 3 Answers
Why wont my attack script get the data from my movement script (error)? 1 Answer