Colliders are components which define the shape of a GameObject
for physical collisions.
GameObjects
are the foundational objects that represent characters, props, and all objects inside a scene – every object in your game is a GameObject
.
Note:
GameObject
and all other components are a part of theUnityEngine
class.
The OnCollisionEnter
function is called whenever a GameObject
collides with another one.
void OnCollisionEnter(Collision collision){Debug.Log(collision.name);}
onCollisionEnter
method takes only one parameter of type Collision
which stores the information about the collision events. It includes the collision direction, the point of contact, the name of the GameObject
, relativeVelocity
, and much more.
There are several reasons for the OnCollisionEnter
method not being called when the two objects collide.
For the solutions explained below:
The blue ball is labeled "Spehere 1".
The black ball is labeled "Sphere 2".
The cube at the bottom is the floor.
Rigidbody
property is not setOne of the GameObjects
must have a Rigidbody component for the onCollisionEnter
method to work. The collision occurs only when the
Solution: You need to make sure that the GameObject
s have a Rigidbody component attached.
In our case, only one of the GameObject
s has a Rigidbody
component (the floor). The onCollisionEnter
method will not be called because Sphere 2 will not move from its position to collide with the floor.
As shown above, only Sphere 1 (the blue ball), has the RigidBody
component attached.
Select the GameObject
In the Inspector menu, select "Add Component"
Select Rigidbody
The script containing the onCollisionEnter()
method is attached to both Sphere 1 and the floor, so it gets called for both objects as the output shows in the console in the picture below.
Note: The method will be called even if the object doesn't have a Rigidbody component attached but something has to collide with it first.
If you set the "Is Trigger" property, the onTriggerEnter()
method will be called instead of the onCollisionEnter()
method. In this case, the colliders will move freely through each other instead of colliding.
Solution: Make sure to uncheck the Is Trigger
property if you want the GameObjects
to collide.
Note:
If you want your colliders to interact physically with the world, "Is Trigger" property should be unchecked.
If you want your collider to act as a trigger for an event when something enters it, "Is Trigger" property should be checked.
Here, Sphere 2 is has the "Is Trigger" property enabled. It will pass through Sphere 1 and the onCollisionEnter()
method will not be called.
The GameObject
becomes kinematic if this property is set to true. It means that the object will not be affected by any forces applied to it. This makes the case similar to when the Rigidbody component is not attached to the object. It will not move and hence the collision will never occur.
Solution: If you want the collisions to happen, make sure to set the "Is Kinematic" property to false.
Sphere 2 (the black ball) has the "Is Kinematic" property enabled. It will not show any motion whereas Sphere 1 (the blue ball) falls to the floor and calls its onCollisionEnter()
method.
If we set both of the GameObjects
to have Is Kinematic
property, the objects will not collide and hence there will be no motion as shown below.
Solution: Make sure that the GameObject
is attached with its relevant script, and the methods are written correctly.
Note: Make sure to add the
onCollisionEnter
method outside theUpdate()
function.
Click on the Add Component
In the search bar, type the name of the C#
script and hit enter.
If the scripts are not attached to the GameObjects
, the methods specified in the script will not be called. In the demonstration below, both of the objects collide with each other. But the onCollisionEnter()
method is not called.
Unity allows you to specify which specific layers can interact with each other. If the layers of two specific GameObject
s are not set to interact with each other, the onCollisionEnter()
method will not be called.
Solution: Check for the layer based settings in the Physics Manager.
Select Edit
Go to Project Settings
Select Physics
Scroll down and check Enable All
Every GameObject
has the Layer set to Default. You can choose which specific layers can interact with each other (check or uncheck the layers) by following the method shown above.
If all of the layers are disabled, the GameObject
s will not interact with each other as shown below.
onCollisionEnter()
is called when both the GameObject
s have the Rigidbody component, C#
script attached and furthermore, have the "Is Trigger" and "Is Kinematic" properties set to false.