Question by
Wolf_dead_inside · Oct 19, 2020 at 02:40 PM ·
unity 2dmovement script
rb.moveposition cant do diagonals
when holding 2 keys at once, the player will only move on one axis at a time. how can i make the player move diagonally?
code is here:
private void ApplyMovement()
{
if (Input.GetKey(KeyCode.W))
{
rb.MovePosition(transform.position + (transform.up * movementSpeed * Time.fixedDeltaTime));
}
if (Input.GetKey(KeyCode.S))
{
rb.MovePosition(transform.position + (-transform.up * movementSpeed * Time.fixedDeltaTime));
}
if (Input.GetKey(KeyCode.D))
{
rb.MovePosition(transform.position + (transform.right * movementSpeed * Time.fixedDeltaTime));
}
if (Input.GetKey(KeyCode.A))
{
rb.MovePosition(transform.position + (-transform.right * movementSpeed * Time.fixedDeltaTime));
}
}
if its important the function is being run in fixed update and movement speed is 4. thank you!
Comment
Your answer
Follow this Question
Related Questions
Diagonal movement, stopping at screen edge 0 Answers
I have problem with using vs with unity 0 Answers
Player making "random jumps" while is moving at random places (Unity 2D) 0 Answers
How to make GoalKeeper move within post but not move towards to the player? 1 Answer
How can I get the player to face the direction it is going in a Unity 2D Game? 0 Answers