- Home /
How to save Keyboard Input in a variable?
Im trying to clean up my Playercontroller and thought i could do a switch/case with the Input of which or if any Button is pressed at this frame instead of all these if/else in one function.
im trying to implement sth like that:
WhateverType KeyboardInput = null;
switch(getKeyboardInput())
{
case Keycode.W: PlayerMovement(V3Up; break;
case Keycode.S: PlayerMovement(V3Down); break;
...
case null: break;
}
But i just don't know how can i get the Keyboard Input stored in a varibale :(
may you know :)
thx in advice
CKM
You're looking for the $$anonymous$$eyCode enum.
$$anonymous$$eyCode my$$anonymous$$ey = $$anonymous$$eyCode.Space;
Next issue: C# switches require that the case values are constant at compile time.
const $$anonymous$$eyCode my$$anonymous$$ey = $$anonymous$$eyCode.Space; //can never change this value
If that limitation is a problem, you may find it easier to use a dictionary or some other lookup method.
Answer by KlivenPL · Feb 26, 2015 at 03:20 PM
void Update(){
string keyboardInputString = Input.inputString;
switch (keyboardInputString){
case "k":
// your logic here, for example // Application.Quit();
break;
}
}
I did not test this code, but it should work :D
Your answer
Follow this Question
Related Questions
Saving/Loading variables 1 Answer
Save Scores In Scene 4 Answers
Save int value to .txt file 1 Answer
save variable from scene to scene 2 Answers
How to save a gameObjects on a prefab and variables with a script? 1 Answer