Hey, I’m walking here! — A quick guide to collisions in Unity

Unity has two methods of collision interactions built in. Collisions and Triggers.

Think of Collisions as the physical touch and interaction of one object with another such as a car crash or a bullet hitting an enemy.

A collision occurs.

Triggers are a softer version of physics where we see if two objects have touched without actually having those objects bounce off each other. You see this when a player walks into a portal to teleport and when you pick up a power-up in a racing game.

A more discrete collision occurs.

How do we know when we are interacting with another object?
Collision methods are automatically called in Unity. Just because an object has a collider component does not mean it is set up to work with physics and collisions in Unity. In order to register an interaction (collision) at least one of the two objects interacting require a rigid body component.

If we want our objects to collide and bounce off each other, registering the hard collisions, we do not need to make changes to the collider component.

If we wish for our objects to pass through each other, then we need to set their attached collider to “Is Trigger”. this is what we will do in order to register a laser hitting an enemy in the following example.

What interactions can we react to?

We have a few options when checking for interactions. “Enter”, “Exit” and “Stay” which can be used in conjunction with the method “OnTrigger” and “OnCollision”. For example “OnTriggerStay()”;

Enter: Called on the first frame when two or more game objects collide with each other. A typical example of when we use this would be when a bullet hits an enemy.

Exit: Called on the frame when the game objects stop touching each other. An example of this could be when a player steps off a pressure plate on a floor.

Stay: Called on each frame the two objects are touching. An example of this could be a mech standing in a charging pod.

How do we know what we collided with?

Each method returns the object that the collision occurred and logic can be run on this data.

As we can see “OnTriggerEnter” has an output of a collision, which gets assigned to the variable “other”.

We can access the object in the collision information via this “other” variable. In this case, we will just output the name of that object to the console.

Now we can access collisions we can run logic for interactions for a laser to cause damage or a player to pick up powerups which we will do in a future article.

That’s all for now.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Luke Duckett

Luke Duckett

Unity developer with a love of learning all things programming.