Start and use Timer outside Update
I'm making a mobile 2D scroller that allows the player to go back, to have a clear screen on the direction the player is facing the Touch Controller changes position and locates behind the Player.
I'm trying to make the controller wait three seconds to change position in case the player goes back just a little bit and moves forward again, every time the player flips, it should count three seconds before checking if it should change the controller position.
I need to start from three to zero seconds every time the player flips and I thought of using a Coroutine to do this, however, the problem is that every time the player switches direction the coroutine is called, for instance, if the player changes twice in a second, the coroutine to count is called twice.
I tought of using a timer and set it to start (3 secs) every time the player flips, but this means that I need to call the StartTimer function from the Flip method, this is outside of the Update function.
My question is How can I start a Timer outside of the Update Function and still be able to count three seconds with Time.deltaTime and then call the ChangeController Position method?
Thank you very much for any information you can provide on this.
Ok
I found a way to do it here it is in case someone else is facing something similar:
I did set the timer (a float with value 3) in the Update function and substract Time.deltaTime, if the Timer is equals or less that zero a boolean is set to allow ChangeControl Position.
In the Flip method the Timer value is set to 3 so every time the Flip function is called, the Timer sets to 3 and inmediately starts counting down.
I don't know if it is the most efficient way (as the timer will still count down to $$anonymous$$us infinite if the function Flip is never called) but it is the way it worked as intented.
Thank you!
Your answer
Follow this Question
Related Questions
Why does Bool code never work in my C# Script? 1 Answer
how do i remember a just instantiated prefab as a variable gameobject? 1 Answer
Return key triggering a button OnClick? 1 Answer
Objects are created in the same location. 0 Answers
RPC Instantiate issue, Prefabs incompatible with MAC ? 0 Answers