2D Parabolic character movement between platforms

Hello! 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 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.

Here’s something I did in like 5 minutes: I got a bit confused about what you did, so I just did something different.

if (facingRight)
                rb.velocity = new Vector2(Mathf.Abs(transform.position.x - destination.transform.position.x), jumpForce * GetForceDivide());
            else
                rb.velocity = new Vector2(-(Mathf.Abs(transform.position.x - destination.transform.position.x)), jumpForce * GetForceDivide());
  private float GetForceDivide()
    {
        float aiY = transform.position.y;
        float destinationY = player.transform.position.y;
        float test = Mathf.Abs(aiY - destinationY);
        return aiY > destinationY ? 1f / test : 1f;
    }

The parabolic movement is alreay working without the GetForceDivide(). I only added this if you wanted to change the jump force if the destination is lower than the AI.

Well this seems to achieve exactly what I want to achieve… I swear I’ve seen so many different posts on the internet using complex maths that make use of angles and physics formulas (which is what my previous function did too) and you just did it with a function that could be reduced to a one line… I am baffled as much as I’m amazed. Thank you!

1 Like