I am trying to create a simple sprint function, the code I currently have is
//Sprint function
if (Input.GetKey ("LeftShift")) {
speed = 8;
} else
speed = 4;
Mlnodevelop doesn’t detect any errors with this code but when I try to use it I cannot move my player object at all, however if I replace (“LeftShift”) with a random key such as (“u”) for example the code works fine. Any suggestions? Thanks for any help.
Like Dibbie said in the comment it’s in general a bad idea to use a string key name as it’s easy to write it wrong. See this documentation page to see how the string names are written (the list is at the very bottom).
It’s better to use the KeyCode enum as you can’t misspell the name since the compiler will complain.
edit
Just in case Unity changes, renames or remove that documentation page (which happens a bit too often) here’s the key list copied 1-to-1:
- Normal keys: “a”, “b”, “c” …
- Number keys: “1”, “2”, “3”, …
- Arrow keys: “up”, “down”, “left”, “right”
- Keypad keys: “1”, “2”, “[3]”, “[+]”, “[equals]”
- Modifier keys: “right shift”, “left shift”, “right ctrl”, “left ctrl”, “right alt”, “left alt”, “right cmd”, “left cmd”
- Mouse Buttons: “mouse 0”, “mouse 1”, “mouse 2”, …
- Joystick Buttons (from any joystick): “joystick button 0”, “joystick button 1”, “joystick button 2”, …
- Joystick Buttons (from a specific joystick): “joystick 1 button 0”, “joystick 1 button 1”, “joystick 2 button 0”, …
- Special keys: “backspace”, “tab”, “return”, “escape”, “space”, “delete”, “enter”, “insert”, “home”, “end”, “page up”, “page down”
- Function keys: “f1”, “f2”, “f3”, …
But again, it’s way easier to use the KeyCode enum. It will get autocompleted in VisualStudio and MonoDevelop. It’s also a bit faster since an enum value is just a number. Strings always need to be compared character by character.