How to setup a Coroutine so that it can press a KeyCode for a certain amount of time?
I have a List of KeyCodes and also a list of Float which stores Time.time - startTime
I wanted so that my character can move based on the stored keys in list and press the buttons for how long it has been pressed when I recorded it. This is my code;
IEnumerator RelayInputs(){
for (int i = 0; i < keyList.Count; i++){
lastKey = keyList[i];
character.Move(lastKey);
yield return new WaitForSeconds (0.5f);
}
}
I haven't added the StreamReader functions to read the keys from the .txt file I may also need a guide on that.
Regards.
Answer by Brocccoli · Mar 21, 2016 at 07:58 PM
A better solution is to map those KeyCodes to certain actions (methods).
Instead of programmatically holding down a key for a certain amount of time, just call the method to perform the respected actions the amount of times appropriate for the time the key was held down.
This is better OO design and allows you to change the definition of the action independent of the key press that causes it.
so it should be pressing the number of times it takes for the seconds that I pressed? That would be something, I'd have a go on that. And do you know how to run it on a coroutine? seems like my code doesnt work and the examples I search for are too advanced
Haven't worked much with coroutines, I suggest taking a look at the documentation.
Your answer
Follow this Question
Related Questions
Listening keypresses out of Update? 1 Answer
Is it possible to use the keyboards keys like toggle buttons? 1 Answer
Smooth 360 degree rotation every keypress - C# 0 Answers
Is there a way to identify the state of the scene obstacles? 0 Answers
Problem with array and coroutine...Sometimes Works!!! 1 Answer