🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Getting AI to jump at an arc

Started by
0 comments, last by sun113344 5 years, 3 months ago

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.

This topic is closed to new replies.

Advertisement