Daniel Yang
Daniel Yang

Reputation: 3

How do you fix bouncing in 2D collision in Unity?

My game is a topdown 2D game, and when the player collides with another object while it is moving the player starts bouncing off. I've tried using a 2D physics material with 0 bounciness and it doesn't solve the problem. I think it has something to do with the way my movement code. I tried using Rigidbody.MovePosition, but it didn't work and my player was glitching out. This is my movement code.

float horizontal = Input.GetAxis("Horizontal") * moveSpeed * Time.deltaTime;
float vertical = Input.GetAxis("Vertical") * moveSpeed * Time.deltaTime;

transform.Translate(new Vector2(horizontal, vertical));

Upvotes: 0

Views: 2145

Answers (1)

Alex Khazov
Alex Khazov

Reputation: 30

Try using the MovePosition method on your Rigidbody inside of FixedUpdate instead of changing the position of the player's transform. If that does not solve the issue, try also changing the collision detection mode of the player's rigidbody to continuous.

When working with physics and Rigidbodies in Unity, you would want to do most of the physics related things in the FixedUpdate method. This method runs in sync with Unity's physics loop and all the physics calculations are done right after all of the FixedUpdate methods have been called. This allows for smooth looking interactions and physics.

Upvotes: 1

Related Questions