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;
GetComponent.<Rigidbody>().AddForce(0,400,0);
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?