In this tutorial, I going to show you the trigger system in unity. Used frequently in games. we will start from basic afterward we going to look at complex trigger systems.
What Is Trigger System?
The trigger system is basically, can be thought of as the system that monitors the condition required for an event to occur. For example, we have an object and we want to run a command line when that object touches a trigger or exits for this we can use a trigger.
Trigger syntax is so simple but The preparation process of the object can be a bit complicated. Now we going to just look at trigger syntax after that we going to use trigger syntax in the project.
Trigger Example Script
Trigger syntax is so basic, so It is very easy to use. Now, we going to create a trigger system after that we going to examine these functions.
How To Work Trigger System?
Basically, there are 3 types of triggers these OnTriggerEnter, OnTriggerExit, OnTriggerStay.
OnTriggerEnter -> The trigger is triggered as soon as the object hits the trigger.
OnTriggerExit -> Triggered when the object leaves the trigger.
OnTriggerStay -> The trigger is triggered as long as the object remains in the trigger.
Working Multiple Trigger System
Sometimes, we need to use all trigger systems so, let’s create a new script with all triggers. After that, we going to learn how to create a trigger object.
How To Create Trigger Object
We need to trigger an object to use the trigger system so let’s create a new trigger object.
You should add a rigid body to the game object after that enable the “is trigger” feature of the collider.
Creating A Sample Attack System With Trigger System
Now, we know about the trigger system so we going to create a simple attack system in unity. Create a trigger object and a normal object after that, we going to ready to writing the script.
the script is ready, now, you should add a player, after adding, you should select tag name but before let’s talk about tags.
What is Tag?
Tags are short texts used to introduce an object. For example, unity has 6 tags, if you want to create a new tag, firstly, click on an object and open the tag menu from the top after that, just click for add tag from the bottom of the menu. You can create a new tag from the Tags & Layers menu.