Player cannot stop moving after key released!
I want to make my player move only using "WASD" , so I have this script:
void FixedUpdate()
{
//float moveHorizontal = Input.GetAxis("Horizontal");
//float moveVertical = Input.GetAxis("Vertical");
if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.S))
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
}
if(Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
{
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
Vector3 movement = new Vector3(moveHorizontal, 0.0f, moveVertical);
rb.velocity = movement * speed;
}
}
The player moves when I hit "WASD" but when I, say, I hit "W", it'll just keep moving forward itself even after I realeased the key. I'm not talking about the " GetAxis " soomthing effect here, I'm saying the player will move at the same speed on its own forward.......
Please help! Thanks in advance!
Hello, do you $$anonymous$$d accepting my answer since it is correct. It really makes me stay motivated to answer these questions.
Answer by AurimasBlazulionis · Sep 08, 2016 at 06:40 PM
You should just remove the if statements and the whole part inside KeyCode.A and KeyCode.D statement since it is the same thing. If you keep all statements, then who applies the velocity when all the keys are released? That is right, no one.
But if I have sth. like:
float moveHorizontal = Input.GetAxis("Horizontal");
float moveVertical = Input.GetAxis("Vertical");
rb.velocity = movement * speed;
Then both "WASD" and Arrow$$anonymous$$eys can control the movement of the player, that's not what I want, I'm saving the Arrow$$anonymous$$eys for sth. else.
You can go to input settings and define custom axes only with wasd. Then use these for your movement script.
Answer by cvaughan02 · Sep 08, 2016 at 06:48 PM
Given that you have a non-standard control scheme, wouldn't it be easier to just change your axis definitions to suit? since the arrow keys aren't actually controlling Horizontal and Vertical?
Could you please elaborate on how to change axis definitions? Thanks a lot!