isKinematic rigidbody collisions

I want to detect collisions between two moving box colliders but have no need for any physics calculations to occur. I am currently moving them using their transform’s in scripts attached to them. I set them both to have isKinematic=true rigidbodies and attached a box collider to each with isTrigger=true.

I also attached a script to each box:

void OnTriggerEnter(Collision collision)
{
    Debug.LogError("HIT");
}

However I am not seeing any collisions, I have verified that the boxes collide at one point in the scene editor whilst debugging the game but no messages are logged. Is there something I am doing incorrectly?

3 Answers

3

Change the type of your parameter to Collider instead of Collision

Hmmm...yes, the parm type is wong, but that normally gives an error. Unless the script isn't a monobehavior?

Shouldn't that give an error?

if you want to enable it when it hits add rigidbody.isKinematic = false; to your script
other then that freeze the postion & rotation of the rigid body to avoid it moving around.
from the wiki

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. Kinematic bodies also affect the motion of other rigidbodies through collisions or joints. Eg. can connect a kinematic rigidbody to a normal rigidbody with a joint and the rigidbody will be constrained with the motion of the kinematic body. Kinematic rigidbodies are also particularly useful for making characters which are normally driven by an animation, but on certain events can be quickly turned into a ragdoll by setting isKinematic to false.

Intended behavior. From the docs (Unity - Scripting API: MonoBehaviour.OnTriggerEnter(Collider)):

“Note that collision events are only sent if one of the colliders also has a non-kinematic rigidbody attached.”

I assume this is because the physics system sends the messages, which saves time by not checking things that it will never move. Not to say there aren’t workarounds. Wonder if checking all the constraints and leaving isKen off would work (google has more suggestions.)