- Home /
Multiple keypresses at once aren't regitered?
So, I have a movement script, working perfectly except one thing : Lets say i go to the right (D button) and then i want to jump (W button), the game doesn't register W. However if i jump and then let go of W it works?
if (Input.GetKey(KeyCode.D))
{
transform.Translate(new Vector3(0, 0, -1) * playerSpeed * Time.deltaTime);
}
else if (Input.GetKey(KeyCode.A))
{
transform.Translate(new Vector3(0, 0, 1) * playerSpeed * Time.deltaTime);
}
else if (Input.GetKeyDown(KeyCode.W) && isGrounded)
{
// Instead of jumping the player teleports a certain amount upwards?
transform.Translate(new Vector3(0, 1, 0) * jumpPower * Time.deltaTime);
}
else if (Input.GetKeyDown(KeyCode.S) && isGrounded)
{
// Teleports player down
transform.Translate(new Vector3(0, -1, 0) * jumpPower * Time.deltaTime);
}
Answer by Mubanga · Aug 11, 2015 at 09:32 PM
So your code right now only does the first if
that is true and stops there. Else if
means what it is says: if the first if
isn't true try this.
So what you probably want to do is remove the else
from else if (Input.GetKeyDown(KeyCode.W) && isGrounded)
and you should be good to go.
Edit: I also noticed your jump will overwrite movement to the right/left. Try doing something like this (untested):
Vector3 move;
move = Vector3.zero;
if (Input.GetKey(KeyCode.D))
{
move.x = (-1 * playerSpeed);
}
else if (Input.GetKey(KeyCode.A))
{
move.x = (1 * playerSpeed);
}
if (Input.GetKeyDown(KeyCode.W) && isGrounded)
{
// Instead of jumping the player teleports a certain amount upwards?
move.y = (1 * jumpPower);
}
else if (Input.GetKeyDown(KeyCode.S) && isGrounded)
{
// Teleports player down
move.y = (-1 * jumpPower );
}
transform.Translate(move * Time.deltaTime)
You solution for the overwriting didn't work :/ Though the other one did! Thanks :D Edit Didn't really notice any overwriting so i'll leave it as it is :D