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;
}
}