# How to implement jump in NavMesh?

I want my NavMeshAgent to jump when Space key is hit.

The Script (in Unityscript) I tried is :

var isJump:boolean=false;

function Update () {

if(Input.GetKeyDown(KeyCode.Space)){

GetComponent.<UnityEngine.AI.NavMeshAgent>().enabled=false;
GetComponent.<Rigidbody>().isKinematic=false;
isJump=true;

}
}

function OnCollisionEnter(col:Collision){

if(col.gameObject.layer==4 && isJump){

GetComponent.<UnityEngine.AI.NavMeshAgent>().enabled=true;
GetComponent.<Rigidbody>().isKinematic=true;
isJump=false;
}

}


The ground where the NavMeshAgent stands is in layer 4. But the Agent is not jumping in the first place. But when I don't used the OnCollisionEnter function it jumps. But I need some function to return it to the initial state.

I tried a print("something") inside if statement of OnCollisionEnter function and it prints it as soon as I hit Space. That if statement should have executed when the object jumped and collides with the ground.

How to make it work?

## Why it is not working

As you said, the function is called as soon as you hit Space. That's why it doesn't work because isKinematic is then instantly reset to true before your character can even start his jump. You must have another object other than the ground that is in layer 4 and is colliding with the player.

## How to make it work

Instead of using the layer, I would give the Ground object (and any other similar object) a Tag, like "Ground" for instance. And use :

function OnCollisionEnter(col:Collision){

if(col.gameObject.tag == "Ground" && isJump){

GetComponent.<UnityEngine.AI.NavMeshAgent>().enabled=true;
GetComponent.<Rigidbody>().isKinematic=true;
isJump=false;
}
}

• thanks......yes I need the NavMesh for sure....I don't want my character to be control by physics when moving around the world ....so kinematic is also necessary i guess.... – coder_86 Jul 31 '17 at 10:03
• and the isKinematic=true should execute when object hits the ground....why its executing even before the jump.. – coder_86 Jul 31 '17 at 10:06
• Okay I gave a wrong answer I guess. Try out what I just wrote and tell me if it works :) – Shashimee Jul 31 '17 at 10:22
• sorry not working.....and its the same code as mine but you use tag and i used layer... – coder_86 Jul 31 '17 at 10:27
• Yes but I don't know your game, so this change could be meaningful. What other objects are on layer 4 ? – Shashimee Jul 31 '17 at 10:28

Here's the edited script :

 var hold:boolean=true;
var isJump:boolean=false;
function Update () {

if(Input.GetKeyDown(KeyCode.Space)){

gameObject.GetComponent.<UnityEngine.AI.NavMeshAgent>().enabled=false;

GetComponent.<Rigidbody>().isKinematic=false;
if(hold)
Hold();

}
}

function Hold(){
hold=false;
yield WaitForSeconds(0.1);
isJump=true;
hold=true;
}

function OnCollisionEnter(col:Collision){

if(col.gameObject.layer==4 && isJump){

GetComponent.<UnityEngine.AI.NavMeshAgent>().enabled=true;
GetComponent.<Rigidbody>().isKinematic=true;
isJump=false;

}
}