Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
0
1
00:00:00,030 --> 00:00:02,760
Welcome to the sensor implementation.
1
2
00:00:03,000 --> 00:00:08,460
And the first sensor we are going to look into is the range sensor.
2
3
00:00:08,490 --> 00:00:16,170
First, let's give this scene a quick test run so you can see what we are going to achieve in this module.
3
4
00:00:16,260 --> 00:00:21,960
We have the enemy in the middle of the scene that has a range specified by this wire sphere.
4
5
00:00:22,140 --> 00:00:29,580
If my player, which I can move with WASD or the arrow keys goes into the sphere, then it is detected,
5
6
00:00:29,580 --> 00:00:30,510
as you can see here.
6
7
00:00:30,510 --> 00:00:32,850
And if it's out then it's false.
7
8
00:00:33,600 --> 00:00:35,430
This is a quite simple sensor.
8
9
00:00:35,910 --> 00:00:42,180
It's not useful in cases where, for example, when you want field of view, but in some other cases,
9
10
00:00:42,180 --> 00:00:47,850
for example, turrets or things that have 360 view, it works great.
10
11
00:00:48,300 --> 00:00:51,750
Plus it can work with other sensors together.
11
12
00:00:51,750 --> 00:00:54,300
So let's see how this is made.
12
13
00:00:54,360 --> 00:00:54,840
Okay.
13
14
00:00:55,110 --> 00:00:56,730
Now here we have the scene.
14
15
00:00:56,970 --> 00:01:02,730
And as you can see, there are a couple of different components here that I want to discuss before diving
15
16
00:01:02,730 --> 00:01:03,120
deep.
16
17
00:01:03,120 --> 00:01:10,290
So we have the AI in the middle, which is basically the AI instance that has the sensor.
17
18
00:01:10,710 --> 00:01:13,110
We have the terrain, which is basically just the cube.
18
19
00:01:13,560 --> 00:01:18,840
We have the target, the camera that we can see through while the scene is playing and the control,
19
20
00:01:18,840 --> 00:01:21,990
which is basically just some UI elements.
20
21
00:01:22,410 --> 00:01:26,340
Let's quickly look at the target because this is the player.
21
22
00:01:26,340 --> 00:01:28,740
So here there is a kinematic body.
22
23
00:01:28,770 --> 00:01:33,330
It's important to be on layer one because the detection will be on layer one.
23
24
00:01:33,420 --> 00:01:36,990
It has a mesh instance, which is a capsule and a collision shape.
24
25
00:01:36,990 --> 00:01:40,230
But don't forget, it's really important to have the collision shape.
25
26
00:01:40,230 --> 00:01:45,630
In my case, it's a capsule shape, but any shape works. For the target script if we look into it right
26
27
00:01:45,630 --> 00:01:51,660
now because it's quite easy, it just sets up the type of the enemy using the meta functionality and
27
28
00:01:52,020 --> 00:01:52,740
the collision.
28
29
00:01:52,740 --> 00:01:58,800
This is not relevant, but it's used to detect the body which has the collision.
29
30
00:01:58,830 --> 00:02:04,770
If we go back to the scene, we also have the range sensor test that has a script attached to it.
30
31
00:02:04,770 --> 00:02:07,890
And this is really important because this is the debug script.
31
32
00:02:07,890 --> 00:02:12,480
What this debug script does is of course initializes the range detector.
32
33
00:02:12,780 --> 00:02:14,700
By the way, here is the radius.
33
34
00:02:14,700 --> 00:02:17,760
So if you increase this, then the sphere will get bigger
34
35
00:02:17,760 --> 00:02:20,910
(the detection sphere). First part says if it's enabled or not.
35
36
00:02:20,910 --> 00:02:23,250
And lastly, it gives the AI (reference).
36
37
00:02:23,310 --> 00:02:30,330
In this particular example, this is not so useful, but in the real world example, we want to avoid
37
38
00:02:30,330 --> 00:02:34,950
detecting itself, so we don't want the AI to be able to detect itself.
38
39
00:02:35,370 --> 00:02:43,230
So this will actually work to remove the the AI from being detected. In the process function which happens
39
40
00:02:43,230 --> 00:02:50,400
every single frame we do have move up and down and left and right which move the target up, down,
40
41
00:02:50,400 --> 00:02:51,240
left and right.
41
42
00:02:52,050 --> 00:02:59,850
Also, the range detector provides a target list which we query all the time, every frame to see if
42
43
00:02:59,850 --> 00:03:01,650
its size increased from zero.
43
44
00:03:01,650 --> 00:03:08,880
And if it does, then this statement will return true and will appear on our UI that is_detected is
44
45
00:03:08,880 --> 00:03:10,980
true, otherwise it will be false.
45
46
00:03:11,010 --> 00:03:14,070
Now let's take a look at the AI and
46
47
00:03:14,070 --> 00:03:15,360
what does it have.
47
48
00:03:15,360 --> 00:03:19,920
Basically, the AI is just a node with a mesh instance, which is a capsule, as you can see.
48
49
00:03:19,920 --> 00:03:22,890
But the important part here is the range detector.
49
50
00:03:22,890 --> 00:03:28,620
Currently, there is no physical part that can interact with the range detector from the AI.
50
51
00:03:28,620 --> 00:03:32,820
But in a real case scenario, there will be. Let's look at the range detector.
51
52
00:03:32,820 --> 00:03:38,640
By the way, I created a special scene for this to make it useable as possible.
52
53
00:03:38,640 --> 00:03:42,120
So if I click open an editor, it will open the new scene.
53
54
00:03:42,120 --> 00:03:45,870
And here there are a couple of things really important.
54
55
00:03:45,870 --> 00:03:52,260
First, we have the area collision and it's really important to create this area collision because this will
55
56
00:03:52,290 --> 00:03:56,550
determine the collision between this object and what comes into it.
56
57
00:03:56,550 --> 00:04:03,600
Please make sure that monitoring is set to on. Monitorable means that it can be queried by other things.
57
58
00:04:03,600 --> 00:04:08,610
But since we are the ones that are monitoring, we can safely put this to off.
58
59
00:04:08,610 --> 00:04:13,080
We need to put the mask on one because the layer of the player is on one.
59
60
00:04:13,080 --> 00:04:14,880
So we need to make sure this is on one.
60
61
00:04:14,880 --> 00:04:17,610
The collision shape for this is a sphere.
61
62
00:04:17,610 --> 00:04:22,650
So this is put as a sphere, but this will not matter because you'll change it through code because
62
63
00:04:22,650 --> 00:04:24,300
you want it to be dynamic.
63
64
00:04:24,330 --> 00:04:26,250
There is also an immediate geometry.
64
65
00:04:26,250 --> 00:04:28,290
This is for debug purposes.
65
66
00:04:28,560 --> 00:04:32,490
But what I can tell you about this is just the simple immediate geometry node.
66
67
00:04:32,550 --> 00:04:36,780
It has a material of yellow without light, so it's unshaded.
67
68
00:04:36,780 --> 00:04:43,050
And also here it's quite simple code, but basically what it does, it just creates a sphere out of
68
69
00:04:43,050 --> 00:04:44,460
primitive lines.
69
70
00:04:44,460 --> 00:04:51,780
Using the radius provided by the collision sphere in the range detector. It's nothing fancy, just for visual
70
71
00:04:51,810 --> 00:04:53,070
debugging reference.
71
72
00:04:53,070 --> 00:04:57,480
Now moving on to the main part of this is the range detector script.
72
73
00:04:57,480 --> 00:04:59,760
And here's the full script we're going to look.
73
74
00:04:59,840 --> 00:05:03,020
Into each part and see how it works.
74
75
00:05:03,110 --> 00:05:06,540
Basically, starting from the top, we have the extends node.
75
76
00:05:06,560 --> 00:05:09,420
It can also extend the spatial, since it's a spatial, this one.
76
77
00:05:09,440 --> 00:05:15,980
It provides two different signals which in some cases you might use, in some cases you might not.
77
78
00:05:16,340 --> 00:05:24,860
But on the target enters this range detector, it will signal that fact and also send what target actually
78
79
00:05:24,860 --> 00:05:27,710
entered or what target exited. Next
79
80
00:05:27,710 --> 00:05:35,120
it will have a target list continuously updated with all the targets that are monitored in that range.
80
81
00:05:35,240 --> 00:05:41,270
The collision shape is the sphere shape mentioned here and this will be used to make the correct size
81
82
00:05:41,270 --> 00:05:43,370
when we actually customize it.
82
83
00:05:43,430 --> 00:05:48,770
The parent is the actual component that we want to take out from the potential target list.
83
84
00:05:48,770 --> 00:05:54,380
For example, if the AI detects itself, we don't want it to be in that list because it would be redundant.
84
85
00:05:54,590 --> 00:05:55,640
Is enabled of course.
85
86
00:05:55,640 --> 00:06:01,340
Is self-explanatory true if it is enabled false if it's not. Here we have a couple of different functions.
86
87
00:06:01,880 --> 00:06:09,620
Two of them are custom and two are signals from the area collision. Starting with the initialize it says
87
88
00:06:09,620 --> 00:06:11,270
is enabled, custom range.
88
89
00:06:11,270 --> 00:06:16,700
This is what actually gives the range of the whole sphere and the parent the collision shape as I mentioned
89
90
00:06:16,700 --> 00:06:22,130
before, why this is not important, this sphere shape, because we are going to create the new sphere
90
91
00:06:22,130 --> 00:06:28,640
shape and we are going to set the radius of the sphere shape to the custom range that we provided. Next,
91
92
00:06:28,640 --> 00:06:34,820
of course, we need to initialize is_enabled and parent from these two values. And for the collision shape,
92
93
00:06:34,820 --> 00:06:39,290
we need to of course set it correctly with the one that we currently created.
93
94
00:06:39,290 --> 00:06:45,410
And if is enabled, of course the immediate geometry gets the collision sphere as well.
94
95
00:06:45,500 --> 00:06:52,160
The main part of this is the function called is target in range, which will give true or false.
95
96
00:06:52,280 --> 00:06:58,190
But as you can see, there is no query to physics, but more a query to if the target is in the target
96
97
00:06:58,190 --> 00:06:58,550
list.
97
98
00:06:58,760 --> 00:07:05,000
And this is because the target list will automatically get updated with things that come in or out of
98
99
00:07:05,000 --> 00:07:05,210
it.
99
100
00:07:05,210 --> 00:07:07,760
And this happens because of the next two things.
100
101
00:07:07,760 --> 00:07:09,350
So how we can hook up the signals?
101
102
00:07:09,350 --> 00:07:14,870
Well, if we go to the area and we go to the node and on body_entered.
102
103
00:07:14,870 --> 00:07:21,140
So it's body not area because this one is the area, the player kinematic body is a body actually in
103
104
00:07:21,140 --> 00:07:21,470
here.
104
105
00:07:21,470 --> 00:07:22,460
Just say connect.
105
106
00:07:22,940 --> 00:07:28,430
Of course we need to put the range detector because this is the script where it has the connection to.
106
107
00:07:29,440 --> 00:07:32,380
And when you go back, we repeat the process for both of them.
107
108
00:07:32,590 --> 00:07:37,060
When you go back, we end up with these two functions and as you can see here, they are connected.
108
109
00:07:37,090 --> 00:07:43,360
What happens here is that the area detects a target that entered and then the target gets processed
109
110
00:07:43,360 --> 00:07:44,230
into an enemy.
110
111
00:07:44,590 --> 00:07:51,670
Here I have a singleton called globals which has get enemy from collider. Will quickly go into that
111
112
00:07:51,670 --> 00:07:52,420
in a second.
112
113
00:07:53,050 --> 00:07:58,780
But before that, if the enemy is not null, that means that it was processed correctly and the target
113
114
00:07:58,780 --> 00:08:00,640
list does not contain the enemy.
114
115
00:08:00,670 --> 00:08:07,150
This means basically that the enemy is legit and it can be added to the target list since it's a new
115
116
00:08:07,150 --> 00:08:08,300
enemy detected.
116
117
00:08:08,320 --> 00:08:10,630
First, of course, we need to prevent the self collision.
117
118
00:08:10,780 --> 00:08:15,580
What I said to the parent, we don't want the enemy to be itself.
118
119
00:08:15,580 --> 00:08:21,700
And then we use append for the target list, which is basically a way to add new elements to the list.
119
120
00:08:21,700 --> 00:08:23,890
And then we emit the signal.
120
121
00:08:23,890 --> 00:08:28,480
We send the event that, hey, this target entered the range. In the case,
121
122
00:08:28,510 --> 00:08:33,700
for some reason this happens and the enemy detected was already in the range.
122
123
00:08:34,060 --> 00:08:38,650
Then it won't be counted as a new enemy getting inside this list.
123
124
00:08:38,680 --> 00:08:41,040
The same happens for the target exit.
124
125
00:08:41,050 --> 00:08:44,380
So basically it still determines if the enemy is valid.
125
126
00:08:44,380 --> 00:08:50,650
And then if enemy was actually in the target list and if it was, then it needs to be erased.
126
127
00:08:50,650 --> 00:08:55,690
And then another event is released saying that the target is out of range.
127
128
00:08:55,720 --> 00:08:58,980
Now let's quickly look at get the enemy from collider.
128
129
00:08:58,990 --> 00:09:01,510
So here is basically a physics collider.
129
130
00:09:01,660 --> 00:09:02,800
So in this case.
130
131
00:09:03,940 --> 00:09:08,160
What we're going to receive is probably this kinematic body.
131
132
00:09:08,170 --> 00:09:09,250
So keep this in mind.
132
133
00:09:09,640 --> 00:09:15,790
And the kinematic body, as you remember, we have this small script that has a meta tag called type
133
134
00:09:16,120 --> 00:09:17,290
with the value enemy.
134
135
00:09:17,380 --> 00:09:23,890
And this is really important in what's to come, because if we go here with control, click basically
135
136
00:09:24,340 --> 00:09:30,910
it gets the enemy from the collider is just a value and then it checks if it has the tag and that
136
137
00:09:30,910 --> 00:09:37,480
means that it is detected and if it's not detected, then it will return null because there is no enemy
137
138
00:09:37,480 --> 00:09:38,350
to detect.
138
139
00:09:38,350 --> 00:09:45,970
But then, of course, we need to process the tag and to see if it's actually an enemy or a player and both
139
140
00:09:45,970 --> 00:09:47,530
of them are labeled as enemy.
140
141
00:09:47,770 --> 00:09:55,240
This actually happens because the plan is: for the enemy to target both the player, but also enemy
141
142
00:09:55,260 --> 00:09:57,100
AI from different teams.
142
143
00:09:57,100 --> 00:10:00,310
So this has to happen in order to do this.
143
144
00:10:00,310 --> 00:10:07,270
If the tag is not enemy and not player, it might be a third party type, like for example, obstacle
144
145
00:10:07,270 --> 00:10:08,080
or something else.
145
146
00:10:08,380 --> 00:10:11,800
And we don't really need to count it as a target.
146
147
00:10:11,800 --> 00:10:15,040
So for this we return null because we don't need it.
147
148
00:10:15,640 --> 00:10:20,710
Now, if we go back to the range scene and test it again, this is the result of this.
148
149
00:10:20,710 --> 00:10:24,610
And if we quickly check the target list.
149
150
00:10:25,870 --> 00:10:27,430
So I'm going to take the target list.
150
151
00:10:27,430 --> 00:10:29,950
I'm going to print the target list.
151
152
00:10:31,600 --> 00:10:34,480
And as you can see, the target list is the kinematic body.
152
153
00:10:34,480 --> 00:10:38,830
But if I step away from the range, the target list will be empty.
153
154
00:10:39,130 --> 00:10:40,990
Go in the kinematic body.
154
155
00:10:40,990 --> 00:10:44,410
Out, empty, in, out and out.
155
156
00:10:44,800 --> 00:10:45,550
Very simple.
156
157
00:10:45,560 --> 00:10:48,340
This is basically the range sensor.
157
158
00:10:48,640 --> 00:10:52,690
Now let's move over to the field of view sensor.
16723
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.