Hello everyone! I’m new to unity, and building a game for a class. I’m having a small issue with Input.inputString. (I saw that there were issues at some point with this on Mac, but I am on Windows 7 and did not see any threads saying this method was bugged on this OS).
What I’m trying to do is record every time the player pressed “w”, “e”, “a”, and “d”, as well as the time they pressed it, or released it. Right now I have a GameObject called KeyRecorder that runs the following code every frame, which (hopefully) adds a new entry into my recording every time a relevant key is pressed or released:
HashSet<char> diffPresses = new HashSet<char>(Input.inputString.ToCharArray());
diffPresses.IntersectWith(keysToRecord);
if (!diffPresses.SetEquals(curKeypresses)) {
StringBuilder sb = new StringBuilder();
foreach (char c in diffPresses) {
sb.Append(c);
}
curKeypresses = diffPresses;
curRecording.Add(new KeyTimePair(sb.ToString(), Time.time - startTime));
}
keysToRecord is a hashset containing the characters ‘w’, ‘a’, ‘d’, ‘e’.
startTime is the time this GameObject was created.
curKeypresses is where I store the current keys being pressed to check against every frame for differences.
KeyTimePair is just a pair of string and float denoting the keys and the time relative to the gameobject spawning.
My issue is that whenever I press a key, I’ll get two entries, one with the key (like “d”, 5.34187), and a second really soon after with an empty string (like “”, 5.356074) which seems to be the frame right after I pressed “d”. This is causing issues with when I try to play back the recordings. I tried to prune out blanks, but that also causes issues since I need to know if no keys are being pressed at a given time. Does anyone know why this is happening, and how I could fix it?
I’m running on Unity 5.6.0f3 Personal 64-bit, if that’s important
Thanks!