Hi Kyle,
Despite all of the awesomeness Unity includes I am often amazed that something which appears to be seemingly required doesn’t have better/easy support.
Your observations regarding Input.GetKeyDown
are correct, a boolean value will be returned depending on the KeyCode or String that you pass as a parameter to be checked. As such, trying to compare true
to S
is never going to do what you want it to do.
Your next approach is a good one, however, Input.inputString
can, in fact, return more than just the one character!
Try something along the lines of the following, perhaps create yourself a new, empty scene (so that I don’t break your cool stuff)…
/// <summary>
/// Update is called once per frame
/// </summary>
private void Update()
{
CheckForPlayerInput();
}
/// <summary>
/// Checks for keyboard interaction
/// </summary>
private void CheckForPlayerInput()
{
if (Input.anyKeyDown)
{
string input = Input.inputString;
if (input.Length > 0)
{
input = input.Substring(0, 1);
}
OptionSelected(input);
}
}
In the above, the CheckForPlayerInput()
method is being called from within the Update()
method.
The CheckForPlayerInput()
method first checks to see if Input.anyKeyDown
is true, e.g. has anything been pressed. If not, we just pass over the following code. If a key has been pressed then we grab the Input.inputString
and pop it into a variable.
Next, we check that the length of the variable is at least 1 character long and then use the Substring()
method to take the first character. The 0,1
refer to the start and end character positions to return the substring from, in this case, 0 and 1 - thus, the first character.
Note: The above does assume that, in the case of a player hitting more than one key in the same frame, the first key hit was the intended one. You could, of course, opt to seek clarity from the player, e.g. if the length of the Input.inputString
was greater than 1 then prompt the user to be a bit more careful with their sausage fingers
Finally, we pass the single character to the OptionSelected()
method. So, let’s have a look at that…
Note: You’ll only get ASCII characters returned in the inputString, it may also contain two special characters, backspace and return/enter (see the link to the documentation below for details).
/// <summary>
/// Handles option selection
/// </summary>
/// <param name="key">The key which was pressed</param>
private void OptionSelected(string key)
{
key = key.ToUpper();
switch(key)
{
case "R":
Debug.Log("Rob was 'ere!");
break;
case "K":
Debug.Log("Kyle was 'ere!");
break;
case "C":
Debug.Log("Coding Coolness!");
break;
default:
Debug.Log("Stop pressing keys I don't care about!");
break;
}
}
The first thing we do in the above is upper-case the character that has been passed to this method. It doesn’t really matter if you upper-case it, or lower-case it, but what we want is some form of consistency that we can rely on (helps cover scenarios where a user may have CAPS LOCK enabled for example).
Then, we just use a switch
statement to act upon the specific character.
Hope the above is of use to you.
See also;