3
\$\begingroup\$

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?

\$\endgroup\$
2
\$\begingroup\$

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;
  }
}
\$\endgroup\$
  • \$\begingroup\$ 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.... \$\endgroup\$ – DudeMan Jul 31 '17 at 10:03
  • \$\begingroup\$ and the isKinematic=true should execute when object hits the ground....why its executing even before the jump.. \$\endgroup\$ – DudeMan Jul 31 '17 at 10:06
  • \$\begingroup\$ Okay I gave a wrong answer I guess. Try out what I just wrote and tell me if it works :) \$\endgroup\$ – Shashimee Jul 31 '17 at 10:22
  • \$\begingroup\$ sorry not working.....and its the same code as mine but you use tag and i used layer... \$\endgroup\$ – DudeMan Jul 31 '17 at 10:27
  • \$\begingroup\$ Yes but I don't know your game, so this change could be meaningful. What other objects are on layer 4 ? \$\endgroup\$ – Shashimee Jul 31 '17 at 10:28
0
\$\begingroup\$

Just hold for sometime before changing isJump to true.

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

    }
   }
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.