- Home /
How to make player starts moving after holding a key for certain time only once
Hi I am making a 2d game runner like the picture where the first action to only start the game ( after holding down the key player does not have to hold it anymore), player has to hold down a key (lets say G) and after a certain time like 3 seconds the player model starts moving automatically forward on x axis only and up and down on y axis. It is like when you want to start a car engine you have to hold down the switch for like 3 to 5 seconds and then car engines starts to move.
source: https://www.youtube.com/watch?v=5M7vX_z6B9I [1]: /storage/temp/171097-capture.png
Answer by xxmariofer · Nov 20, 2020 at 08:39 AM
void Start()
{
StartCoroutine(InitMovement());
}
IEnumerator InitMovement()
{
bool iddle = true;
float timer = 0;
float maxTimer = 3;
do
{
if (Input.GetKey(KeyCode.G)
{
timer += Time.deltaTime;
if (timer >= maxTimer) iddle = false;
}
else
{
timer = 0;
}
yield return null;
}
while(iddle);
//START MOVING HERE
}
Your answer
Follow this Question
Related Questions
Delay before any action 3 Answers
Weapons Cool Down 1 Answer
Add Delay to Moving Platform 1 Answer
Basic toggle timer Help! 2 Answers