Pathfinding with Navigation2D in Godot #2 (Adding Line of Sight)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
so you want to make enemies detect player only within a certain range hey it's ice and in this video i'm going to teach you how to make a line of sight in godot okay go to the enemy here add a recast 2d basically it's a line that is able to detect collisions and nothing much so first of all don't forget to enable it i don't know why it's not enabled by default but it isn't so be careful here we set the cast values that basically defines the direction of this raycast so we need it to be a straight line that will be rotated so set the y to 0 and the x to 70. for instance we can set the collision layers for it and we can set whether it can collide with areas and or bodies we will detect just the player's body okay rename the raycast to line of sight and now let's add some functionality the raycast has no signals however it has a few very convenient methods that we are going to use go to the code and here first of all add a reference to it and under player create a boolean variable code player spotted in physics process if the player exists rotate the line of sight so it's always looking at the player's global position and generate a path and navigate by this path only when the player is spotted okay a reasonable question would be how we can spot the player if it's false here right we're going to create another method called check player in detection and its functionality would be this okay so the first line variable collider is line of sight get clarity what is it as i said regas has a bunch of very useful methods and get collider returns the first object that the array intersects or null if no object is intersecting it so we check if there is some object and if it is then we check if this object in the player group and if it is then we obviously just found the player and we set the player spotted to true we print the message that currently serves just debug purposes and we also return that player is in the detection area so you might ask why do we return true or false regarding if the player is in the detection if we already have the player spotted variable it's a nice question c player spotted variable changes only once first it's false and then it sets to true at the first time when the player was spotted so it's like an alarm you are detected however this method can be called every single frame telling us whether the player is in the range and it is very useful for position specific behaviors like enemies that would shoot the player only in a certain range like 300 pixels right and further than that they will not shoot the player they will chase it or whatever so you get the idea now after we set the rotation of the line of sight call this our new method and well we can already check it as you can see it's working the raycast has just collated with us and player spotted is now true and the enemy is already chasing us however there might be a very serious problem and you see what look this enemy has detected us and it's trying to chase us however this second enemy we can see that it's raycast also touching the player but for some reason it doesn't chase us the problem is that this raycast currently intersects every physics body and detects it so whether it's a player whether it's a wall or an enemy it will be all collided with our raycast however in your game you probably don't want a bunch of enemies not rack to player because they are behind one of the enemies right and in our project i want them to be able to see player through each of them however the only obstacles that must be for the raycast are the player itself and the wolves because you obviously don't want to say that the player can be spotted if it is not visible the solution to it would be using collision layers go to project settings and in general tab open layer names 2d physics i have already covered collision layers in my combat essentials episode 2 so if you want an introduction please watch my video without further ado rename the layers to environment player and enemy close save update the scene and now let's set the collisions for our line of sight collision mask environment and the player okay good and enemy it exists in the enemy layer and it will interact with environment player and enemy here tile map it exists in environment and interacts with player and enemy well it doesn't really interact but still we can use it it's not a big deal for now and the player it must exist in the player layer and collide with the environment and enemy great now let's see if our system is working as you could see some of the raycasts were going through the enemy and still they detected the player so that was what i wanted to make so that's it hopefully that was useful and you liked it and if you did don't forget to like share and subscribe it was ice and until next time you
Info
Channel: John Ivess
Views: 2,311
Rating: undefined out of 5
Keywords: godot, gdscript, pathfinding, tutorial, programming, coding
Id: WNF8U_ogrL8
Channel Id: undefined
Length: 6min 27sec (387 seconds)
Published: Wed Apr 21 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.