How to make Line of Sight in Unity 2D with Raycast

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's take a look at the line of sight and talk about raycasting all right we finally back in here he wants more and in this tutorial we're gonna be talking about line of sight this comes as a suggestion from Archangel Azrael because last time we did following the player so basically these little crabs over here are following the player we can actually take a look at this you can see they're basically following the player in the exact position where the player is at so that is pretty cool but they asked okay how could we do line of sight right so how could we maybe make a wall between and then the enemy is actually not going to follow the player and we're going to implement this right now this code without the modifications is also going to be available to you in the description below as a Unity package so you can basically start from exactly this project with the Enemy full player script and let's take a look so basically we just want to change this script a little bit and what we're going to do first and foremost is we're gonna have a private rule called has line of sight and this is going to be false by default for default the enemy does not have line of sight to the player and then what we're going to do is we're going to use raycasting so raycasting is a very cool thing that basically allows you to draw a line let's say or cast a rave from one position into a certain direction and then tell whether or not it's going to have a collider or hit anything along the way this will happen in the fixed update method and what we're going to do is we're going to do this by making a raycast hit 2D you can see this one we're going to call this the ray is going to be equal to physics 2D dot raycast so this is a raycast method right here the first parameter of which is going to be the origin as you can see and then it's the direction this is extremely important to think about this isn't going from this position to this position it's going from this position in this direction so the position is going to be transform position that's fine so this is going to go from the enemy and then to get it to the direction of the player we want to say player dot transform dot position minus this position so there's going to be transform that position because this gives us the direction this is basic Vector math I highly recommend checking out some of that if this might be confusing or interesting to you is basically how you get the direction from this position to this position and with that we can basically then say hey if the ray dot collider is unequal to null basically meaning we have actually hit something but if this is the case then we can say has line of sight and then we're going to do something really cool we're going to say ray.collider and we're going to look at the tag so we're going to compare the tag and if it's the player then we have line of sight and if it's not then this is automatically going to be false so that's really cool and then just to help us see a little bit better we're going to make has ifla has line of sight we're going to debug dot draw and this is awesome we can draw a race so we're going to draw a raycast I'm going to basically put in the exact position and direction that we are using in the raycast over here so this is going to be once again transform dot position and then the direction once again is going to be player transform position minus transform dot position and we're gonna do this in color green so basically we're going to draw the array in green when there is a line of sight and then we can do an else statement right here and what I'm going to do is I'm going to copy over the debug there we go so I'm just going to select this press Ctrl C and then Ctrl V to paste it in and then here we're going to make a red so basically meaning that if there's no line of sight then the array is going to be color red otherwise it's going to be colored green and then the last thing we want to do in the update method is we want to only do the position change over here if the line of sight is true so we're just going to take this cut it out Ctrl X and then control inside of the if statement and now only if has line of sight is true then are we actually going to move towards the player and that is actually all that we want to do here this is this is everything that we need with an inbuilt cool debug over here this is of course something that in the game itself would not be a thing right you really wouldn't be able to see the line of sight but this is really good for debugging purposes so if we now jump back into the game in theory everything should be set up the enemies have the enemy follow player script and what's very important is that the player has the player tag right here that is of course quite important and what we're going to do is we're just going to add this These Bricks over here as as walls right so we're just going to Let's size them up a little bit so that we basically take a look at whether or not this is going to properly work let's do a five by five let's go oh it's not a 55 that is a little bit too big maybe that's going to be enough already so just something like this I'm going to duplicate this Ctrl D just so that basically like both of them like all of them have no line of sight when we're starting and to actually block the line of sight the bricks right here need a collider so in this case we're just going to add a box collider this is going to add a simpler collider basically on top of the entire bricks game object over here and what's of course also important is that your player also has a collider otherwise the raycast is not going to work and there's another step if your crap in this case our crabs do not have a collider if your enemies also have a collider you're going to need to ignore that we're going to take a look at that in just a moment because usually your enemies will also have a collider let's first of all take a look at it like this and we're gonna see if we go in you can see all of the arrays are red right and if I move over here and you can see this is going to sneak through there you go so there it has line of sight and it turns green and it starts following me I mean this is this is a really really freaking cool effect and it's pretty straightforward All Things Considered right like it's not that crazy if as you've seen right so there you go and now I'm I I can't I can't escape anymore no there you go so that is going to be the line of sight like this and then one of the issues you might run into if one of your crabs or one of your enemies right has a box collector 2D added to it you will now find that if I go try to go into the line of sight of it you will see it does not work because basically it's it's actually detecting itself and it says no I I'm I can't see the player because I'm seeing myself which of course is I mean number one kind of ridiculous and number two it's not really going to work now there are actually a plethora of ways that we can fix this so one thing would be under the layer right here we're just going to use ignore raycast on the crab itself and then what's going to happen if I go into the game again you will find that now the crap follows me without any issues because basically it's ignoring while the raycast is is ignoring the game object that we've specified right here so that works totally fine another way that you can do this is instead of using raycast you can also use raycast all now the difference here is going to be if I hover over this this is how a raycast hit array basically this is going to give you every single raycast hit along the way so it's not only going to give you the first one that hits right so in this instance right obviously only the first collider that is going to be hit with the raycast is going to be converted into a raycast hit with raycast all you're going to get the crab and you're gonna get the wall and you're gonna get the player and if there's maybe something behind the player you would even get that as well this is also feasibly how you can do this the ignore raycast here is probably going to be the easiest way to facilitate this raycast all might be a more robust way if for example there's going to be something else that you need the raycasting ability for your enemies for but yeah that is line of sight for your enemies pretty freaking awesome and that's already it for this tutorial right here now take your line of sight and put it to this video right here where we'll add custom project tiles in unity hope to see you there so yeah
Info
Channel: Game Dev By Kaupenjoe
Views: 3,006
Rating: undefined out of 5
Keywords: Kaupenjoe, Unity Tutorial, Unity 2D Tutorial, Unity Beginner, Unity How to, Unity Guide, C#, Unity Hide Mouse, Unity Raycasting, Raycast, Raycasts 2D, Raycast Unity 2d, Raycast Unity, Unity Line of Sight
Id: xDg2pxqJHq4
Channel Id: undefined
Length: 7min 4sec (424 seconds)
Published: Mon Sep 04 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.