Jump to content
  • Advertisement
sun113344

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.

Share this post


Link to post
Share on other sites
Advertisement

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!