Unity does not register first key press
I'm VERY new to the Unity community and programming and I'm following a course which is awesome, but I've run into some issues when making my first text adventure game.
I'm using the following code to get back and it works fine.
if (Input.GetKeyDown(KeyCode.T))
{ myState = States.hule;}
Where "hule" is the Danish word for Cave.
I use the EXACT same syntax for when I want to "see the sheets"
if (Input.GetKeyDown(KeyCode.L))
{myState = States.lagen_0;}
where "lagen" is sheets in Danish. When I press L to view sheets, i Have to press it twice, every time, or else nothing happens.
I'm using Visual Studio as the programming "studio".
I hope someone can help with this issue.
Answer by Psy_boy2720 · May 31, 2016 at 09:49 AM
Even though its over 130 lines of code? I must mention that I haven't written any comments on the code either.
I've seen that others have done the same, so here goes.
public Text text;
private enum States {hule, spejl, dør_0, lagen_0, hule_spejl, dør_1, lagen_1, frihed};
private States myState;
// Use this for initialization
void Start(){
myState = States.hule;
}
// Update is called once per frame
void Update()
{
print(myState);
if (myState == States.hule) {state_cell();}
else if (myState == States.lagen_0) {state_lagen_0();}
else if (myState == States.lagen_1) {state_lagen_1();}
else if (myState == States.spejl) {state_spejl();}
else if (myState == States.hule_spejl) {state_hule_spejl();}
else if (myState == States.dør_0) {state_dør_0();}
else if (myState == States.dør_1) {state_dør_1();}
else if (myState == States.frihed) {state_frihed(); }
}
void state_cell ()
{
text.text = "Du er fanget i et fængsel og du vil gerne flygte. " +
"Der ligger et snavset lagen på hvad der ligner " +
"en seng, et spejl hænger på væggen og en dør der er låst " +
"udefra.\n\n" +
"Tryk L for at Se Lagenet, S for at se Spejlet og D for at se Døren ";
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L)) {myState = States.lagen_0;}
else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S)) {myState = States.spejl;}
else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.D)) {myState = States.dør_0;}
}
void state_lagen_0()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L))
{
myState = States.lagen_0;
text.text = "Du fatter ikke at nogen kan sove her, nogen må da skifte lagenerne. " +
"Det er vel bare fængsels livets 'glæder' ! \n \n " +
"Tryk T for at komme Tilbage";
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T)) {myState = States.hule;}
}
void state_spejl()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.S))
{
myState = States.spejl;
text.text = "Du ser et snavset spejl. Det sidder lidt løst. \n \n" +
"Tryk G for at tage spejlet. " +
"Tryk T for at komme Tilbage";
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T)) {myState = States.hule;}
else if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.G)) {myState = States.hule_spejl;}
}
All Inputs should be checked only in Update function. yes according to your code , when first time L is pressed state_lagen_0() but Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L) is false inside state_lagen_0() because Input.Get$$anonymous$$eyDown is true for only one frame or one update(). change Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L) to Input.Get$$anonymous$$ey($$anonymous$$eyCode.L) it will work because Input.Get$$anonymous$$ey will be true for all the frames the key is held down.
void state_lagen_0()
{
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.L))
{
myState = States.lagen_0;
text.text = "Du fatter ikke at nogen kan sove her, nogen må da skifte lagenerne. " +
"Det er vel bare fængsels livets 'glæder' ! \n \n " +
"Tryk T for at komme Tilbage";
}
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.T)) {myState = States.hule;}
}
but keep in $$anonymous$$d inputs should always be checked in update function only.
Omg it worked! :)
So, just to be sure that I know what you mean.
The first time a new key press is "mentioned" it should be input.Get$$anonymous$$eydown($$anonymous$$eycode.[]) correct? and after the first statement, it should be input.Getkey(keycode.[]) ?
Am I understanding this correctly?
yes. It is a temporary fix.
Hereafter when you code call input.Get$$anonymous$$eydown or input.Getkey only in Update ()
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L))
{
//do something
}
if (Input.Get$$anonymous$$ey($$anonymous$$eyCode.L))
{
//do something
}
}
but calling in other functions is not good practice.
void state_lagen_0()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.L))
{
//do something
}
}
above code is not recommended
Your answer
Follow this Question
Related Questions
"event.current.keyCode" not work! 0 Answers
How to change Button input to Keyboard input? 1 Answer
Joystick buttons don't work 0 Answers
Droping a box by pressing space or any key 1 Answer
Can't return KeyCode in build 0 Answers