All language subtitles for 4. Implementing Raycasts

af Afrikaans
ak Akan
sq Albanian
am Amharic
ar Arabic
hy Armenian
az Azerbaijani
eu Basque
be Belarusian
bem Bemba
bn Bengali
bh Bihari
bs Bosnian
br Breton
bg Bulgarian
km Cambodian
ca Catalan
ceb Cebuano
chr Cherokee
ny Chichewa
zh-CN Chinese (Simplified)
zh-TW Chinese (Traditional)
co Corsican
hr Croatian
cs Czech
da Danish
nl Dutch
en English
eo Esperanto
et Estonian
ee Ewe
fo Faroese
tl Filipino
fi Finnish
fr French
fy Frisian
gaa Ga
gl Galician
ka Georgian
de German
el Greek
gn Guarani
gu Gujarati
ht Haitian Creole
ha Hausa
haw Hawaiian
iw Hebrew
hi Hindi
hmn Hmong
hu Hungarian
is Icelandic
ig Igbo
id Indonesian
ia Interlingua
ga Irish
it Italian
ja Japanese
jw Javanese
kn Kannada
kk Kazakh
rw Kinyarwanda
rn Kirundi
kg Kongo
ko Korean
kri Krio (Sierra Leone)
ku Kurdish
ckb Kurdish (Soranรฎ)
ky Kyrgyz
lo Laothian
la Latin
lv Latvian
ln Lingala
lt Lithuanian
loz Lozi
lg Luganda
ach Luo
lb Luxembourgish
mk Macedonian
mg Malagasy
ms Malay
ml Malayalam
mt Maltese
mi Maori
mr Marathi
mfe Mauritian Creole
mo Moldavian
mn Mongolian
my Myanmar (Burmese)
sr-ME Montenegrin
ne Nepali
pcm Nigerian Pidgin
nso Northern Sotho
no Norwegian
nn Norwegian (Nynorsk)
oc Occitan
or Oriya
om Oromo
ps Pashto
fa Persian
pl Polish
pt-BR Portuguese (Brazil)
pt Portuguese (Portugal)
pa Punjabi
qu Quechua
ro Romanian
rm Romansh
nyn Runyakitara
sm Samoan
gd Scots Gaelic
sr Serbian
sh Serbo-Croatian
st Sesotho
tn Setswana
crs Seychellois Creole
sn Shona
sd Sindhi
si Sinhalese
sk Slovak
sl Slovenian
so Somali
es Spanish
es-419 Spanish (Latin American)
su Sundanese
sw Swahili
sv Swedish
tg Tajik
ta Tamil
tt Tatar
te Telugu
th Thai
ti Tigrinya
to Tonga
lua Tshiluba
tum Tumbuka
tr Turkish
tk Turkmen
tw Twi
ug Uighur
uk Ukrainian
ur Urdu
uz Uzbek
vi Vietnamese
cy Welsh
wo Wolof
xh Xhosa
yi Yiddish
yo Yoruba
zu Zulu

Original subtitles

0 Welcome to the raycast part. 1

In this session we are going to look how the raycast sensor is implemented. 2

If you remember, we went through the range sensor which tested the range around the AI. 3

Then we looked at the field of view which looked in front of the AI. 4

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

occluded by a cube, then the enemy will still be able to detect it. To prevent the enemies hitting you 6

. 7

through the walls. 8

We need to use raycasts. 9

And raycasts are actually lines that are more physically based. 10

They are not visible in this case. 11

I'm just using a debug line for illustration purposes. 12

But rays are invisible lines that are casted from a point towards a direction and then whatever they 13

intersect in their path, it returns that. 14

So in this case it will be this cube first. 15

So the detected is false, but then if we move, the detected becomes true. 16

And then of course I'm trying here of different obstacles and this is the result that I'm getting. 17

So basically this is what the raycast does. 18

Let's see now how this is implemented and how can you use it in your own game. 19

The main structure is very, very similar to the other range scene and field of view scene with the 20

main difference of course being that this time we have the raycast detector. 21

So this is probably where we need to go check. 22

But I also want to mention that I put some obstacles and the obstacles here. 23

They do have a static body with a collision shape. 24

I opted to put them on layer ten. 25

So we need to make sure that the raycast works for layer one as well as layer ten. 26

Otherwise we don't detect these collisions. 27

But let's move on into the raycast detector. 28

So I created another scene for this because it's much easier to have it modular. 29

And let's look now, what does it have? 30

So I'm using a DebugHit, which is actually just the simple sphere. 31

This is used to show the exact point where the ray hits the object. 32

The raycast, which is actually 33

a node that you can make by create a new note and then raycast. 34

This will give you this object. 35

This object 36

you can actually specify what layers does it collide with. And you can give it a position by moving the 37

object around. 38

And you also can give a direction here. 39

So it actually points down. 40

But this will be changed at runtime because we actually need to point this towards the player and then 41

make sure that it actually hits the player. 42

This doesn't come enabled by default, so make sure that you have it enabled, otherwise it won't work 43

at all. 44

And the debug line is just the line that makes it visible for us to see. 45

There is another way to debug physics objects, so we go to debug and put visible collision shapes 46

to true. 47

And if we run this scene, then you'll see that this capsule shape, this ray is actually intersecting 48

the line I'm making. 49

And also these shapes and any other physics shape is basically visible. 50

I'm going to turn this off because I already have the debug line for this. 51

But what's the most interesting part is this script that is attached to the raycast detector and let's 52

see what's inside. 53

And here, of course, we have a couple of things. 54

I opted to use is_enabled and initialize. 55

These are not necessary. 56

I'm using this because they'll be used in a sensor manager later on. 57

But the most important part is what's inside is target visible. 58

And then here you see, is just only one function. 59

But it does a couple of different things. 60

First, it gets the target, which in this case it's the player either detected by the range sensor 61

or the field of your sensor. 62

Then I'm getting the origin point, but I'll be getting that from the collision shape. 63

And the collision shape. 64

It's basically the collision shape of the object instead of the actual mesh of the object. 65

And I opted to do this because the mesh rotates a little differently from the collision object. 66

So I want to make sure that the ray is always pointed towards the center of the collision. With the 67

position 68

we cannot put the cast_to value just yet because we need to get out of this position. 69

The direction and basically the direction is a vector of length one that points from the enemy towards 70

the target. 71

So how can we do this? 72

Of course we need to take the target position and then we can actually get the ray position, which 73

is the exact position as the AI agent. 74

And if we do a vector subtraction, we actually get the vector. 75

But the thing is, the vector is of length in between (distance) them. 76

So to actually set it to length one, we need to normalize it. 77

And this is how we have the direction. 78

Of course, you need to assign this direction to the cast to value for the raycast. 79

Make sure to multiply this a little more. 80

So the direction is actually further because I've noticed that if it's exactly as the player, then 81

it might not detect the collision 82

good. 83

So try to multiply this 84

with a number not so big, not so small, but this will actually increase the chances of the collision 85

being detected properly. 86

And then I'm using this force raycast update. 87

This is actually necessary to update the collision information. 88

This is required instead of waiting for the next physics process call. 89

So make sure to update the raycast before. And then this is just for debug purposes so I won't go into 90

much detail, but the line gets the two points needed to create the ray, which is the origin point 91

of the ray in the final point. 92

And the most important part for us is this IF statement which checks if the raycast is colliding. And 93

if the raycast is colliding, then we need to get what collider is here. 94

So if the collider is equal to the target, that means if it's the player, then it'll return 95

true. 96

If it's not, then it will return false. 97

So basically, if we give the player as the target for this one and the ray doesn't have any obstacles 98

between the AI agent and the player, this will return true. 99

If it finds an obstacle on the way. 100

Like in this case the cube, then it'll return false. 101

And this is how a raycast detector can be implemented. 102

Please make sure to set up the raycast position correctly. 103

So in this case, it can go from the enemy's eyes. 104

But if you don't assign it correctly, it might go from its legs. 105

So make sure that it's from a point where the enemy does have its eyes or the detection functionality. 106

This is it. 107

And next you'll see how the hit detection gets implemented. 108

So basically, if this AI agent gets hit by another opponent, be it the player or another AI. 109

Let's find out how that is implemented.

Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.