Field of view visualisation (E01)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Another great tutorial. I especially love learning about Editor scripts, they're so useful and take hardly any time to make.

👍︎︎ 6 👤︎︎ u/Arma104 📅︎︎ Dec 26 2015 🗫︎ replies

you got yourself a new patreon! Im really amused with your work and so i decided to support you!

👍︎︎ 2 👤︎︎ u/ZuidPortugees 📅︎︎ Dec 26 2015 🗫︎ replies

Excellent tutorial. Very well explained and useful topic. Thanks so much!

👍︎︎ 1 👤︎︎ u/Telescopeinthefuture 📅︎︎ Dec 26 2015 🗫︎ replies
Captions
hi everyone welcome this little two-part field review series so in this first episode we'll work on setting up the system to be able to detect which of the targets are visible to the character at any given moment and then in the second episode we'll work on actually drawing a little representation of the characters field view so just a quick outline of how the system will work we'll start off by finding all of the targets within a certain view radius around the character and any targets outside of that view radius we'll just consider too far away to be seen then we'll measure the angle between the direction the character's looking and each of the targets to determine which of them fall within the sort of view angle of the character finally we'll take those remaining targets and perform a ray cast from the characters position to the targets position to determine if the line of sight to any of them is blocked by an obstacle so in this little diagram over here of course the one on the left is blocked by an obstacle so we have only one target that can actually be seen okay so then mind let's drum it to unity and just set up a little test scene here quickly so just a plane and a bunch of cubes to act as obstacles we've got some material set up we'll just quickly apply these and you can just scatter a couple of these obstacles around the map just rotate scale them randomly all right that ought to do it this grew create a little empty game object call this obstacles and can just organize all of these different obstacle objects under that I'll just go into my main camera rotate that 90 degrees and just set it up so that it's looking down on the map maybe just set this to orthographic mode just zoom it out a tiny bit and then let's maybe create a capsule for our character is a quite big compared to the map so I'm just going to grab the plane and the obstacles and just scale these up all right that looks better I just want to zoom out the main camera now quickly something like that so I'll call this the character and can duplicate that and call this the target so just add a target material to that and also just scatter these around the map okay once again I'm just going to make a little empty game object maybe call this targets and just keep everything nice and organized parent those to that so we're going to want to have a separate layer for the obstacles and the targets so let's go ahead and say add layer call one obstacle x' and the next targets so if we just quickly select all of our obstacles here we can say layer obstacles and likewise for the targets select this all and choose targets okay then we're going to want to create a new C sharp script so we're going to want something to just be able to move our character around so that we can test things out I'll just call this the controller and then we can create another C sharp script and maybe call this something like field of view right I can just select is both and drag this onto the character while we're here I'll just add in a little rigidbody component that we can collide with obstacles and I'll just freeze the rotation on all three axes since don't want the character falling over let's just make sure that these are all actually above ground level just move them up okay and let's open the controller script and just set up something super simple I don't want to get a reference to our rigidbody rigid body rigid body I also want a reference to the main camera so we can just say camera call this the view camera all right and then start method we'll just assign those so rigid body is equal to get component of type rigidbody and view camera we can set to camera domain then in the update method we'll want to find the position of the mouse in world space so we can just say vector 3 Mouse position is equal to and here we use our view camera to get the screen to world point conversion method so we want to pass in a new vector3 with input dot Mouse position X for the x-axis input dodge Mouse position dot Y for the y-axis and then for the z-axis we just want to specify the distance of the view camera from the ground plane so just view camera doc transform to position dot Y that's not necessary if you're using an orthographic camera like I am but in case you want to use a perspective camera we'll just add that in then we want to rotate the character to look at the mouse position so you can just say transform dot look at and I'll special we want to make sure that the mouse position is level with the character those so that the hair doesn't have to sort of bend to look at it so we can just add vector three-door up multiplied by the Transformers position on the y-axis finally we want to actually move around so let's create a little vector 3 to 12 velocity in and let's also make the public float so we can set our move speed and maybe just set that equal to 6 by default so we'll say velocity is equal to a new vector3 and we'll just get input from the horizontal axis for the x axis and input from the vertical axis for the y axis we want to normalize this and multiply it by the move speed okay so since we need to move rigid bodies in the fixed update method let's create that a fixed update you can just say rigidbody dodge move position and our new position will be our old position plus the velocity x time fix Delta time okay so for anyone who downloaded the starting file and skipped ahead what we've got set up now is a character that can walk around the map and the map consists of a bunch of obstacles each with an obstacle layer applied and a bunch of targets with a target layer applied okay so with that out of the way we can begin work on the field of view script so let's start out with two public floats want to define the view radius and another to define the view angle now it should be very helpful to have a method which could take in an angle and then just spit out the direction of that angle so let's make a public method returning a vector3 call this direction from angle this can take in a float and we can just call this angle maybe specify that the angle should be given in degrees all right we're going to be using trigonometry to figure this out so one thing we should just keep in mind is that when we're doing trigonometry we usually think of a unit circle with 0 over here going around anti-clockwise 90 degrees 180 degrees 270 degrees whereas of course in unity zero Degree starts over here and we move around the circle clockwise 90 180 and 270 so if we call our angle in unity X then in order to convert it to the angle that we'll be using for trigonometry we'll want to say 90 minus X so we can see for example that 90 minus note equals 90 90 minus 90 equals north and here it's the same sort of thing 90 minus 180 equals negative 90 or in other words to 70 and 90 minus 270 equals negative 180 which of course is the same thing as 180 now since the sine of 90 minus X is the same thing as simply the cosine of X all that this actually ends up meaning is that when we're using an angle of an object in unity we're going to swap around sine and cosine so in our direction from angle method we can say return new vector3 and for the x axis we'll use math f dot sine of angle in degrees converted to radians with math F dot degrees to radians we'll leave the y-axis at 0 and for the z axis that will be math F dot cosine of the angle in degrees once again multiplied by math F dot degrees to radians now to be great to have a little visualization of our view angle and view radius in the editor so let's let's work Lee go ahead and create an editor script for that so make an edit folder which remembers mandatory for editor scripts and we can call this the maybe field-of-view editor all right open this up and we can say using unity editor and this extends the e editor and we'll just want to add a little tag saying that this is a custom editor of type field of view okay so we're going to want to draw some handles into the scene window so we'll use the unseen GUI method all right now we want to get a reference to our field of view script so we can say field of view I will just call this fo W for short this is equal to and we can get the object that this is a custom editor off with the target variable and we just want to convert that to a field of view object okay so to draw our view radius let's say handles color is equal to colored or white then we can say handles dot draw wire arc we want to give it the center so that will be the position of our field of view object so fill the view dot transform position then I want say normal in other words the direction around which the angle is going to rotate so that can be vector three dot up then it wants a from vector so that's where the angle will start so that can just be vector 3 dot forwards and then it wants to know how many degrees it must rotate so we want it to rotate all the way around 360 degrees and then the actual radius will be our field of view dot view radius okay let's see if that's working so just go into unity and we'll go onto our character and let's just drag out our view radius you can see on the editor over here we've got this nice little a wire circle being drawn to visualize that so let's add in the view angle as well I'm quickly going to hop into the field of view script and just clamp this to a range of 0 to 360 another thing that might be nice to have is just a little boob parameter in our direction from angle method saying angle is global and if the angle is not global so as to say if not angle is global then we're going to want to convert it to a global angle by adding the transforms own rotation to it so we'll say angle in degrees plus equals transform dot Euler angles dot Y okay so now let's go into the field of view editor and let's say vector 3 view angle a is equal to field of view dot direction from angle and we can give it an angle of negative field of view dot view angle divided by 2 and we can say that this is not a global angle all right and we can say vector 3 view angle B is equal to the same thing except positive field the view angle all right so now we can say handles dot draw line from the field of views transformed up position to the field of view transform drop position plus view angle a and we can multiply that by the field of view view radius to set the length of that line okay so let's just copy that quickly and for this one we just say view angle B okay let's test this out quickly so just go into the character and we can mess around with the view angle variable and as you can see the sort of view segment is being drawn nicely in the scene window and since we said that the angle is not a global angle that should mean that if we now rotate our character that angle should remain relative to the character's rotation which it is doing so yay okay so remember those three steps from the beginning let's quickly whiz through those just open up the field of view script we'll quickly go to want to create a public layer mask for our target mask as well as a layer mask for our obstacle mask and let's make a little method to say find visible oops add an extra s find visible targets so I want to start off by getting an array of all of the colliders of all of the targets within our view radius so we can say Collider array targets in view radius is equal to physics overlap sphere and we can give it a center of transform drop position a radius of course of view radius and to make sure that it only returns targets we will use our target mask so we'll want to loop through all of these so let's say 4 into I equals note I less than targets in view radius dot length I plus plus we can say transform schoolís target is equal to targets in view radius with an index of I dot transform now I want to check if the target falls within our view angle so if we imagine that this is our character and is maybe looking this way and we've got our target over here if we find the direction from our character to the target we can then use the vector 3 angles method to find the angle between these two directions so let's say vector 3 direction to target is equal to the targets position - our own position and we'll want to normalize that then we can say that if vector 3 dot angle between the direction we're looking so transform drop forward and the direction to the target if that angle is less than the view angle over - then we'll know that the target is within our view angle and we'll just want to perform the last step which is to see if there's an obstacle between our character and the target so let's start by calculating the distance to the target so we can just say vector 3 dot distance pass in our position and the targets position and we can say if we don't collide with anything when we perform a ray cast from our own position towards the target with a distance of distance to target and a layer mask of obstacle mask then that will mean that there are no obstacles in the way and thus we can see the target so since we're not actually creating a game yet here we don't have anything in particular that we want to do with the target once we see it but perhaps we can just maintain a list of all of the targets that are currently in view so we can say using system dot collections dot generic and we can have a little public list of type per transform call this visible targets so that equal to a new list of transforms and when we see a new transform we can just say visible targets add target ok and each time we run this we want to just start off by clearing all of the targets from the visible targets lists that we don't end up with the duplicates so we can say visible targets dot clear okay so all that remains to be done is to actually call this method so we maybe want to do that from a co routine so let's make a little ienumerator call us find targets with delay you can have a float parameter for the delay this can just say while true we'll want to yield for however many seconds the delay specifies and then we'll want to call the find visible targets method okay and then from the start method we can just initiate this corrosion so we can say starch co-routine pass in its name and the argument let's say every all should be pretty quick every point two of a second okay at the moment of course we're not going to know if this is working or not because nothing happens when we see a target so maybe we can just quickly go into the field of view editor and say for each transform visible target in field of view dot visible targets handles dot draw line from field of view transform drop position to visible target or position and let's make that line reg so before we start that for each loop we can just say handles color equals color dot red let's see if this is working so press play and it is not working probably got something to do with the fact that we haven't actually assigned these layer masks yet let's say targets and obstacles I don't really want the visible targets to show up in the field of view inspector and even no lists did that is that new I'm not sure so let's just say hygiene inspector it needs to be public so that our editor can access it okay so let's try this out again hopefully with more success this time cool so our little red line is showing up there let's just quickly walk around test it out for these other guys all seems to be working once we go behind an obstacle we lose our field of view which is good and let's just see if I rotate my character as soon as the angle passes the center of the target visibility is lost which is what we want and if it's outside of our view radius we can't see it and as soon as it gets into our view radius we can't see it so everything seems to be working as intended so I'm going to end this episode here don't forget to join me in the next episode we will look at creating that little field of view visualization should be pretty interesting but until then Cheers
Info
Channel: Sebastian Lague
Views: 330,469
Rating: undefined out of 5
Keywords: Unity (Software), line of sight, stealth game, c#, programming tutorial, game development
Id: rQG9aUWarwE
Channel Id: undefined
Length: 22min 31sec (1351 seconds)
Published: Sat Dec 26 2015
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.