OnCollisionEnter Vs. OnTriggerEnter — When to use them?
After a gameObject’s creation and the coding is in place to give them some movement through the space, they will not collide with each other.
They move through other gameObjects without change. They ignore other objects.
A Collider adds a net of coordinates around the gameObject. A cube object has a cube shape net, a capsule object has a surrounding radius and length Collider net.
A ridgidbody is next added to the gameObject.
Shooting a capsule (a Lazer) at a cube (an Enemy) should create the visual of the capsule entering the cube and disappearing. Or if you prefer, the cube disappears when the capsule is touched, or both disappear after touching each other.
Because there is no bounce off of each other, this is an OnTriggerEnter instead of an OnCollisionEnter.
A trigger collision might also be considered a pass-through collision without the followup bounce.
Next determine which objects receive RidgidBody status. Extra unneeded RidgidBody choices make unneeded drag on performance.
“Either of the two objects must have a Ridgid component” — onTriggerEnter.
Scripting API -> Unity Engine -> Classes -> MonoBehavior -> Messages
Here are some trigger messages:
Here are some hard collision messages:
Enter: when collision occurs
Stay: during collision
Exit: end of collision
In this example, a Player object shoots (creates) a moving Lazer object that travels to collide with an Enemy object.
<Click> on Add Component and search for Rigid
When the Rigidbody component is added, “Use Gravity” is checked.
<uncheck> “Use Gravity” to prevent the Laser movement with physics on a collision.
In the example, Enemy object drop and move downward in the scene, so collisions are possible with the Player object, so logic will need to be added, requiring another Rigidbody — the Enemy object. So add the Rigidbody component to the Enemy object as was done with the Lazer Object
Now two of the three objects have Rigidbody components without “Use Gravity”. The game logic can be coded.
private void OnTriggerEnter(Collider other)
{
Debug.Log("Hit: " + other.transform.name);
}
Using OnTriggerEnter (here inside the Enemy class) to first note what the colliding object is. “other” gives us the object. “transform” gives us the root instance of the object. “name” gives us the name component of the object as a string, useful in a Log message.
private void OnTriggerEnter(Collider other)
{
Debug.Log("Hit: " + other.transform.name);
// if other.name is Player
// damage player
// destroy this Enemy
// if other.name is Laser
// destroy Laser
// destroy this enemy
}
In the code above, notice that the “destroy this …” follows the “destroy that …”
What is a Tag?
A tag links an other object with an object in the game hierarchy.
Player appears in the drop down menu, okay for a Player tag to a Player object.
A Tag to an Enemy object will need to added to the Tag list using the ‘+’ icon.
A new Tag name can be added to the list of Tags
And then the new name “Enemy” appears in the list as Player appeared.
Player has a Player tag. <click> and Enemy has an Enemy tag. Now apply the Overrides to Enemy
Lazer is not in the Hierarchy, and the message appears in the Inspector where the Overrides was located for Enemy.
Ready to code with a tag? This example is taken from:
Unity — Scripting API: GameObject.tag (unity3d.com)
using UnityEngine;
public class Example : MonoBehaviour
{
void Start()
{
//Set the tag of this GameObject to Player
gameObject.tag = "Player";
}
private void OnTriggerEnter(Collider other)
{
//Check to see if the tag on the collider is equal to Enemy
if (other.tag == "Enemy")
{
Debug.Log("Triggered by Enemy");
}
}
}
In our example, keep in mind that the Collision code is inside the Enemy class and inside the Enemy.cs script file. So “this.gameObject” is the Enemy. “other.gameObject” is either the Laser or the Player.
private void OnTriggerEnter(Collider other)
{
Debug.Log("Hit: " + other.transform.name);
// if other.name is Player
// damage player
// destroy this Enemy
if (other.tag == "Player")
{
Debug.Log("Hit Player! Hit Player!");
Destroy(this.gameObject);
} else if (other.tag == "Laser")
// if other.name is Laser
// destroy Laser
// destroy this enemy
{
Debug.Log("Lazer Hit! Lazer Hit");
Destroy(other.gameObject);
Destroy(this.gameObject);
}
}