[3D]Collision not working.

Hello everyone,

I am working on a project and have been hung up on collisions for almost two hours now. I’ve read through countless articles and my code matches those I’ve read yet I see no results.

In my project I have a mouse that moves towards a block of cheese. My goal is when he mouse collides with the cheese for it to remove 10 health from the cheese and remove the mouse. I added debug logs and the script isn’t even firing.

This is on the cheese gameObject:

private void OnCollisionEnter(Collision collision){
    		if (collision.gameObject.name == "mouse"){
    			Debug.Log("[CHEESE] - Collision Detected");
                        currentHealth -= 10;
    		}
    	}

This is on the mouse gameObject:

private void OnCollisionEnter(Collision collision){
		if (collision.gameObject.name == "cheese"){
			Destroy(collision.gameObject);
			Debug.Log("[MOUSE] - Collision Detected");
		}
	}

Both gameObjects have a rigid body and a box collider. Collision Detection is “Continuous”.

mouse Component Settings:
IsTrigger: True
IsKinematic: False

cheese Component Settings:
IsTrigger: True
IsKinematic: False

Thanks a lot in advance for all help given. :slight_smile:

-Joshua4missions

Colliders are trigger. Try with

. void OnTriggerEnter(){}

As fafase mentioned, you can get two kinematic rigid bodies to detect a collision with triggers.

If you test your original method with one of the objects non-kinematic, the other kinematic, your code will be called fine. However, with both as non-kinematic rigid bodies, using triggers can solve your problem.

Set both your collider ‘Is Trigger ?’ values to true.

Change OnCollisionEnter to OnTriggerEnter like so:

private void OnTriggerEnter(Collider collision)
{
	if (collision.gameObject.name == "cheese")
	{
		Destroy(collision.gameObject);
		Debug.Log("[MOUSE] - Trigger Detected");
	}
}

Notice the parameter changes from a Collision object to a Collider object.