- Home /
Help with jump cooldown
Hi, i'm a beginner Unity developer and i'm stuck at jumping cooldown, my player script allows me to jump but if i spam the jumping key the player keep jumping on mid air and starts flying just by spamming that key, i want a cooldown so when i spam the key it makes only one jump and after 3 or 4 seconds i can jump again,please help me, here is my player script, what should i add or remove :
using UnityEngine; using System.Collections;
public class Player : MonoBehaviour {
 private Animator anim;
 private CharacterController controller;
 public float speed = 12.0f;
 public float turnSpeed = 200.0f;
 private Vector3 moveDirection = Vector3.zero;
 public float gravity = 20.0f;
 public float JumpSpeed = 7.0f;
 
 void Start()
 {
     controller = GetComponent<CharacterController>();
     anim = gameObject.GetComponentInChildren<Animator>();
 }
 void Update()
 {
     if (Input.GetKey("up"))
     {
         anim.SetInteger("AnimationPar", 1);
     }
     
     else
     {
         anim.SetInteger("AnimationPar", 0);
     }
     if (Input.GetKey("down"))
     {
         anim.SetInteger("AnimationPar", 2);
     }
     if (controller.isGrounded)
     {
         moveDirection = transform.forward * Input.GetAxis("Vertical") * speed;
     }
             if (Input.GetButtonDown("Jump"))
                
         {
         anim.SetBool("is_in_air", true);
                 moveDirection.y = JumpSpeed;
         
     }
     else
     {
                 
                 anim.SetBool("is_in_air", false);
               
             }
     float turn = Input.GetAxis("Horizontal");
     transform.Rotate(0, turn * turnSpeed * Time.deltaTime, 0);
     controller.Move(moveDirection * Time.deltaTime);
     moveDirection.y -= gravity * Time.deltaTime;
    
 }
 }
Answer by W01Fi3 · Oct 09, 2018 at 06:18 PM
Before you jump, try adding a controller.isGrounded if statement
 if (Input.GetButtonDown("Jump"))
 {
     if(controller.isGrounded == true)
     {
         anim.SetBool("is_in_air", true);
         moveDirection.y = JumpSpeed;
     }
 }
Your answer
 
 
             Follow this Question
Related Questions
Making player jump when left mouse clicked 1 Answer
how to move and jump at the same time,how to move and jump at the same time 0 Answers
Jumping in only the direction of the last key pressed 0 Answers
2D C# Jump Script Addforce 2 Answers
If statement executing even though argument is false 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                