Unity On Collision Enter



Hi In this Unity tutorial, I will show you how to use OnCollisionEnter and OnTriggerEnter Functions in Unity 3DThe script you can found in this video and use. In contrast to OnTriggerEnter, OnCollisionEnter is passed the Collision class and not a Collider.The Collision class contains information about contact points, impact velocity etc. If you don't use collisionInfo in the function, leave out the collisionInfo parameter.

Unity on collision enter. Csharp by Confused Coyote on Oct 09 2020 Donate. 0 Source: docs.unity3d.com. Add a Grepper Answer. C# answers related to “unity oncollisionenter” check for collision unity c#; collision detector unity c# 2d; doing void when gameobject setactive unity. OnCollisionEnter messages will fire on the GameObject containing the Rigidbody, reaching your parent control script without needing to write an extra relay script to stick on each collider. If you need to find out which of your child colliders was involved in the collision, you can do it like so. Unity is the ultimate game development platform. Use Unity to build high-quality 3D and 2D games, deploy them across mobile, desktop, VR/AR, consoles or the Web, and connect with loyal and enthusiastic players and customers.

Collision detection is required for all types of games. Detecting and utilizing the collision is implemented in a different manner in different game engines. Unity collider can be added to any game object as a component. Also, you need to understand how colliders and rigidbody work together to efficiently use Unity colliders. Without a basic understanding of how Unity Colliders work, it will be very difficult to code the game mechanics.

If you are totally new to Unity check out how to learn Unity.

Unity Oncollisionenter Collider

Unity colliders are very simple to use. Unity has divided the colliders into 2D and 3D. So you have to very careful which collider are you using. All your codes and collision detection will change for 2D and 3D. Further, there are types, static collider, rigidbody collider, kinematic rigidbody collider. The collider used in all these are the same, the type is for how the object to which the collider is added behaves.

Unity Collider Types

Static collider

Static colliders are considered to be non-moving objects by unity. Do not confuse static gameobject with static collider. If a rigidbody is not attached to a collider then it’s a static collider. They do not move when an object Collides to with them. Though, they can be moved using transform, moving a static collider leads to performance loss during runtime.

Rigidbody collider

A rigidbody collider works like a real-world object. It is governed by the forces of physics. If you apply force on it, it will move. In Unity, rigidbody is used on an object which will move a lot during gameplay. Check the screenshot for how a rigidbody is attached to a game object.

Kinematic rigidbody collider

A kinematic rigidbody is a rigidbody that behaves like a static object. So, the next question is then why to use a kinematic rigidbody. The main reason to use a kinematic rigidbody is to tell Unity that the object does not respond to forces but will be movable by script during runtime. The performance loss is very less in moving a kinematic rigidbody compared to a Static object.

Unity Oncollisionenter Trigger

OnCollisionEnter vs OnTriggerEnter

Unity onCollisionEnter

Unity will detect a collision only if one of the objects is a rigidbody. That is, if you have two gameobject marked as static colliders then collision between them will not be detected. It’s the same case with two kinematic rigidbody. If you use the OnCollisionEnter function in any of the above cases, the function will not be called during a Collision. Find the collision matrix for unity colliders in the image below.

Ref:unity docs

Unity OnTriggerEnter

When you mark a collider as a trigger, it no longer behaves like a solid object. It allows the other object to pass through it. But the time of one object entering another object’s space can trigger a function. With that, you can know if a collision has happened without actually creating a collision. You have to use the OnTriggerEnter function if the collider is Marked as a trigger. The collision matrix is a little different in the case of triggers.

Trigger collision doesn’t work if both the colliders are static colliders. In all the other cases the on trigger enter is called.Knowing these basic things about colliders will help you set them up easily in your game.

If you did not understand the matrix completely then here is a simple comparison to help you. Consider static colliders as object that don’t move like wall. Rigidbody collider denotes a moving object like a ball. Kinematic Rigidbody denotes that the object is not moved by physics but can be moved by script or animation.

Unity does not want to detect collision between two static objects so normal collision is detected only if one of the game object is a movable by physics. Where as trigger function works a little different. It works only if one of the collider is marked as trigger and can be moved either by physics or script.

Sample Unity collision script

unity OnCollisionEnter

In this script we are using a normal collision. “col” returns the collider of the game object. We further check if the game object is an enemy. If yes, we destroy it. Remember either the parent gameobject or the enemy game object needs to have a Rigidbody for this script to work.

Unity Oncollisionenter Not Firing

Unity OnTriggerEnter

We can do the same thing with trigger with this code.

Pro tip: If the object is marked as trigger, it will pass through the obstacle and no collision physics will act.

Other features in Unity OnCollisionEnter and OntriggerEnter

Both the examples of Unity OnCollisionEnter and OnTriggerEnter above shows that you can get the gameobject of the colliding object. Apart from that you can get a lot of data from the collision class object. Here is the list of things that you can get from the collision class

Unity on collision enter not working
  1. collider- get the collider of the colliding object
  2. relative velocity- get the relative velocity between the colliding objects.
  3. transform- get the transform of the object hit.

They both look similar and behave similarly, what's then the difference between OnTriggerEnter and OnCollisionEnter? The key to understand this is in knowing what are triggers in Unity.

In Unity, an object might be controlled by the physics engine or by script. If you need to control an object by script but still would like to know if an object touched another, a 'collision' happened, you need to use triggers.

Unity Oncollisionenter Trigger

Unity

Collision is under quotes because, strictly under Unity's terminology, a collision only happens when object's movements are governed by the physics engine, for the other cases what we have are simply objects touching each other, also, for such event, our script can be alerted as well.

What are triggers

Unity On Collision Enter

A trigger is a collider that's not influenced by the physics engine. It doesn't respond to forces nor gravity. But they still do have a use for the physics engine, they are used to detect whether an object passed through another. Triggers are everywhere in Unity game development, and in other engines too to be honest.

This grim repear has all its colliders as triggers

This Reaper is controlled by a simple back-and-forth walk AI, the physics engine is not used, but we still want to know when it has touched some things in the stage. For that, we can use an OnTriggerEnter

Collisions

A collision is also the result of an object touching another one, but instead of passing through, these objects push each other in a realistic way. Use OnCollisionEnter when your rigidbody colliders aren't triggers and you'd like to know when they touched each other.

On this prototype we can use OnCollisionEnter to start calculating the score

For more information on how to create and use triggers, please see the official documentation on the subject. If you followed the documentation and something with your collision detection is not working, you may try to fix it using our comprehensive collision fixing tutorial.

TLDR;

Use triggers if you don't want/need the physics engine to control your object but still need to know if an object passed through another or reached some `zone` within the game. In that case, you'll use OnTriggerEnter().

If your object is indeed controlled by the physics engine, you'll use OnCollisionEnter() to know if an object touched another one.