- Home /
How to shift the player to a definite position in the x-axis using key presses?
I am essentially trying to create a subway surfers game for a project. So the plane is divided into three tracks. Left, Middle, Right. If the player is at right, I want to move the player to the middle position and if the player is in the middle, the player has to move to the extreme left position, when pressed "a".
But when I press A, the player initially in the rightmost position(-2,0,0) goes directly to (2,0,0) (left) instead of middle(0,0,0). This is because the update function works very fast and hence press "a" satisfies both conditions. Any solutions?
if(Input.GetKey("a")){
Vector3 p = transform.position;
if(p.x==0){
Debug.Log("Moved to 2");
transform.position = new Vector3(2, 0, p.z);
}
if(p.x==-2)
{
Debug.Log("Moved to 0");
transform.position = new Vector3(0, 0, p.z);
}
}
Answer by asafsitner · Aug 29, 2021 at 11:33 PM
Input.GetKey
returns true
every frame that the key is held down.
If you want to catch just a single press, you probably want to use Input.GetKeyDown instead.