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:01,700
Welcome to the raycast part.
1
2
00:00:01,710 --> 00:00:06,230
In this session we are going to look how the raycast sensor is implemented.
2
3
00:00:06,250 --> 00:00:11,960
If you remember, we went through the range sensor which tested the range around the AI.
3
4
00:00:12,030 --> 00:00:16,140
Then we looked at the field of view which looked in front of the AI.
4
5
00:00:16,260 --> 00:00:23,490
But this case is when the field of view fails, because if the player is in front of the AI agent but it's
5
6
00:00:23,490 --> 00:00:30,570
occluded by a cube, then the enemy will still be able to detect it. To prevent the enemies hitting you
6
7
00:00:30,570 --> 00:00:30,860
.
7
8
00:00:30,860 --> 00:00:32,040
through the walls.
8
9
00:00:32,610 --> 00:00:34,650
We need to use raycasts.
9
10
00:00:34,650 --> 00:00:39,450
And raycasts are actually lines that are more physically based.
10
11
00:00:39,450 --> 00:00:41,130
They are not visible in this case.
11
12
00:00:41,130 --> 00:00:44,670
I'm just using a debug line for illustration purposes.
12
13
00:00:44,670 --> 00:00:52,320
But rays are invisible lines that are casted from a point towards a direction and then whatever they
13
14
00:00:52,320 --> 00:00:55,680
intersect in their path, it returns that.
14
15
00:00:55,680 --> 00:00:58,020
So in this case it will be this cube first.
15
16
00:00:58,140 --> 00:01:03,690
So the detected is false, but then if we move, the detected becomes true.
16
17
00:01:03,900 --> 00:01:10,830
And then of course I'm trying here of different obstacles and this is the result that I'm getting.
17
18
00:01:12,250 --> 00:01:14,280
So basically this is what the raycast does.
18
19
00:01:14,290 --> 00:01:19,060
Let's see now how this is implemented and how can you use it in your own game.
19
20
00:01:19,270 --> 00:01:25,420
The main structure is very, very similar to the other range scene and field of view scene with the
20
21
00:01:25,420 --> 00:01:30,250
main difference of course being that this time we have the raycast detector.
21
22
00:01:30,880 --> 00:01:33,340
So this is probably where we need to go check.
22
23
00:01:33,340 --> 00:01:38,440
But I also want to mention that I put some obstacles and the obstacles here.
23
24
00:01:38,830 --> 00:01:41,410
They do have a static body with a collision shape.
24
25
00:01:42,160 --> 00:01:44,260
I opted to put them on layer ten.
25
26
00:01:44,260 --> 00:01:49,660
So we need to make sure that the raycast works for layer one as well as layer ten.
26
27
00:01:49,750 --> 00:01:52,540
Otherwise we don't detect these collisions.
27
28
00:01:53,020 --> 00:01:55,810
But let's move on into the raycast detector.
28
29
00:01:56,020 --> 00:02:00,400
So I created another scene for this because it's much easier to have it modular.
29
30
00:02:01,540 --> 00:02:04,300
And let's look now, what does it have?
30
31
00:02:04,510 --> 00:02:10,120
So I'm using a DebugHit, which is actually just the simple sphere.
31
32
00:02:10,720 --> 00:02:15,430
This is used to show the exact point where the ray hits the object.
32
33
00:02:16,730 --> 00:02:18,470
The raycast, which is actually
33
34
00:02:19,620 --> 00:02:26,190
a node that you can make by create a new note and then raycast.
34
35
00:02:26,340 --> 00:02:27,810
This will give you this object.
35
36
00:02:28,710 --> 00:02:29,370
This object
36
37
00:02:29,370 --> 00:02:36,330
you can actually specify what layers does it collide with. And you can give it a position by moving the
37
38
00:02:36,330 --> 00:02:37,080
object around.
38
39
00:02:37,380 --> 00:02:39,540
And you also can give a direction here.
39
40
00:02:40,050 --> 00:02:41,460
So it actually points down.
40
41
00:02:42,440 --> 00:02:49,580
But this will be changed at runtime because we actually need to point this towards the player and then
41
42
00:02:49,580 --> 00:02:51,890
make sure that it actually hits the player.
42
43
00:02:53,200 --> 00:02:58,840
This doesn't come enabled by default, so make sure that you have it enabled, otherwise it won't work
43
44
00:02:58,840 --> 00:02:59,290
at all.
44
45
00:02:59,590 --> 00:03:04,750
And the debug line is just the line that makes it visible for us to see.
45
46
00:03:04,870 --> 00:03:11,980
There is another way to debug physics objects, so we go to debug and put visible collision shapes
46
47
00:03:11,980 --> 00:03:12,670
to true.
47
48
00:03:13,270 --> 00:03:22,030
And if we run this scene, then you'll see that this capsule shape, this ray is actually intersecting
48
49
00:03:22,030 --> 00:03:23,500
the line I'm making.
49
50
00:03:23,500 --> 00:03:28,630
And also these shapes and any other physics shape is basically visible.
50
51
00:03:29,110 --> 00:03:32,440
I'm going to turn this off because I already have the debug line for this.
51
52
00:03:33,220 --> 00:03:39,220
But what's the most interesting part is this script that is attached to the raycast detector and let's
52
53
00:03:39,220 --> 00:03:40,420
see what's inside.
53
54
00:03:41,530 --> 00:03:44,350
And here, of course, we have a couple of things.
54
55
00:03:45,310 --> 00:03:49,060
I opted to use is_enabled and initialize.
55
56
00:03:49,480 --> 00:03:50,740
These are not necessary.
56
57
00:03:50,770 --> 00:03:55,030
I'm using this because they'll be used in a sensor manager later on.
57
58
00:03:55,180 --> 00:03:59,320
But the most important part is what's inside is target visible.
58
59
00:03:59,470 --> 00:04:02,650
And then here you see, is just only one function.
59
60
00:04:02,650 --> 00:04:04,750
But it does a couple of different things.
60
61
00:04:05,230 --> 00:04:11,620
First, it gets the target, which in this case it's the player either detected by the range sensor
61
62
00:04:11,620 --> 00:04:13,030
or the field of your sensor.
62
63
00:04:14,140 --> 00:04:20,140
Then I'm getting the origin point, but I'll be getting that from the collision shape.
63
64
00:04:20,140 --> 00:04:21,210
And the collision shape.
64
65
00:04:21,220 --> 00:04:26,610
It's basically the collision shape of the object instead of the actual mesh of the object.
65
66
00:04:26,620 --> 00:04:32,840
And I opted to do this because the mesh rotates a little differently from the collision object.
66
67
00:04:32,860 --> 00:04:39,130
So I want to make sure that the ray is always pointed towards the center of the collision. With the
67
68
00:04:39,130 --> 00:04:39,850
position
68
69
00:04:39,880 --> 00:04:46,150
we cannot put the cast_to value just yet because we need to get out of this position.
69
70
00:04:46,150 --> 00:04:55,240
The direction and basically the direction is a vector of length one that points from the enemy towards
70
71
00:04:55,240 --> 00:04:56,050
the target.
71
72
00:04:56,950 --> 00:04:58,150
So how can we do this?
72
73
00:04:58,390 --> 00:05:05,500
Of course we need to take the target position and then we can actually get the ray position, which
73
74
00:05:05,500 --> 00:05:08,740
is the exact position as the AI agent.
74
75
00:05:08,740 --> 00:05:13,540
And if we do a vector subtraction, we actually get the vector.
75
76
00:05:13,660 --> 00:05:17,110
But the thing is, the vector is of length in between (distance) them.
76
77
00:05:17,590 --> 00:05:22,180
So to actually set it to length one, we need to normalize it.
77
78
00:05:22,240 --> 00:05:24,130
And this is how we have the direction.
78
79
00:05:24,370 --> 00:05:29,380
Of course, you need to assign this direction to the cast to value for the raycast.
79
80
00:05:29,410 --> 00:05:32,020
Make sure to multiply this a little more.
80
81
00:05:32,020 --> 00:05:40,570
So the direction is actually further because I've noticed that if it's exactly as the player, then
81
82
00:05:40,570 --> 00:05:42,460
it might not detect the collision
82
83
00:05:42,730 --> 00:05:43,140
good.
83
84
00:05:43,150 --> 00:05:45,550
So try to multiply this
84
85
00:05:45,550 --> 00:05:51,010
with a number not so big, not so small, but this will actually increase the chances of the collision
85
86
00:05:51,010 --> 00:05:52,180
being detected properly.
86
87
00:05:52,960 --> 00:05:55,570
And then I'm using this force raycast update.
87
88
00:05:56,110 --> 00:05:59,350
This is actually necessary to update the collision information.
88
89
00:05:59,380 --> 00:06:03,520
This is required instead of waiting for the next physics process call.
89
90
00:06:03,580 --> 00:06:10,390
So make sure to update the raycast before. And then this is just for debug purposes so I won't go into
90
91
00:06:10,420 --> 00:06:16,660
much detail, but the line gets the two points needed to create the ray, which is the origin point
91
92
00:06:16,660 --> 00:06:18,190
of the ray in the final point.
92
93
00:06:19,310 --> 00:06:28,010
And the most important part for us is this IF statement which checks if the raycast is colliding. And
93
94
00:06:28,010 --> 00:06:32,510
if the raycast is colliding, then we need to get what collider is here.
94
95
00:06:32,990 --> 00:06:38,810
So if the collider is equal to the target, that means if it's the player, then it'll return
95
96
00:06:38,810 --> 00:06:39,200
true.
96
97
00:06:39,230 --> 00:06:42,140
If it's not, then it will return false.
97
98
00:06:42,830 --> 00:06:51,200
So basically, if we give the player as the target for this one and the ray doesn't have any obstacles
98
99
00:06:51,410 --> 00:06:55,070
between the AI agent and the player, this will return true.
99
100
00:06:55,760 --> 00:06:57,950
If it finds an obstacle on the way.
100
101
00:06:57,980 --> 00:07:01,940
Like in this case the cube, then it'll return false.
101
102
00:07:02,510 --> 00:07:06,000
And this is how a raycast detector can be implemented.
102
103
00:07:06,020 --> 00:07:09,890
Please make sure to set up the raycast position correctly.
103
104
00:07:10,430 --> 00:07:13,810
So in this case, it can go from the enemy's eyes.
104
105
00:07:13,820 --> 00:07:17,180
But if you don't assign it correctly, it might go from its legs.
105
106
00:07:17,180 --> 00:07:24,330
So make sure that it's from a point where the enemy does have its eyes or the detection functionality.
106
107
00:07:24,350 --> 00:07:25,090
This is it.
107
108
00:07:25,100 --> 00:07:28,250
And next you'll see how the hit detection gets implemented.
108
109
00:07:28,790 --> 00:07:35,540
So basically, if this AI agent gets hit by another opponent, be it the player or another AI.
109
110
00:07:35,990 --> 00:07:37,970
Let's find out how that is implemented.
11137
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.