- Home /
How to start loops when I press a key and stop when I press again?
I am creating a dashboard system for my car game. I just need when I press a key, the light turns on and turns off after 0.3 seconds and loop until I press the key again.
Answer by Benproductions1 · Oct 19, 2013 at 04:15 AM
Hello,
I would do this:
When the button is pressed you toggle a boolean variable between true and false. If the boolean is toggle on, you reset the "timer".
For the timer, I would use Time.time along with the "timer" and a little bit of math to determine whether the light should be on or off every frame. This timer will only run if boolean variable is on.
Just as a little bit pseudocode if it helps:
toggle = true
timer = 0.0
update()
if button
toggle = !toggle
if toggle
timer = Time.time
if toggle
light.enabled = (Time.time - timer)%0.6 < 0.3
Hope this helps,
Benproductions1
Your answer
Follow this Question
Related Questions
problem with loop and mesh values 0 Answers
my foreach loop and condition wont work 0 Answers
Change transform.position.y in a loop 1 Answer
How can I loop objects with Polygoncollider2D attached to them? 0 Answers
Why is OnGUI not working? 1 Answer