Collisions in high speed without unity physic / rigidbody

You say you’re not using 2D physics but you’re using queries therefore obviously colliders. This IS using 2D physics therefore you need to use it correctly so here are some basic rules:

  • Don’t ever modify the Transform if using 2D physics components

  • If you want a 2D Collider to move then add a Rigidbody2D and use its API to move it; many ways to do this.

  • Rigidbody2D don’t make it “unresponsive”. If you want explicit control then set the body-type to Kinematic; it’s what it’s for.

  • When you query, don’t use the Transform position as a reference, it’s not the authority on the pose, that’s the Rigidbody2D so use its position/rotation properties.

If you don’t use a Rigidbody2D but 2D colliders then you’re implicitly saying Static (non-moving) colliders but then go and change the Transform going against that. Changing the Transform on a GameObject with only a Collider2D is relatively expensive as it requires the Collider2D to be recreated from scratch. Even worse, you’re likely doing so per-frame when physics doesn’t even run per-frame by default unless you’ve selected that. Also, changing a Transform doesn’t even update anything other than the Transform. Renderers only know this when they render. Likewise physics only knows this when it runs which is again, by default, during the fixed-update.

If you change the Transform, nothing in the physics (or any other system) will follow this change. This means that if you then also run a query, it’ll be querying colliders as they were the last simulation step and won’t be related to the change of Transform.

So from above, adding a Rigidbody2D and setting it to be Kinematic means it’s entirely under your control. Use its API to cause it to move. Use the Rigidbody2D.position/rotation properties to refer to the position. If you want smooth motion then either set the physics to update per-frame or leave it at default and set the Rigidbody2D to use interpolation.

There are loads of queries such as casting a Rigidbody2D or an individual collider. You can even ask the distance which also shows overlap so you can resolve overlap collisions yourself. You can control all this detection and movement.

You just need to stop driving the Transform system as if you’re driving a per-frame renderer.

4 Likes