Nav Mesh Agent don't let me jump

I’m working on a FPS, I’ve got a cube enemy. It has a Nav Mesh Agent component attached to. When active it don’t let me use rigidbody.AddForce to make it jump. Here are the scripts:

To follow the player:

    var target : Transform;
    
    function Update ()
    {
    	GetComponent(NavMeshAgent).destination = target.position;
    }

And the second one, to make it jump when reach certain distance to player:

var target : Transform;//The player
var rayHit : RaycastHit;
var jumpDist : float;//distance the enemy will jump from

function Update () {

	if(Physics.Linecast(transform.position,target.position,rayHit))

        {
            if(rayHit.distance < jumpDist)
            {
			print('Jump over player');                 
//    		animation.Play("jump");
			rigidbody.AddForce(0, 20,0);
 		   }
        }
}

When I tested it, the console prints “Jump over player” and show no errors. But the enemy do not jump.

Why it’s not working and how can I fix this?

You got to stop or disable the navagent before you apply the force, otherwise it will stick to the navmesh.

Also, in your first snippet where you set the destination, it’s faster (now it needs to look up and find the component every time you update), it also makes the code more readable if you just cache the component as you only need to use the getcomponent once in the Awake method. It’s as simple as (in C# but more or less same in JS I suppose)

NavMeshAgent navAgent;

void Awake() {
    navAgent = GetComponent<NavMeshAgent>();
}

Now your navAgent variable holds a reference to the component so you can just call
navAgent.SetDestination() or whatever navmeshagent function you need.

So to fix the Jumping issue, just call navAgent.Stop() before you apply the force to the rigid body, calling navAgent.Stop(true) will stop the agent dead on (it uses the deaccelerate value otherwise) then call navAgent.Resume() whenever you land or hit something to start it up again.

Edit make sure that the rigid body is not kinematic and using gravity, you also need to add a force modifier so all the force is used at once. The below code works.

navAgent.Stop(true);
rigidbody.isKinematic = false;
rigidbody.useGravity = true;
rigidbody.AddForce(new Vector3(0,5,0),ForceMode.Impulse);

Edit 2 also use

rigidbody.AddRelativeForce(new Vector3(0,5,5),ForceMode.Impulse);

That way the force is always applied to the objects local forward.

Update - I’d suggest setting the RigidBody.detectCollisions to false when the AI is not jumping, as it seems capable of interfering with the NavMeshAgent.