Reputation: 27
I'm using these script and the jump seems fine but the problem is I can do infinite Jump, how to limit the jump?
Like... I only want to jump once and jump again after hit the ground, not jump in the air, nor double jump...
Public void jumping()
{
if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Character - Idle - Nina"))
{
GetComponent<Animator>().SetTrigger("Jump");
}
if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Character - Run - Nina"))
{
GetComponent<Animator>().SetTrigger("Jump");
}
GetComponent<Rigidbody2D>().velocity = new Vector2(0, jump);
}
Upvotes: 0
Views: 735
Reputation: 735
You should define a boolean variable like canJump. when you collide with ground it should be true. When you press space for jump, if your canJump variable true, you can start jump anim then set your canJump variable false
bool canJump;
float force=10f;//it is an example try for better
Public void jumping()
{
if(canJump){
if (GetComponent<Animator>().GetCurrentAnimatorStateInfo(0).IsName("Character - Idle - Nina"))
{
GetComponent<Animator>().SetTrigger("Jump");
}
if (GetComponent<Animator>
().GetCurrentAnimatorStateInfo(0).IsName("Character - Run - Nina"))
{
GetComponent<Animator>().SetTrigger("Jump");
}
GetComponent<Rigidbody2D>().AddForce(Vector3.up*force);
canJump=false;
}
}
void OnCollisionEnter(Collision collision){
if(collision.gameObject.compareTag("ground")){
canJump=true;
}
}
Upvotes: 1