What’s the proper syntax for entering a modified key in the input manager?
For example, I can enter “left shift” or “left ctrl” without a problem, but how do I enter “left shift” + x? Every combination option I’ve tried is cleared as soon as I exit the field.
I don’t know of a way to do that with the Input inespector, BUT, if nobody pipes up with a way to do that:
You can check keys that are not in the Input inspector (it’s just that people can’t customize their own controls if you do that). Use GetKey instead of GetButton for that.
and
You can check to see if a key is HELD down. GetKey checks for that, while GetKeyDown only registers the moment the key was first pressed down.
The names of the keys are in the manual under Input. To see if Shift is held down:
if ( Input.GetKey ("left shift") )
You could use that along with the function of the “x” key. For example, if x in input manager is “Fire,” then to check for left shift-x you could use:
if ( Input.GetKey ("left shift") Input.GetKeyDown("Fire") )
And if you don’t care which Shift key, then maybe you would have to use || for “or” (unless “shift” alone works but I haven’t tried it):
Those examples all assume you just want to trigger Fire once, not continuously. If you want x to be held down and keep on firing, then change that GetKeyDown to just GetKey.
Thanks for all your help today, Morgan. You’ve been great.
My character has different idles depending on his “state.” I’ve got a regular, relaxed idle and I’m switching to a battle-ready idle. GetKeyDown is working fine to switch him from relaxed to defensive, but how do I get him back to relaxed? I tried another GetKeyDown and it’s not working.
What I’m trying to do is press one key (G for guard) to get the character into his defensive posture. When the threat is gone, press G again (could be another key if necessary) and go back to relaxed idle.
There’s a combination of factors at play here…it’s not just getting Unity to recognize the keystrokes, it’s also placing the loops in the right layers so that they will override one another.
Right now, I’m getting hit and miss results and I can’t figure out why…
It might be good to draw out a flowchart of some kind to get the function worked out in your mind, and then reproduce that with code. Write out what different states the player might be in, what different inputs the user might give, and what the result should be for each input when given in each state. So in the guarding state, the G key does one thing (stops guarding) but in the normal state, the G key does another thing (starts guarding).
If you want G to do something different depending on whether or not you are already guarding, then you might need a variable like state = “guarding” (or other states) that gets checked before acting.
Then every action wouldn’t just trigger a result, it would check the current state and THEN trigger a result. For instance
Now, if you only HAVE two states, guarding and not-guardingm, then instead of state you could just have a variable guarding which would equal true or false. That’s a “boolean” variable, and that lets you could check it in a simpler way:
if (guarding)
And to reverse the guarding state every time you hit G, you would just use:
guarding = ! not guarding;
…because ! means “not” and you can reverse a boolean variable that way. True becomes false and false becomes true.
If you have multiple states that can be combined (like guarding, firing, shielded, whatever) you can have a boolean variable for each.
Hope that applies somewhat to what you are attempting