Detect collisions between objects is crucial to any game.
The game engines, always have some collision detection mechanism, and this topic is important to take in account before choose the game engine for your project.
The basic points to watch carefully are:
– How to set the collision detection;
– which basic geometric forms available for colliders(spheres, cudes, planes …) are available;
– If the game engine, support 3D mesh collisions.
When collision are detect, in general, event messages are sent through the system. And those event must be “captured” by some game object, in order to execute game routines, that will provide the game dynamics.
For these tutorial, create a scene, as shown in the image bellow:
Attention: In this tutorial I will use the names for the game objects, as the image indicates.
On Unity3D, collision detections are performed by the physics engine, so, the elements for setup your collision can be found in the Componet >> Physics menu, which are Colliders and Rigidbody.
Let’s configure the basic elements to the game objects:
Object SphereTrigger:
– check if it has the component SphereCollider, if positive, check the trigger option. If the sphereCollider is not present, you must include it. go to menu Component >> Physics >> Sphere Collider. Than, check it as trigger.
Object SphereObject:
– Check if the component SphereCollider is present, and uncheck the option “is Trigger”.
– Add a Rigidbody component, if needed (To add the rigidbody, go to menu Component >> Physics >> Rigidbody) . And uncheck the option “Is Kinematic”.
Object Cube:
– Check if it has the Box Collider component, and the “is Trigger” option, unchecked.
– Add the Rigidbody component. To add the rigidbody, go to menu Component >> Physics >> Rigidbody. And check the option “Is Kinematic” – that will allow us to move the object without physics simulations.
– Add the BasicInputHandlerCube script (shown in the user input tutorial).
– Add the BasicCollisionHandlerCube script that you can see bellow:
BasicCollisionHandlerCube:
[code lang=”java”]
private var colisionInfo:String = “Collision info”;
function Update () {
}
function OnCollisionEnter(col : Collision){
colisionInfo = “Collision Enter (Rigidbody) : “;
}
function OnCollisionExit(col : Collision){
colisionInfo = “Collision Exit (Rigidbody) : “;
}
function OnTriggerEnter(col : Collider){
colisionInfo = “Collision Enter (Trigger) : “;
}
function OnTriggerExit(col : Collider){
colisionInfo = “Collision Exit (Trigger) : “;
}
function OnTriggerStay(col : Collider){
colisionInfo = “Collision Stay (Trigger) : “;
}
function OnGUI(){
GUI.Label(Rect(200,75,300,100),colisionInfo);
}
[/code]
This demo is very simple. It will show a message in the screen, about the collision nature. The BasicCollisionHandlerCube script changes the value of the colisionInfo variable, according the collision events, and than it prints the collisionInfo variable on the GUI.
The Unity3D, uses it’s physics engine to calculate the collisions, and the colliders (BoxCollider and SphereCollider components) are the sensors used to detect when one collider touch another collider.
When a collision is detect, the Rigidboby component, is informed, and sends an event message to the game object components according the collision’s nature. If one of the colliders were a trigger it sends the messages OnTriggerEnter, OnTriggerStay, and OnTriggerExit. Otherwise the rigidbody send the messages OnCollisionEnter, OnCollisionStay, and OnCollisionExit.
In this example, when the Cube collides with SphereTrigger, the message OnTriggerEnter, is sent when the collision starts; the message OnTriggerStay, is sent while the objects are colliding; the message OnTriggerExit, is sent once the objects stop colliding.
The same thing happens when the Cube collides with the SphereCollider, but in this case the messages are OnCollisionEnter, OnCollisionStay, and OnCollisionExit, because in this collision none of the colliders are set as triggers.
Check the final sample here.
What do you do with the other spheres?? Have fun!! Set their properties as you want, check how they behave in collisions, try to experiment some scripts… explore!!