- Home /
2D Rigidbody rebound with RigidBody.velocity
I'm using RigidBody.velocity to move my player like this:
_rigidBody.velocity = new Vector2(
_horizontal * _movementSpeed * Time.deltaTime,
_vertical * _movementSpeed * Time.deltaTime
);
And I flip the sprite depending if the player is moving to +x or -x like tihs:
private float _horizontal;
private float _vertical;
private float _xPosition;
private void Update()
{
_horizontal = _input.Player.HorizontalMovement.ReadValue<float>();
_vertical = _input.Player.VerticalMovement.ReadValue<float>();
float actualXPosition = _transform.position.x;
if (actualXPosition > _xPosition)
{
_sprite.flipX = false;
}
else if (actualXPosition < _xPosition)
{
_sprite.flipX = true;
}
_xPosition = _transform.position.x;
if (_rigidBody.velocity.magnitude > 0)
_animator.SetBool("walk", true);
else
_animator.SetBool("walk", false);
}
But when I hit a collider it gets a little bit to the opposite side its aheading like this gif shows.
I have my Player object RigidBody 2D Collision Detection to Continuous and played a little with other configurations and didnt got anything.
What I can see in the RigidBody 2D component is in the Info area that when I hit a wall it gets the Position X to the opposite value of the direction its going.
And if I hit the same collider that flipping bug do not happen again. I have to hit one and them a different one to this to happens.
Can I solve this changing my code, changing setting or other thing that can remove this rebound, wich will in the future affect other things.
Thanks in advance.