# C# Getting AI to jump at an arc

## Recommended Posts

Hi,

Currently I'm trying to get my ai enemy path find to the player. I've got astar working as expected but i want the enemy ai to jump at an arc when it need to jump between platforms. Here how I'm calculating initail velocity using suvat physics equations:

 public LaunchData CalculateLaunchData(Vector3 launchTarget, Vector3 projectilePositions, float height) {
float displacementY = launchTarget.y - projectilePositions.y;
Vector3 displacementXZ = new Vector3(launchTarget.x - projectilePositions.x, 0,
launchTarget.z - projectilePositions.z);
float time = Mathf.Sqrt(-2 * height / gravity) + Mathf.Sqrt(2 * (displacementY - height) / gravity);
Vector3 velocityY = Vector3.up * Mathf.Sqrt(-2 * gravity * height);
Vector3 velocityXZ = displacementXZ / time;
return new LaunchData(velocityXZ + velocityY * -Mathf.Sign(gravity), time);
}

According to the v = u + at formula, I can use time and initail velocity to calculate final. So does final velocity mean velocity at any time t? My idea was todo something like this in my enemy ai class, but it doesnt quite work expected as my enemy is having a weird landing and jumping behaviour and not as smooth as when I just add displacement to original position. Here is what I'm doing:

IEnumerator Jump(LaunchData launchData) {
float timer = 0.0f;
while (timer <= launchData.timeToTarget) {
velocity.x = launchData.initialVelocity.x + (timer);
velocity.y = launchData.initialVelocity.y + (timer * (ps.gravity));
timer += Time.deltaTime;
enemyController.Move(velocity * Time.deltaTime);
yield return null;
}
jumping = false;
}

Where enemyController will handle some collision detection related stuff and call Transform Translate method in unity. Something else is that I can use the equation: s = ut + (1/2)(a)(t^2) to get the displacement directly. This works quite well as I can just add the displacement directly to the jump starting position. However, I can only set the transform.position directly with this way and thus I can't handle collisions as nicely as transforming with a velocity (since my resusable code is inside the controller takes velocity). I'm quite new to unity and game programming, so am I doing something wrong with setting my velocity? I can add a little gif later to better visulise things.

## Create an account

Register a new account

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 11
• 18
• 15
• 9
• 9