Prototype for DETECTING OBJECTS entering a COLLIDER in Unity

Introduction

I made a simple prototype for Unity to show how to detect when an object enters inside the collider of another object and execute functions in that case.

Scene of the prototype that comes in the Unity Package

Download Unity Package

Below you can download the Unity package that when imported into your engine, you will recover the Assets we use and you will be able to test it on your own computer.

Please read carefully what I say below about the functioning of this prototype

I have created this prototype for you to understand how an action can be executed when we detect that a Collider enters inside another Collider in Unity.

THE EXPLANATION IS DIVIDED IN TWO PARTS, I did it this way because this is how I solve any problem using the Unity engine, after analyzing a problem and creating a plan to solve it, you have to take care of two things, the first one is the scene elements in Unity, that is to say create GameObjects, assign components and configure. The other part to take care of is the code instructions that solve the problem, but this is not possible if we don’t know which are the objects of the scene that we are going to use in our scripts.

Video #1: Configure Colliders as triggers in Unity

To begin with, we are going to analyze how things have to be configured in Unity to be able to detect that an object enters inside another object. It is necessary to correctly configure the Colliders of the objects and add Rigidbody components so that a Collider is able to work as a Trigger

IF YOU DON’T GET THIS PART RIGHT, NO MATTER HOW WELL YOU WRITE THE CODE, IT JUST WON’T WORK.

Pay close attention to the following video:


Video #2: The OnTriggerEnter function in Unity

Once we made sure that we have correctly configured the objects in the scene, now we move on to the code part, in the following video you can see the OnTriggerEnter function that is executed when a Collider is detected by a Trigger, this concept can be used for many different things, in this case, when the character enters the trigger an object is turned on, a sound is played and an enemy appears in the scene:



Implementation

The main goal of this video is to help you understand how Unity’s collider detection system works, this will allow you to take better advantage of it according to your needs.

Most of the actions that occur in this prototype consist simply in activating and deactivating GameObjects of the scene and also components, for example the “enemy” that falls from the sky when we pass through a certain area is simply a GameObject that was inactive and we activate it at the moment we detect that the player enters the collider.

Here are two videos that explain how to activate and deactivate GameObjects from the scene and how to activate and deactivate components attached to GameObjects from the scene.

ABOUT THESE VIDEOS

Below you have two videos about the actions applied inside the prototype for the detection of an object that enters inside a Collider. In the first video we see how to ACTIVATE and DEACTIVATE a GameObject through code. In the second video we see how to ACTIVATE and DEACTIVATE components assigned to GameObjects through code.

Scroll to Top
Secured By miniOrange