- Home /
Character slows down when running against a wall (2D)
Basically I have a character with a collider and rigidbody and a wall with a collider in a 2D setting.
The character's rigidbody has no Drag or Gravity (all 0).
The character moves using this code during Update():
Vector2 Velocity = new Vector2(0,0);
if (Input.GetKey(KeyCode.W))
{
Velocity.y = 5;
}
else if (Input.GetKey(KeyCode.S))
{
Velocity.y = -5;
}
if (Input.GetKey(KeyCode.A))
{
Velocity.x = -5;
}
else if (Input.GetKey(KeyCode.D))
{
Velocity.x = 5;
}
GetComponent<Rigidbody2D>().velocity = Velocity;
If I move down and right (S & D keys) it moves fine until I hit a wall below me, at which point the movement on the Y axis stops completely (as it should, I hit a wall), but the movement along the X axis slows down as well. Why does this happen? I don't want the X-axis velocity to slow down just because they are also trying to run along a Y axis that is blocked by a wall.
Answer by AlienNova · Oct 22, 2017 at 12:11 AM
Are you trying to move the player diagonally into the wall?
Essentially yes. If running diagonally I'd be at -5Y and +5X, but if I am running into the wall on the Y Axis, should it not then become 0Y and +5X? It doesn't make sense that it slows on the X axis.
I could do this with custom code by checking collision before setting the movement but I'd like to use what's built-in if possible.
Oh, also, a little unrelated, but still an issue with what's built-in. If you run parallel to the wall and tap down (moving down on the Y Axis) it will occasionally dip down into the wall for a frame until it corrects the collision. This doesn't happen when holding down the button, only when tapping it.
Your answer
Follow this Question
Related Questions
Object hits another but doesnt bounce off again. 0 Answers
Moving colliders that are part of a composite collider 1 Answer
How to select the trigger layer on a Box Collider 2D 2 Answers
Rigidbody2D.velocity out of controll 3 Answers
How to stop projectile collider (is trigger) from colliding with other colliders (also is trigger) 2 Answers