Title: how can i add cooldown to a GetKey for position and rotation transforms in C#? unity3d
Tags: c#;unity3d
Question: im trying to reset my car position by pressing Q to avoid the vehicle to be stuck rollover, but when i press Q, there is no cooldown and if i keep pressing the car will go up flying. I dont want that, hopefully someone can help me :&gt;
I also wanted to change only the X and Z rotation with the same keycode but im having problems to make it work.
This is the code that i have currently
if (inVehicle == true &amp;&amp; Input.GetKey(KeyCode.Q))
{
Vector3 car = transform.position;
car.y += 1;
transform.position = car;
Quaternion rotation = Quaternion.Euler(0, 0, 0);
}

Here is the accepted answer: Use Coroutines to make a cooldown
public float cooldown = 1;
bool canReset = true;
void Update ()
if (inVehicle == true &amp;&amp; Input.GetKey(KeyCode.Q) &amp;&amp; canReset)
StartCoroutine(Cooldown(cooldown));
...
IEnumerator Cooldown(float seconds)
canReset = false;
yield return new WaitForSeconds(seconds);
canReset = true;

Here is another answer: Common solution is to use Time.time difference.
// ... Class fields ... //
[SerializeField]
protected float cooldown = 0.5f; //Half a second
float lastKeyPressed = 0f;
// ... In Update Function ... //
if ((Time.time - lastKeyPressed) &gt;= cooldown &amp;&amp; inVehicle == true &amp;&amp; Input.GetKey(KeyCode.Q))
Vector3 car = transform.position;
car.y += 1;
transform.position = car;
Quaternion rotation = Quaternion.Euler(0, 0, 0);
lastKeyPressed = Time.time;
You can also use Time.unscaledTime if you don't want it to be affected by current Time Scale.
Other option is use coroutines as convenient way mentioned in other answer, but be careful, coroutines often involves unnecessary GC allocations

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 0, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2021:00:06/11/21', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<c#><unity3d>', 'Title': 'how can i add cooldown to a GetKey for position and rotation transforms in C#? unity3d', 'ViewCount': 108, 'id': 67929908, 'user_AboutMe': None, 'user_AccountId': 21907394.0, 'user_DisplayName': 'fellow_programmer', 'user_Reputation': 1.0, 'user_Views': 4.0, 'user_id': 16192281.0}}