- Home /
How to make a timer? 2D GAME
I would love to know how to make a timer, i wanna make a cooldown between jumps, i'm making a 2D game, and i am making the player controller right now, but if you spam space you can fly! So i wanted to add a timer that is 5 and if the var "Time" >= 5 then you can press space to jump and every 1 seconds substract 1 from "Time" else don't do anything. I don't know if that's possible... I'm a beginner even thoe i'm familiar of how everything works.
Here's the code :
 using UnityEngine;
 using System.Collections;
 
 public class PlayerController : MonoBehaviour {
 
     public float speed;
     public float jump;
     float moveVelocity;
     public float jumpDelay = 1;
     public float timeBetweenShots = 0.3333f;
 
     //Grounded Variables
     bool grounded = true;
     
     void Update () 
     {
 
         if (Time.time >= timestamp && (Input.GetKeyDown (KeyCode.UpArrow))) 
         {
             GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, jump);
 
             timestamp = Time.time + timeBetweenShots;
         }
 
 
 
 
         if (Input.GetKey (KeyCode.LeftArrow)) 
         {
             moveVelocity = -speed;
         }
 
         if (Input.GetKey (KeyCode.RightArrow)) 
         {
             moveVelocity = speed;
         }
 
         GetComponent<Rigidbody2D> ().velocity = new Vector2 (moveVelocity, GetComponent<Rigidbody2D> ().velocity.y);
 
         if (Input.GetKeyUp (KeyCode.RightArrow)) 
         {
             moveVelocity = 0;
         }
         
         if (Input.GetKeyUp (KeyCode.LeftArrow)) 
         {
             moveVelocity = 0;
         }
 
     }
 
     void OnTriggerEnter2D()
     {
         grounded = true;
     }
 
     void OnTriggerExit2D()
     {
         grounded = false;
     }
     
 }
 
Answer by getyour411 · Aug 02, 2015 at 06:32 PM
"I'm a beginner even thoe i'm familiar of how everything works." That's quite a statement! At the least you should know how search works then and 'unity timer example' search string to Google yielded tons of examples such as:
http://answers.unity3d.com/questions/443387/simple-c-question.html
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                