I’m making a 4-player top down arcade game and the collision between players is not behaving like i would like it.
You can see here the behaviour:
I would like to get rid of that awkward twitching when multiple colliders intersect.
I am using transform.position to control the players, and i’m guessing that is the cause of the problem. Is there any workaround this? Or is the solution to use physics instead and apply velocity to move the players?
Besides this problem i like how it’s behaving, and wouldn’t want to deal with acceleration and deceleration.
With Physics (or Physics2D), never manipulate the Transform directly. If you manipulate the Transform directly, you are bypassing the physics system and you can reasonably expect glitching and missed collisions and other physics mayhem.
Always use the .MovePosition() and .MoveRotation() methods on the Rigidbody (or Rigidbody2D) instance in order to move or rotate things.
Thanks for the reply, didn’t know about MovePosition.
From what i can understand it tells the rigidbody to go to a particular position. My players are controlled in real time with a directional gamepad by holding pressed the direction it should go to, in this case AddForce seems more adequate.
I also looked into wheel colliders but that seems to be exclusive to non-2d rigidbodies, so not sure whether it’s usable in my project.
So you know, the reason for the “twitching” is that the Rigidbody2D in Dynamic body-mode solves overlaps for you when the simulation runs. A Rigidbody2D will write its position/rotation to the Transform. When you write directly to the Transform, you’re therefore stomping over what the Rigidbody2D is there for so you place it into a position that is likely overlapped and then, when the simulation runs, it tries to sovle the overlap and next update, you stomp over the position again, likely causing another overlap.
Adding a forces means you’re not stomping over the position. Using MovePosition/MoveRotation will calculate a velocity required to move the body into that pose. It’s designed for Kinematic bodies but in 2D it works perfectly well for Dynamic bodies.
For Dynamic bodies using MovePosition/MoveRotation, you are NOT guaranteed that the body will reach the position/rotation because of collision detection and response i.e. the solver won’t cause an overlap. This is why using this on a Dynamic body stops jitter. It does mean however that you won’t be reaching the position/rotation you ask for if you contact something. This is desired behaviour though for a Dynamic body.
A Kinematic body doesn’t have a collision response as it doesn’t respond to forces so will always reach the specified position/rotation.
Hope that helps the “why”.
Also note, for this kind of simulation, you are free to set the simulation mode to per-frame too so the physics runs per-frame. You might find this easier to work with too as you no longer need to mess with FixedUpdate and Rigidbody2D interpolation is not required. You can find this setting in “Project Settings > Physics 2D > Simulation Mode” with “Update”. Worth experimenting with.