Create A Detection System In Unity
Let’s understand how to create a detection system in Unity (Dot Product Detection Solution)
Taking that into account, we’ve discussed the process of creating and implementing a simple detection system in Unity over here. So, if you’re new to Unity and are looking to start with something easy yet mentally stimulating at the same time, then here’s your cue! We’ve provided detailed coverage of the detection system in the following segments, as demonstrated in the video.
Video tutorial: https://www.youtube.com/watch?v=MB7d3MdVHwU
Detection System- Understanding The Scope
As discussed in this video, the scope of the detection system is fairly straightforward- Consider two entities, a player and an enemy. Imagine a small, sweeping area around the enemy object in which it is sensitive to the approach of the player object. We’ll call this area the detection range of the enemy object.
Now, the purpose of this detection system is to display a message on the screen every time the player object enters the detection range of the enemy object.
The detection system will check for the violation of the said detection range both in terms of distance and angle.
To download the starting project of this detection system, go to the link provided in the video's description.
To understand how to open the file in Unity, refer to [0:33] of the video. Should the project fail to open in this manner for you, you can also access it via assets. To understand how to do that, navigate to [1:25] of the video.
Creating And Calling The Player Method
To get started, we first need to create a “look for player” method within the script of the enemy object. Before we do that, let us understand a few basic concepts concerning methods or functions in Unity -
- A method is a block of code that a programmer can create and then call on at a subsequent place in the program.
- The syntax of a method in Unity(C#) is:
<return type> <name of method> (<arguments>)
// code block
The process of defining a “look for player” method for this detection system has been demonstrated in [3:20] of the video.
Understanding The Positions Of The Player And The Enemy
It is important for us to first understand the relative position of the player object concerning the enemy object before defining our “look for player” method. We need to visualize both the player and the enemy objects in a 3D vector space to achieve this.
To calculate the Euclidean distance between the enemy object and the player object, we need to subtract the coordinates of the enemy object from that of the player object. This exercise has been elaborated upon in [5:19] of the video.
To obtain greater clarity, carry out this visualization either on a drawing tool or manually on a sheet of paper. Alternatively, to achieve better visualization within Unity itself, make use of the top to bottom view.
Checking For The Vector Magnitude
For every instance that we wish to determine the distance between the player object and the enemy object, we must compute the magnitude of the difference of their respective vectors. For this detection system, the vector representing how to get to the player object from the enemy object is denoted by “toPlayer” in the script. This is discussed in [8:07] of the video.
If the magnitude of this “toPlayer” vector happens to be lower than or equal to the stipulated detection radius, then our detection system will get triggered and subsequently display the message on our screen. In this video, the detection radius has been set as 10 units. The working of the detection system has been demonstrated in [9:32] of the video.
Checking For The Angle
Our detection system can now alert us whenever the player object is entering into the pre-defined detection radius of the enemy object. And thus, we’ve accomplished the objective we set out to achieve in this video. Well, not quite so.
While there’s no doubt regarding the working of our detection system at present, there’s a certain catch to it- it only detects the range of the player object and not its angle.
For our detection system to be completely accurate, we need it to scan the entire semi-circular or circular portion around the enemy object and not just check for the presence of the player object in a single direction. You can achieve this by defining a simple statement in your script. To understand it, refer to [10:58] of the video.
With this implementation, your detection system is now complete.
If this simple detection system piqued your curiosity, then you should consider opting for The Complete Unity Guide 3D- Beginner to RPG Game Dev in C# offered by Eincode. This course features among the most immersive and practical resources out there.
Curated by experienced software engineer and freelance developer Filip Jerga, this course starts with the fundamentals. Then, it progresses gradually to eventually take its subscribers through the journey of developing their own RPG game by using Unity 2020 and C#.