Absolute beginner, else if
Hey Guys I am an absolute beginner just started unity, and I learned about if and else if, check this code
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
Debug.Log("UpArrow is Pressed");
}
else if (Input.GetKeyDown(KeyCode.DownArrow))
{
Debug.Log("DownArrow is Pressed.");
}
else if (Input.GetKeyDown(KeyCode.Return))
{
Debug.Log("Enter is Pressed.");
}
}
}
So if I understand correctly, when I press the UpArrow and DownArrow at the same time, then just one of the messages should appear, but thats not the case when I tested it, if I press both of them (or even all of them, doesn't matter) then both messages appears in the console, what am i doing wrong? is it about Update()? idk Thanks.
Answer by JavaMcGee · Jun 10, 2020 at 06:23 AM
Input.GetKeyDown is only true for one frame in which is the key is pressed, and Update is called every frame. Your code is logging both keys because you're pressing them a split second apart, in different frames. Use Input.GetKey instead to see something more like what you expect (but will print output repeatedly while key is pressed).
Hey, thanks for the answer, I saw a video where the guy in there uses Input.GetKeyDown, and it works just fine,I am pretty sure he was using an older version of unity,have they changed something in Update in the current versions, let me know.
GetKeyDown works just fine, but you misunderstood the IF ELSE construct because of how GetKeyDown behaves.
Because it is virtually impossible to press two keys down on exactly 1 frame, that is why Java$$anonymous$$cGee's explanation is correct. You're getting one key logged on one frame and the other on the next frame.
GetKeyDown only fires (is true) for exactly 1 frame and no more, even if you continue to hold the key down.
On the other hand, GetKey remains true as long as a key is held down.
Using GetKey ins$$anonymous$$d of GetKeyDown, If you press two keys at almost same time (again, next to impossible that that you will hit both keys on the same frame) You will see one key logged, and from there after whichever key is above the others in your IF-ELSE chain will be logged and no other key will be logged.
So for your example, if you press UP Arrow and keep it held down, and then press Down Arrow and keep it held down, you will only see "UpArrow is Pressed", because the UP Arrow test comes before the Down Arrow test in your IF ELSE construct.
And, if you press Down Arrow and keep it held down, and then on the next frame press UP Arrow and keep it held down, you will see "DonwArrow is Pressed" for one frame, and from there after you will see "UpArrow is Pressed", again because the UP Arrow test comes before the Down Arrow test in your IF ELSE construct.
It will prove that your IF ELSE behaves like you think it should.
Your answer
Follow this Question
Related Questions
standard Assest 1 Answer
How to substitute one object with another in animation clip 0 Answers
Why is my Unity different than the one in the Beginner Tutorial? 1 Answer
2D trigger and mouse button help 0 Answers
My raycasting isn't working 1 Answer