Quarty
Quarty

Reputation: 89

In Unity3d, how to work out speed required to travel distance at specific time, cannonball with Sin()

I'm moving an object in a cannonball pattern without a rigidbody, without addforce. I need to work out how fast the cannonball should travel to reach its destination at the right time.

My code so far:

moveTimer += Time.deltaTime * ballSpeed;
Vector3 currentPos = Vector3.Lerp(startPos, endPos, moveTimer);
currentPos.y += height * Mathf.Sin(Mathf.Clamp01(moveTimer) * Mathf.PI);
this.transform.position = currentPos;

I know that by increasing 'ballSpeed', the cannonball will follow the same curve but faster. Assuming I want to arrive at endPos in precisely 10 seconds, how do I calculate the ballSpeed required?

Thanks in advance!

Upvotes: 0

Views: 62

Answers (1)

Mikko Koivisto
Mikko Koivisto

Reputation: 301

ballSpeed = 1f / 10f; // duration 10s
...
moveTimer += Time.deltaTime * ballSpeed;
Vector3 currentPos = Vector3.Lerp(startPos, endPos, moveTimer);
currentPos.y += height * Mathf.Sin(Mathf.Clamp01(moveTimer) * Mathf.PI);
this.transform.position = currentPos;

Upvotes: 2

Related Questions