public float speed;
//lets try the jump ability agian
private bool onGround;
private float minJump;
private float maxJump;
//this if for the cool down
public float coolDown = 5f;
public float coolDownTimer;
public bool JumpCoolDown = false;
public Text countText;
public Text winText;
private Rigidbody rb;
private int count;
void Start()
{
//when i start the game i can jump stright away
onGround = true;
minJump = 2f;
maxJump = 8f;
rb = GetComponent<Rigidbody>();
count = 0;
SetCountText ();
winText.text = "";
transform.position = new Vector3(0, 3, 0);
}
void FixedUpdate ()
{
//cool down
if (coolDownTimer > 0)
{
coolDownTimer -= Time.deltaTime;
}
if (coolDownTimer < 0)
{
coolDownTimer = 0;
}
if (Input.GetButton("Jump") && coolDownTimer == 0)
{
Debug.Log("Hi!");
coolDownTimer = coolDown;
}
if(onGround)
{
if(Input.GetButton("Jump"))
{
if(minJump < maxJump)
{
minJump += Time.deltaTime * 5f;
}
else
{
minJump = maxJump;
}
print(minJump);
}
else
{
if (minJump > 0f)
{
minJump = minJump + maxJump;
rb.velocity = new Vector3(0f, maxJump, 0f);
minJump = 0f;
onGround = false;
}
}
}
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.AddForce(movement * speed);
}
void OnCollisionEnter(Collision other)
{
//this is when i touch the ground i am able to jump but i will add the 5 second delay lyaer
if (other.gameObject.CompareTag("ground"))
{
onGround = true;
}
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("Pick Up"))
{
other.gameObject.SetActive(false);
count = count + 1;
SetCountText ();
}
}
void SetCountText ()
{
countText.text = "Count " + count.ToString ();
if (count >= 12)
{
winText.text = "You Win!";
}
}
}
Hello,
i am here to ask for your help please, as i dont know how to create a cool down timer or should i say i dont know how to link the jump and the cool down timer together.
if anyone could be able to guide me in the right direction that would be amazing.
Cool down timers are simple in concept - if anything you’re overthinking it Basically, you have two variables - one of them describes how much time should pass before something can happen, and the other describes how much time has passed since the last time that thing happened.
Every call to Update, add your Time.deltaTime to the second variable.
Every time the thing you want to happen is triggered (like when the player presses a jump button) do a simple check - is the time that has passed currently greater than the time that should pass before this thing can happen? If no, don’t do anything. If yes, reset the time that has passed back to zero, and perform whatever action you want to happen.
Here’s some pseudo code that probably explains the concept better: