Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Collision Detection for Kinematic Rigidbodies

Discussion in 'Physics' started by siddharth3322, May 8, 2020.

  1. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,041
    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:

    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.
     
    honor0102, havokentity and WoodyDRN like this.
  2. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,414
    • 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.
     
  3. siddharth3322

    siddharth3322

    Joined:
    Nov 29, 2013
    Posts:
    1,041
    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.
     
    JoeStinky and erhankorkmaz01 like this.
  4. Modafuka

    Modafuka

    Joined:
    Nov 21, 2019
    Posts:
    45
    Just use profiler and test it.
     
  5. alonesher

    alonesher

    Joined:
    Aug 18, 2018
    Posts:
    1
    IT WORKED! Thank you bro ;)
     
    unity_Yw2Svg4miw7HMw likes this.
  6. AdrianoDeRegino

    AdrianoDeRegino

    Joined:
    Aug 10, 2019
    Posts:
    87
    Very useful for me, thank you!
     
  7. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,414
    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
    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.
     
    honor0102 and Rispat-Momit like this.
  8. FormalSnake

    FormalSnake

    Joined:
    Oct 26, 2019
    Posts:
    12
    Can you give me more details on that? i am making a free fly camera
     
  9. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,414
    I think it's already clear enough...
     
  10. md-creator

    md-creator

    Joined:
    Oct 7, 2020
    Posts:
    1
    I have done all things you said but it still can't detect collision with OnCollisionEnter function
     
  11. hamzaali1410

    hamzaali1410

    Joined:
    Jan 29, 2018
    Posts:
    1
    you can use dynamic property with gravity scale zero
    and freeze its positions and rotation if needed
     
  12. nakoustix

    nakoustix

    Joined:
    Apr 21, 2019
    Posts:
    15
    @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.. S***... your posts helped me a LOT!!
    Thank you for your contribution and keep going!
    Michael
     
    Edy likes this.
  13. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,414
    Thank you so much! I also had a hard time learning, and now I'm happy to help :)
     
  14. Skflowne

    Skflowne

    Joined:
    Apr 6, 2014
    Posts:
    14
    @Edy Thanks for this one, made my collisions work by changing it to "Enable Kinematic Kinematic Pairs"
    Now I'm a bit confused about this part
    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 ?
     
  15. Edy

    Edy

    Joined:
    Jun 3, 2010
    Posts:
    2,414
    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.
     
    tomtefar, Ohilo, mbalcipt and 2 others like this.
  16. Skflowne

    Skflowne

    Joined:
    Apr 6, 2014
    Posts:
    14
    Thanks! This is very useful to know. Unity should hire you to write the physics manual section.
     
    tomtefar, JoeStinky, Ohilo and 3 others like this.
  17. yonixw

    yonixw

    Joined:
    Dec 25, 2012
    Posts:
    2
    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
     
  18. junior333istall

    junior333istall

    Joined:
    Jan 11, 2023
    Posts:
    101
    I cant find contacts pair mode and im using physics 2d
     
  19. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,255
    It's per Rigidbody2D in 2D. The setting is here.