Search code examples
unity-game-enginecollision-detectiongame-physicskinematics

How to make two kinematic objects in Unity detect overlap?


I am making a very simple "game" that will work for a large audience of players. Up to 50 players each control an object with the mouse. When they make the objects touch, a sound plays and they light up. The spheres don't have to "collide" (IE they can overlap). But I do need to detect when they are touching.

Since they are controlled by mouse (as in: they are basically 3D mouse pointers) I think it makes sense for them to be kinematic. Since there are going to be many concurrent players I don't think it will be performant to have each one doing a manual overlap call each frame.

What I want is a way to have these kinematic objects detect touching each other. But no matter what combination of settings and components I use, I can't get OnTriggerEnter or OnCollider to work. It seems like once upon a time I could make them both triggers but that doesn't seem to work with newer version of Unity.

What is the appropriate way to accomplish what I'm trying to do?


Solution

  • https://docs.unity3d.com/ScriptReference/Rigidbody-isKinematic.html

    If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. The rigidbody will be under full control of animation or script control by changing transform.position.

    The appropriate way is don't use kinematic in this case. The main purpose of kinematic objects is to make animated stuff. The most common example is moving platforms. For your purposes you should use dynamic objects.

    You can check youtube for wide answer. There are a lot of videos that explains this topic wider. For example, this

    I asked ChatGPT to make visual table that have to help you in understanding of collisions and triggers. enter image description here

    P.S. One minor remark about velocity and collisitons. If object is moving towards another object, and the velocity of first one per frame is greater than size of the second object, then the objects won't collide. The first one will appear behind the second object in next frame. I don't know if this behaviour still appeares in new versions of Unity, but I had to warn you.