arc movement?

Hello all!

And I am asking for a hint

How can I program in C # moving an object along an arc in this way:

Conditions:

• the movement will always be held down to the right or left
• the road is always 1 unit, e.g. Start: 0.0 End -1, -1; Start: 1.1 End of 0,0 etc.

Regards
Marcin

I think this might work:

In your diagram, you are rotating around the lower left corner.

Or look into using splines. There are several spline tools on the asset store.

I will check this solution

In this way maybe I will learn something new

Sure, but just so you know, a spline is a fairly standard game industry tool and has been for a long long time. Iâ€™m just not sure why there isnâ€™t one native to Unity.

I believe youâ€™re looking for: Arcing Projectiles in Unity.

This is the part that calculates the arc:

``````// Compute the next position, with arc added in
float x0 = startPos.x;
float x1 = targetPos.x;
float dist = x1 - x0;
float nextX = Mathf.MoveTowards(transform.position.x, x1, speed * Time.deltaTime);
float baseY = Mathf.Lerp(startPos.y, targetPos.y, (nextX - x0) / dist);
float arc = arcHeight * (nextX - x0) * (nextX - x1) / (-0.25f * dist * dist);
nextPos = new Vector3(nextX, baseY + arc, transform.position.z);

// Rotate to face the next position, and then move there
transform.rotation = LookAt2D(nextPos - transform.position);
transform.position = nextPos;

// Do something when we reach the target
if (nextPos == targetPos) Arrived();
``````

EDIT: look at 2d:

``````/// Quaternion that rotates +X to align with forward
static Quaternion LookAt2D(Vector2 forward) {
return Quaternion.Euler(0, 0, Mathf.Atan2(forward.y, forward.x) * Mathf.Rad2Deg);
}
``````