OnColliderEnter vs OnTriggerEnter - Overview of Collisions
In the previous article, we discussed ‘Rigidbodies.’ In today's article, we will be taking a look at ‘Colliders.’
What are Colliders and Collisions?
Colliders are used when you want interactions between your GameObjects. They can be imagined as the physical structure of the GameObjects. There are many types of Colliders in Unity - and which one we need is dependant upon the GameObject and the desired functionality. Similar to Rigidbody - there are 2D and 3D versions, which do not interact with each other.
Collisions are events that happen when two or more GameObjects with Collider components interact with each other in the scene - they touch each other or overlap the space in the scene.
Unity provides us with two main types of collision methods: ‘OnCollisionEnter’ and ‘OnTriggerEnter.’
How to setup Colliders
Setting up Collider is as easy as adding another component to the GameObject. Each collider has different settings on them, but there are three things in common:
We will get to explain all of them, but the first one allows the user to adjust the collider's positioning and size.
OnCollisionEnter
This method is used for solid, hard collisions and interactions.
The event for this method gets detected at the point of contact between the colliders. The object that detects the event must also have a ’Rigidbody’ component. The Collision variable contains information, for example, about contact points and impact velocity.
OnTriggerEnter
This method is used for passthrough collisions.
In order for this method to work, one of the colliding GameObjects needs to have a ‘Is trigger’ setting checked. A collider with this setting enabled does not act as a solid body; instead, it allows the GameObjects to pass through it.
The event happens on the FixedUpdate function when two GameObjects collide. The Colliders involved are not always at the point of initial contact.
When to use each one?
The easiest way to differentiate between the two types of Collision is to imagine them. OnCollisionEnter can be visualized as a person hitting a wall, and OnTriggerEnter can be pictured as a runner winning the race and hitting the finish line ribbon.
OnTriggerEnter is used if the GameObject doesn’t need the physics engine to control it but still needs to know if another GamObject passed through it.
OnCollisionEnter is used if the physics engine controls the GameObject, so it knows if another GameObject collided with it.
Stay/Exit
Apart from ‘OnCollisionEnter’ and ‘OnTriggerEnter’ methods, there are also four others:
- OnCollisionStay - This method is called every frame while two or more objects with Rigid Bodies and Colliders are touching.
- OnCollisionExit - This method is called once when two objects with Rigid Bodies and Colliders stop making contact.
- OnTriggerStay - This method is called intermittently while a Collider is contacting or overlapping the Trigger area.
- OnTriggerExit - This method is called after a collider stops being in contact with a Trigger area.
Physics Materials
The Physic Material is used to adjust the friction and bouncing effects of colliding objects.
To create a Physic Material, select ‘Assets’ > ‘Create’ > ‘Physic Material’ from the menu bar. Then drag the Physic Material from the Project View onto a ‘Collider’ in the scene.
And that is all today in regards to the Collider components. I hope I have given you an insight into Colliders and when to use them.
But that is it for now, thank you for reading and feel free to follow me for more articles - and as always, good luck and see you next time!