I am trying to write an enemy AI that jumps between available platforms when it doesn't have ground below its feet. I want the character to do the jumps in a parabolic fashion rather than doing a straight jump between them as shown in the image below:
And to do the arc motion, I use this function and set the actor's velocity to the value returned:
public static Vector2 GetVelocityForArc(Vector3 arcStartPos, Vector3 arcTargetPos, float objectGravityScale, float arrivalTimeVal) {
float velocityX = (arcTargetPos.x - arcStartPos.x) / arrivalTimeVal;
float velocityY = (float)((arcTargetPos.y - arcStartPos.y - 0.5 * (Physics.gravity.y *
objectGravityScale) * Mathf.Pow(arrivalTimeVal, 2)) / arrivalTimeVal);
return new Vector2(velocityX, Mathf.Abs(velocityY));
}
However my function doesn't seem to perform properly. If the target is higher than start position the target just does a straight jump to the target rather than a parabolic one, and sometimes it gets too much X velocity. How can I achieve the parabolic jumps that I'm trying to achieve? Thanks in advance.
arrivalTimeVal
? We have lots of past Q&A showing how to compute a velocity to follow a parabolic arc that you may be able to apply. What makes the x velocity "too much" and how should we judge what amount is correct? What behaviour would you like to see when the target is higher (there are many parabolas we could choose - what characterizes the one you want)? \$\endgroup\$