Main

{ How to DETECT OBJECTS in a certain area in Unity } OnTriggerEnter/OnTriggerExit Example Prototype

In this video we are going to analyze a prototype I made to detect an object enter the Collider of another one and activate or deactivate GameObjects and specific components assigned to some GameObject, that is to say we see how to detect collisions between objects and execute code instructions at that moment. 🎁 DOWNLOAD THE UNITY PACKAGE HERE: https://gamedevtraum.com/en/game-and-app-development-with-unity/unity-tutorials-and-solutions/objects-detection-and-trigger-events-in-unity/ PART 2 of this video: https://youtu.be/iOnDcO8Xrww ____________________________________________________________ *LINKS* *Portfolio:* https://gamedevtraum.com/en/portfolio-en/ *LinkedIn:* https://www.linkedin.com/in/gamedevtraum/ *Downloads:* https://gamedevtraum.itch.io/ *Contact:* https://www.instagram.com/gamedevtraum/ ____________________________________________________________ #EN

The gamedev Traum

3 years ago

Let's analyze this prototype I made based  on the two videos on how to activate and deactivate GameObjects and specific components of  the GameObjects. Here in the corner I leave you a link to my website where you can download a Unity  package with all these assets so you can import it and see how it works in your own computer. The prototype works in the following way, here we have an object that we can control and  on the other hand we have a detection object, when the first object enters this 
area some actions are performed. I'm going to split the explanation  in two videos, in this first part we're going to see how the prototype works here  in the Unity scene, components, GameObjects, I'm going to show you what you need to consider to  make sure that an object can detect another one. And in the second part we are going to  analyze the scripts and instructions. So, Let's start analyzing the hierarchy, we have  two disabled objects, one is an enemy, that is simply a red sphere that h
as a RigidBody Component  assigned to it, when we are in the play mode and activate this object, the sphere appears and it  falls to the ground because of the rigidbody, we also have this object that has an AudioSource  component assigned to it with an audio clip, here we see that the play on awake box is  checked, meaning that the sound will be played when the object is activated. With this we are going to practice enabling specific components from the  GameObjects. Finally we have the sign tha
t is enabled when the character enters  the collider and disabled when it leaves. Let's see what is needed to make  an object detect another one, here I have a GameObject called trigger object,  it has a mesh renderer and a transparent material assigned to see its boundaries. The object  could be invisible and still be able to detect, the important thing here is that the detection  element has a collider in Trigger mode, here the isTrigger box needs to be checked. This object is going to detect
other objects that also have a Collider assigned, notice that  if I deactivate the player's Collider, enter the play mode, this object cannot detect it. And the  third thing we need is that at least one of the two objects has a RigidBody component assigned. So those are the conditions for an object to detect another one, a Collider in trigger  mode, the object we want to detect must also have a Collider and at least one of  the two must have a RigidBody component. Now let's talk a little about t
he scripts  but without getting into the code, this object that we are using as detection is  going to execute a series of functions on the script that it has assigned, we are talking  about the On trigger and On collision events, here in this case in the detection object  we have this script that has those functions inside and notice the components assigned in  the script, the enemy, the sign and the audio source, so this is going to be the script that  will activate the GameObjects and the com
ponents. Something important to consider is that when the  detection takes place, not only the OnTrigger events of the script assigned to the trigger  object will be executed, but also the OnTrigger events of a script that could be assigned in the  object that enters in the collider, in this case we have the player object that has this script,  player control, if we define the on trigger functions within the player control script, these  functions will also be executed and to prove that I print
console messages in both scripts. Notice, when the object enters there are two messages printed, one says "I am the trigger  someone has entered" and the other message says "I am the player I have entered the trigger". So  the OnTrigger event is executed in both scripts. But there is more, if we take this component, copy  and paste it, imagine that you have two different scripts, both with the OnTrigger functions  defined, when we enter the play mode and the player enters the trigger, this messa
ge appears  repeated twice, indicating that the trigger is executing the On trigger events in both scripts. To finish, the player GameObject has the Player tag assigned, we can use this to know if the  object that enters the Collider is the player or is some other object and apply  different actions accordingly. That would be all for this video, in the next  one we will see the scripts and instructions

Comments

@GameDevTraum

I have another channel, please SUBSCRIBE here 😄👉 https://www.youtube.com/@GameDevTraumDE LEARN more UNITY here 👉 https://www.youtube.com/watch?v=E8-k9_DdkBg&list=PLkYLdQEQ__ti94UUicgYPWPy3VEypFj98&index=1

@ziyadcodes

just make the scripts in the same vedio

@kayzoku1

why would you not show the script lmao