Should I put a rigidbody on colliders moved by script ?
Hi,
I'm wondering if a rigidbody is required or not when moving a collider by script. This collider can collide with dynamic objects (these objects have a non-kinematic rigidbody), and no script callback (OnCollisionXXX) is required.
I tried both (without rigidbody attached and with a kinematic rigidbody attached), and the result look the same : All the dynamic objects collide normally with this "moving wall".
I've heard that it's best practice performance-wise for the physics engine to have a kinematic rigidbody on moving colliders. Is this true ? or is it only true if I want collision callback ?
Thanks a lot!
Answer by Edy · Jan 17, 2021 at 08:41 PM
If using 3D physics there's no need to add a Rigidbody to moving colliders. There will be no performance gain, maybe the opposite (as there's an additional Rigidbody the physics engine has to handle).
OnCollisionXXX callbacks are invoked as long as one of the involved colliders has a Rigidbody attached.
If using 2D physics, as far as I know it's the opposite: add a rigidbody to your moving colliders to avoid performance penalties.
Ok I'm confused :D
For example, this tutorial says the opposite : https://learn.unity.com/tutorial/physics-best-practices
"Game objects which don’t have a RigidBody component are considered static colliders. This is important to be aware of because it’s extremely inefficient to attempt to move static colliders, as it forces the physics engine to recalculate the physical world all over again. "
The thing is that this advice seems to be from Unity 4.x. It's been a while since. Did the physics engine changed in a way that this is not required anymore ?
Yes, that changed in Unity 5 (note the tutorial is for Unity 4). It's not an issue in 3D physics anymore. Please read:
https://forum.unity.com/threads/collision-detection-for-kinematic-rigidbodies.885778/#post-6466924
That forum post includes links to the official references on the issue. It's stated in both the release notes and the Unity Blog post for Unity 5.
Thanks a lot for the very quick and clear answer !