Collision Detection for Kinematic Rigidbodies

For my 3d game, I don't require any real physics within the game.
But my cube objects are moving and I require to detect their internal collisions.

As I understand, every moving collider object must require rigidbody within it. So I have attached my all moving cube objects kinematic rigidbody like this:

5820265--616699--cube inspector.png

Now how to detect collision for two kinematic rigidbodies?
I don't want to convert it to non-kinematic body otherwise physics started applying on them.

3 Likes
  • Configure "Project Settings > Physics > Contacts Pair Mode" to "Enable All Contacts"
  • Move your kinematic bodies using Rigidbody.MovePosition and Rigidbody.MoveRotation from FixedUpdate.

Now the collision events (OnCollisionEnter, etc) will happen normally as with standard rigidbodies.


Since Unity 5 this is no longer a hard requirement. The documentation is outdated about this. You can have static colliders moving in your scene without performance penalties.

However, if you need your moving colliders to cause physically correct effects on other objects, then you should move them as described above.

35 Likes

Your reply made me more confused, just a few days ago I watched the latest GDC video they are clearly saying we must have to attach a rigidbody component to the GameObject which has collider attached and moving.
Otherwise, you will get a performance problem and there is a movement issue too.

I can't able to find specific video related to this topic and it was not old Unity 5 version video.

2 Likes

Just use profiler and test it.


IT WORKED! Thank you bro ;)

1 Like


Very useful for me, thank you!


I forgot to reply this. Since Unity 5 this is no longer a requirement in 3D Physics. Probably those devs in the GDC video are not aware either.

From Unity 5 release notes:
https://unity3d.com/unity/whats-new/unity-5.0
6466924--725617--upload_2020-10-28_18-1-51.png
Also described in the Unity blog:
https://blogs.unity3d.com/2014/07/08/high-performance-physics-in-unity-5/

Note that 2D physics may have different requirements.

2 Likes


Can you give me more details on that? i am making a free fly camera

I think it's already clear enough...


I have done all things you said but it still can't detect collision with OnCollisionEnter function

you can use dynamic property with gravity scale zero
and freeze its positions and rotation if needed


@Edy

Hey Edy, you saved my ass a few times yet!
When it comes to physics in unity it seems you always have a solution or tipp... you truly are a wizard and a big contribution to this community!
I'm using another vehicle physics system (NWH) for now and it works great for me, but I'm going to purchase your asset anyway soon! Just because.. shit... your posts helped me a LOT!!
Thank you for your contribution and keep going!
Michael

1 Like


Thank you so much! I also had a hard time learning, and now I'm happy to help :)

5 Likes

@Edy Thanks for this one, made my collisions work by changing it to "Enable Kinematic Kinematic Pairs"
[quote]
- Configure "Project Settings > Physics > Contacts Pair Mode" to "Enable All Contacts"
[/quote]

Now I'm a bit confused about this part
[quote]
- Move your kinematic bodies using Rigidbody.MovePosition and Rigidbody.MoveRotation from FixedUpdate.
[/quote]

I was under the impression that isKinematic should be used when you want to move your objects by Transform but still need to be able to detect collisions. Is there a reason why you should use Rigidbody.MovePosition instead ?


Modifying the Transform means teleporting the object overriding the calculations of the physics solver. This won't provide the correct physics effects, friction for example. You shouldn't modify the Transform on any Rigidbody unless there's a justified reason to do so (i.e. teleporting an object back to it's startup position is a justified reason).

For example, imagine a non-kinematic cube sitting on top of a kinematic cube:

  • Moving the kinematic cube with Transform: the non-kinematic cube stays steady in the scene and eventually falls when the kinematic cube has moved enough.
  • Moving the kinematic cube with Rigidbody.MovePosition(): the friction force has effect so the non-kinematic cube keeps "fixed" on top of the kinematic cube, unless this one moves fast enough to overcome the friction.

Additionally, Rigidbody.MovePosition (and Rigidbody.MoveRotation) support the Rigidbody interpolation options, so visual motion remains smooth at any time scale and frame rate.

5 Likes

Thanks! This is very useful to know. Unity should hire you to write the physics manual section.

6 Likes

onCollision Worked for me with (Not trigger collider + Kinematic rigid body) with only enabling all pairs in

  • "Project Settings > Physics > Contacts Pair Mode" to "Enable All Contacts"

i.e. I didn't need to use RigidBody.MovePosition in FixedUpdate(), changing transform.position in Update() worked also.

My Version: Unity 2021.3.15f1

I cant find contacts pair mode and im using physics 2d


It's per Rigidbody2D in 2D. The setting is here.