Hi everyone, a quick little two-parter on creating a field of view system for stealth and tds games. Hope you find it useful :) In this first episode we create the main system for detecting objects in our line of sight, as well as a custom editor so we can see what's going on in the editor. In the next episode we'll create a cool effect to visualise the field of view. If you'd like to support my tutorials, I have a patreon page here: bit.ly/sebPatreon
@AkumaRaion9 жыл бұрын
+Sebastian Lague another great tutorial dude! this was something i need for my project, I made a much simpler version of this with using only raycasts but this one is much better than mine. I was trying to do the same but I'm quite enw to unity editor. I am getting an error with "target" variable you used in FieldOfViewEditor script since I didn't define it. ... Well iI just noticed my editor was still a MonoBehavior script. Maybe someone else might get the same error so I'll leave this here :P
@RaunaqSoni969 жыл бұрын
+Sebastian Lague This is something I always think when I watch one of your tutorials, HOW AND WHERE do you learn such great stuff from ???
@ibrahimb7569 жыл бұрын
+Sebastian Lague How do you know all of this ?. Is it by practicing many examples/getting used to them then adding them to your library of features you learned ? Is that enough to gain knowledge/logic to create new methods/ideas over time(which i know will take me at least a year of hard working) ?. Or is that not enough to be a good or great developer ?.... :( I feel so stupid and lost. Every time i learn something hard something more insane appear lol. Also great sources to learn like your channel is very hard to find. xP
@SebastianLague9 жыл бұрын
+Ibrahim A I know how you feel, I've been at this for more than 4 years and there is still an overwhelming amount that I don't know/can't do! Still, with practise and exposure to different problems, one definitely begins to gather insights that allow one to tackle new challenges by oneself. So keep at it and don't worry about feeling lost sometimes, it's quite normal :P Glad you like the channel!
@ibrahimb7569 жыл бұрын
+Sebastian Lague Thank you ^^. You made me more confident after your replay. :D
@accioollie97102 жыл бұрын
This tutorial is even better than paid unity assets! Even 7 years later. Amazing tutorial!
@Hazzel313372 жыл бұрын
YES i agree still awsome still using it today xD
@mrbruh2460 Жыл бұрын
fr
@h8today5 жыл бұрын
This largely works for 2D as well. In case you can't figure it out, here is some information: Anything done on the Y axis (including transform.up) needs to be changed to the Z axis (transform.forward), and anything on the Z axis needs to be changed (I believe) to the X axis (transform.right). Any Physics or Colliders need to be changed to their 2D counterpart. Collider[] targetsInViewRadius = Physics.OverlapSphere(transform.position, ViewRadius, TargetMask); Becomes Collider2D[] targetsInViewRadius = Physics2D.OverlapCircleAll(transform.position, ViewRadius, TargetMask);
@sketh70924 жыл бұрын
You are a savior, my friend. Really appreciate this advice, this will save me tons of time that I would have wasted on searching for solutions online.
@Danee21084 жыл бұрын
I can't get it to work :(
@rendeer88224 жыл бұрын
Hey and also in the part with angleInDegrees += transform.eulerAngles.y; Should be in 2d as angleInDegrees -= transform.eulerAngles.z;
@sharkk2444 жыл бұрын
@@rendeer8822 and on the editor script what i should put?
@rendeer88224 жыл бұрын
@@sharkk244 There's not much you need to change except for the line in the video that says Handles.DrawWireArc(fov.transform.position, Vector3.up, Vector3.forward, 360, fov.viewRadius Should be changed into Handles.DrawWireArc(fov.transform.position, Vector3.forward, Vector3.right, 360, fov.viewRadius
@YulRun2 жыл бұрын
My god I've followed your stuff for a few years, and stumbled on this old video, still a gem. This has so many applications for using this calculation.
@binarystateofmind29714 жыл бұрын
Sebastian as always you make the best tutorials, that help even 5 years later. Thank you for that
@echodev73183 жыл бұрын
Thanks for confirming this still works
@pedicagames37297 жыл бұрын
It was one of the best tutorial videos I've ever seen. 1) Smooth expression. 2) First creates a problem, then solves that problem. Much better. 3) No unnecessary talk. Now I will look at the other videos. Thanks.. you deserve to make money :)
@justmark80409 жыл бұрын
Your videos are amazing. It's 3:30 am and I'm still watching you. Your explanations are very entertaining even if you're explaining boring stuff :D
@philliptemidayo7 жыл бұрын
Just Mark lol
@Sisyphus.5 жыл бұрын
yeah it's relaxing
@asclepiiusunknown10904 жыл бұрын
This is amazing even today, and I find it super useful even to add into your top down shooter tutorial.
@thatonedude-68193 жыл бұрын
How about today
@whiteline41573 жыл бұрын
@@thatonedude-6819 This is amazing even today, and I find it super useful even to add into your top down shooter tutorial.
@thatonedude-68193 жыл бұрын
@@whiteline4157 but like… what’s about *today*
@whiteline41573 жыл бұрын
@@thatonedude-6819 This is amazing even 19/09/2021, and I find it super useful even to add into your top down shooter tutorial.
@thatonedude-68193 жыл бұрын
@@whiteline4157 lol
@h3llmepone8 жыл бұрын
Awesome tutorial. Thanks for the excellent explanations. I was trying to do this, but just with multiple raycast and other crazy things, but this is much better. Now I just have to read all the scripts almost 5 more times to understand everything... _=D
@grepme20017 жыл бұрын
Thank you so much! I've been puttering around for days trying to constrain the turrets to a firing arc on the ships in my little free scrolling shooter prototype and this adapted perfectly! I've been cutting my teeth on this stuff for a few weeks now and you've provided quantity and quality of insight an order of magnitude above any other I've found. Again, thank you.
@midniteoilsoftware2 жыл бұрын
All of your tutorials are fantastic! Clear, concise, and to-the-point. I hope to reach that level with my tutorials.
@habsi708 жыл бұрын
For those interested in seeing the only the viewAngle and not the whole circle in the editor (shows only the form of a pizza-slice) - a small adaption of FieldOfViewEditor.OnSceneGUI: void OnSceneGUI() { FieldOfView fow = (FieldOfView)target; Handles.color = Color.white; Vector3 viewAngleA = fow.DirFromAngle(-fow.viewAngle / 2, false); Vector3 viewAngleB = fow.DirFromAngle(fow.viewAngle / 2, false); Handles.DrawWireArc(fow.transform.position, Vector3.up, viewAngleA, fow.viewAngle, fow.viewRadius); Handles.DrawLine(fow.transform.position, fow.transform.position + viewAngleA * fow.viewRadius); Handles.DrawLine(fow.transform.position, fow.transform.position + viewAngleB * fow.viewRadius); }
@rvv276 жыл бұрын
Sebastian Lague is the master for the Expert Unity Tutorials on KZbin. Not those high trafficked easy peasies
@razorback9999able4 жыл бұрын
To draw wire arcs in 2D, use this: Handles.DrawWireArc(fow.transform.position, Vector3.forward, Vector3.right, 360, fow.viewRadius);
@darkergood52484 жыл бұрын
Thanks man. U r a savior.
@mrss6494 жыл бұрын
Thanks!
@fleksyy4 жыл бұрын
+ If you have troubles with the inner FOV on 2D you should change z and y in the returned Vector3 of DirFromAngle
@blaqkstar Жыл бұрын
Man, thank you so much. This was killing me
@lazypug-hayud9 ай бұрын
thanks a lot man i love you
@rogers58879 жыл бұрын
I've been looking for a tutorial for this type of mechanic for a long, long time. Many thanks Sab and Merry Christmas/Boxing day!
@mrss6494 жыл бұрын
Hello
@channel_moved96198 жыл бұрын
Just made the most advanced AI I've made, line of sight, patrolling and losing interest in the player as well as reacting to sound, and investigating sounds. Amazing tutorial explained really well.
@purpleice23438 жыл бұрын
How does it react to sound? Actual sound or just self made sound radius for steps and etc?
@channel_moved96198 жыл бұрын
Sorry for the late reply. It detects sound in a simple way. Firstly, instead of using PlayOneShot, I instantiate an audio object that uses Play after it sets the clip to the audio clip I gave. This way the clip is stored in the game object until it's destroyed. When a sound is played, a function is called in the ai that checks the sound name, audio source volume, and range. Depending on these parameters it will either investigate or leave it alone.
@ZandreAiken3 жыл бұрын
Just found this tutorial. This is exactly what I needed. Great video series. Thanks, Sebastian.
@simeondoesstuffbg38816 жыл бұрын
I’m working on a Plants Vs Zombies style game lately and this was Very helpful in getting my turrets to properly detect enemies on their lane. Thank you very much.
@hunterongames776 жыл бұрын
Sebestian, As always a great tutorial with clear explanations, diagrams, and setup. Very fast, but then we can always pause in KZbin. I'd hate to be your student in a real classroom at this speed, though. LOL.
@jeffreycordova90828 жыл бұрын
Thanks for the great videos Sebastian. I always find myself coming back, these are great to use for references.
@Mark210844 жыл бұрын
Thanks for the great explanation!! This is exactly what I was looking for
@Terak473 жыл бұрын
This is a fantastic tutorial, exactly what I was looking for. Well documentation, explained, and the downloadable assets are great. Thank you!
@Inf4mousKidGames3 жыл бұрын
You know this is actually quite useful for my studies. I've been trying to figure out how to do aim assist for a shooter game I'm making and the field of view system here might be what I need
@Jointknight7 жыл бұрын
Wonderfully applicable work, thank you kindly taking the time to do these demos!
@newsystem36676 жыл бұрын
I'm 13 minutes in and this looks really advanced, but i love it
@EpicSqu1rrel7 жыл бұрын
Seriously underrated unity tutor, should have way more subs and likes! great vid, you're a genius
@lvx9699 жыл бұрын
This is awesome! Can't wait for part 2.
@mrss6494 жыл бұрын
Did you ever find one? I can't find it.
@lvx9694 жыл бұрын
@@mrss649 kzbin.info/www/bejne/bWSnlGiAiaiggas
@mrss6494 жыл бұрын
@@lvx969 thanks!
@lvx9694 жыл бұрын
@@mrss649 No problem :)
@mrss6494 жыл бұрын
@@lvx969 :)
@jumpman23nith3 жыл бұрын
This is amazing! Thank you so much for sharing this. Clear and concise.
@DynaZor5 жыл бұрын
Two tips on optimizing the code: Cache all the components you reference more than once, such as transforms. Instead of DIVIDING by 2, MULTIPLY by 0.5, it seems to be less taxing. Have a nice day :)
@KaletheQuick8 жыл бұрын
This is like the 3rd time I have been here for this Coroutines are at 19:20 Go there to see how to set up a coroutine. Thumb this up. Thanks Sebastian!
@dungooooo3 жыл бұрын
This is just what I needed. Thanks so much for a helpful video that works perfectly and totally understandable!
@grumpyoldman71664 жыл бұрын
I took your code and added the ability to see over walls and other obstacles. Basically add an object at eye level to raycast from on the source object and add a "head" object to serve as the target.
@abdou0237 жыл бұрын
Perfect. keep doing tutorials about mechanics like this please.
@_daniel.w Жыл бұрын
Saving this for later, this is going to be really useful for me. Appreciate it!
@DitlevIs3 жыл бұрын
Oh, this is very impressive! It sure helped me a lot! Thanks!
@Oxmond4 жыл бұрын
Wow. Really, really nice field of view tutorial! Well done! 👍🤓
@NevaranUniverse9 жыл бұрын
Again a stellar work with the tutorial - nice job!
@rizuoo4 жыл бұрын
Fantastic stuff as usual! One thing I was wondering is how to add 'layers' to the field of view like in traditional tactics game - where the first layer is within direct line of sight of an AI character. And a second layer where different conditions apply for detection (e.g. crouching, behind obstacles etc.)
@Mishaboy20105 жыл бұрын
For those of you that want the FOV circle to rotate with the object it is attached to use this: Handles.DrawWireArc(_fov.transform.position, _fov.transform.up, -_fov.transform.right,360, _fov._viewRadius);
@dragonG505 жыл бұрын
Hey, THAT YOU! Do you know if I can rotate the lines with the circle as well?
@Mishaboy20105 жыл бұрын
@@dragonG50 I am still trying to work that out myself :(
@appledev46379 жыл бұрын
Great stuff! Thank you!!! Extremely informative!
@FaithStudioTeam7 күн бұрын
Best ever unity tutorial 😃 love it
@rameezsafdar65634 жыл бұрын
Finest tutorial ever! More Power to you.
@lucasgabrielbica97157 жыл бұрын
Sos un genio! Apenas se algo de ingles y pude entenderte sin ningún problema! Gracias!
@hgopikrishnan5 ай бұрын
Thanks for this useful tutorial. I had one issue in my game when the enemy is standing on a carpet and the player is at floor, it is is not detecting the player. I slightly increased the ray cast transform's y value and it works in my case. Thanks again for the help 👏👍
@pourliver9 жыл бұрын
I really like your video! I'd like to see a video where you could show how to use the integrated Unity Networking system.
@MrGamer-ej8xj8 жыл бұрын
What a informative video.......amazing......thanks man.................you saved my time.
@pablosuarez92112 жыл бұрын
i shall name my first child after you, you golden god
@diyguild13273 жыл бұрын
Great tutorial! Took me a minute to get it to work as I think there is some legacy code in here instead of using [SerializeField] (I don't think it was implemented when the tutorial was made). If anyone is having a hard time getting the ray to show up, you need to generate the mesh for it to show up. So what I did was go into the Character and under the Field of View Script, you have the option to "View Mesh Filter." To get this to work, I made a game object and randomly placed a cube off to the side of the map. Then I created a material for the object, make sure to turn the alpha value all the way up to make it invisible (it will still be visible in the editor, but in the playmode it will dissapear.) Then go back and select the "View Mesh Filter" and add the game object you created and it should render a transparent mesh with the red ray visble.
@Zanktus2 жыл бұрын
I am not really sure what you mean with legacy code. I took the code 1:1 in the newest 2021 Unity version and everything works out of the box. SerializeField and so on is not really new at all.
@dcy32238 жыл бұрын
Hey Sebastian, your awesome tutorial help a lot, thank you !!!
@ritriviaiv86757 жыл бұрын
Ty this made some thing so much easyer now. really thank you
@joaozin0032 жыл бұрын
Timestamp: 7:07
@kachhadjagdish68127 жыл бұрын
Your videos are amazing i like it and most usefull for me. thank you.
@lukesapir15903 жыл бұрын
return new Vector3(Mathf.Sin(angleDegrees * Mathf.Deg2Rad), 0, Mathf.Cos(angleDegrees * Mathf.Deg2Rad)); ... I'm very grateful for this line of code!!! Thank you so much!!!
@pharos6403 жыл бұрын
You are the best this tutorial helped me a lot!
@raigami_san9 жыл бұрын
You're awesome man!! You're talented!! I love it...
@gotbaka3 Жыл бұрын
Great little video, very useful
@Thavernios6 жыл бұрын
Blowed my mind... A lot, actually because i suck a lot at math.
@levyadams12258 жыл бұрын
Dude, you are amazing. Thank you.
@nathancongdon248 Жыл бұрын
Make sure y'all type "IENumer-ator, and NOT "IENumer-able". I spent 10 minutes trying to figure out what broke my code before finding that mistake...
@pureatheistic2 ай бұрын
Side note, if you make this mistake, it's worth the effort to learn what the difference is and why it exists.
@TripleCreeper32 жыл бұрын
didnt understand a single word but i think it's a good video
@jthespacec9 жыл бұрын
That's fantastic! Thanks Seb! How is it for performance, though? Would doing this for multiple gameObjects at once slow things down? That's a lot of raycasts!
@davidkrivan47044 жыл бұрын
Thank you very much for this, very helpful.
@moduntilitbreaks4 жыл бұрын
Watching it with .5 speed and trying to follow :)
@mrss6494 жыл бұрын
Lol
@rik09046 жыл бұрын
i was looking for some thing similar to this, thx for puting code in github
@VGamingJunkieVT7 жыл бұрын
Is this tied to a previous tutorial? Unity's telling me "The name `target' does not exist in the current context"
@SebastianLague7 жыл бұрын
target is part of the Editor class. Does your script inherit from Editor?
@antonpolchenko63919 жыл бұрын
Thank you just what I looking for.
@futoadam72546 жыл бұрын
Great tutorial! Thank you very much!
@rgaryfugate9 жыл бұрын
Awesome! Needed something like this. thanks.
@RahulOne17 жыл бұрын
@Sebastian Lague Why you have included camera's y position in the script while calculation ScreenToWorldPoint @5:32 in the video?
@jazzling Жыл бұрын
among us video awesome 10/10 love it best video on KZbin
@jackbenfu5 жыл бұрын
Awesome work! Many thanks :)
@thejestersshow9783 жыл бұрын
Sebastian : So were going to be using trigonometry Me: ... I just started geometry...
@pauloloureiro99446 жыл бұрын
@Sebastian Lague | Thanks for your great tutorials! I hope you make anytime a "fog of war" tutorial.
@prakharsaxena44606 жыл бұрын
I dont know about the audience you are targetting, but for some of the important lines of code; you are going a little fast with lack or less of explaination.. For everything else, its a great tutorial..
@cyco3oy8 жыл бұрын
awesome presentation, thx a lot!
@KnightoftheSorryFace5 жыл бұрын
I just watch these cause it's cool to watch. I don't take anything in at all
@Sisyphus.5 жыл бұрын
i dont know are u sick or something if u say something is cool u think is good what u are saying dosent make sense
@KnightoftheSorryFace5 жыл бұрын
@@Sisyphus. I don't take anything at all. Is not me making a negative comment on it. I'm saying that the only reason I watch is because cool and that I don't take in any of the stuff
@Sisyphus.5 жыл бұрын
@@KnightoftheSorryFace man i forgot what u said before but i didn't mean like that but if something is cool for u it has to be good and u changed ur comment
@KnightoftheSorryFace5 жыл бұрын
@@Sisyphus. "You changed your comment" except it is clear that I haven't edited it at all if you'd look to see there is no mark to say it is edited
@RaviPal-pp2pc4 жыл бұрын
code till 7:07 is not working completely for me. Character do not follow the cursor but turn to the cursor points. can anyone help on it
@AurelianoShowsTheWorld3 жыл бұрын
Thx for this tutorial! I've reused it in my «Among Us Clone» pet project :) Trying to break into game dev.
@KeyTryer6 жыл бұрын
Great tutorial. This concept isn't very easy to find around.
@workshopoleg56614 жыл бұрын
I have an issue, Line is drawning while i am In the radius + Angle sight/ How can make scope vision only at view angle???
@horussolis95777 жыл бұрын
u r so much better than brackeys
@DawnosaurDev7 жыл бұрын
I think they are both really good!!
@horussolis95777 жыл бұрын
me too. brackey's is good but damn sebastian is in a whole other lague of his own
@tvash8367 жыл бұрын
And the 'Best comment of the day' ward goes to...
@purpleice23438 жыл бұрын
Yeah lists show up, the only time they don't is when you store classes in them, which aren't marked as serializable by default, well or anything else like that, I think colour also can't be serialized unless marked, and can't be saved into binary file.
@diliupg6 жыл бұрын
you are an excellent instructor. If you do a course at Udemy I will surely buy.
@blubehriluv4 жыл бұрын
I can't thank you enough
@omerfsa8 жыл бұрын
Thanks it's really usefull tutorial.
@joshsavage86597 жыл бұрын
Really like your videos!!
@tiagorodrigues_br5 жыл бұрын
Thanks, that tutorial is amazing!
@Epsio05 жыл бұрын
is the if statement at 17:12 actually correct? In my version of the project it seems to ignore the angle of the field of vision. What's happening is that the object I'm trying to test with could be behind the player yet it would still trigger it as being in the field of vision despite having this if statement in
@Chrisbrei25022 жыл бұрын
That's so cool! Thank you!
@Jaosen2 жыл бұрын
There is a change in the Unity Editor where you do not need to make an Editor folder for custom editors. If in your FieldOfView script you add "using UnityEditor;" and then make a separate class either inside the FieldOfView class or outside it will work all the same. Though if you want to be more organized then the Editor folder would be the way to go.
@Insajdo8 жыл бұрын
Hey, great work, I love your tutorials! One quick question, does this work with 2D perspective and sprites?
@SebastianLague8 жыл бұрын
+Insajdo Not out of the box, but it's relatively easy to adapt. (Requires changing some axes, using 2d raycast etc). I'll happily make a tutorial on getting it to work in 2D if it's something people are interested in/struggling with.
@Insajdo8 жыл бұрын
+Sebastian Lague I'll try it myself, thanks for your answer :)
@keyankenney8 жыл бұрын
I'd like to put in a +1 for a 2D version! Trying desperately to convert, but I'm struggling to get it right. All your tuts are awesome, love your work! Thanks.
@chntnshrff7 жыл бұрын
Hi, this was amazing, I am trying to use this in my 2D project... and I am stuck. Here is what I did... In Editor Script: Handles.DrawWireArc(fow.transform.position, Vector3.back, Vector3.right, 360, fow.viewRadius); In FieldOfView Script float angle = transform.eulerAngles.z - viewAngle / 2 + stepAngleSize * i; Getting angle from x and y axis public Vector3 DirFromAngle(float angleInDegrees, bool angleIsGlobal) { if (!angleIsGlobal) { angleInDegrees += transform.eulerAngles.z; } return new Vector3(Mathf.Sin(angleInDegrees * Mathf.Deg2Rad), -Mathf.Cos(angleInDegrees * Mathf.Deg2Rad), 0); } Used overlap circle instead of sphere Collider2D[] targetsInViewRadius = Physics2D.OverlapCircle(transform.position, viewRadius, targetMask); Changed all to Physics2D, RaycastHit2D and Collider 2D. I think I have missed something...your help will be much appreciated.
@JMYChannel7 жыл бұрын
hi! nice tutorials! I try this into 2D perspective, already try change the axes. But I can't find any target within radius. any suggestion?
@fern0926 жыл бұрын
At 7:17 my Character follows the mouse, but it does not rotate/turn. It keeps the same 90 degree angle. What am I doing wrong: Have I missed some code? I am using a standard mouse.
@fern0926 жыл бұрын
Setting the Global/Local tool handles to Local aligns to the Character itself.
@dracolichaАй бұрын
Puedo usar parte de tus video para explicar el concepto de FOV en mis proyectos?
@huntserston35795 жыл бұрын
Hey I just want to say I love this and plan on using this for custom 2d lighting just wondering if theres a way to make it effect sprites.
@asclepiiusunknown10904 жыл бұрын
Did you end up finding its uses as a light source? I'm trying to do the same and use it to achieve an effect like in Thomas Was Alone
@huntserston35794 жыл бұрын
@@asclepiiusunknown1090 I did! It sort of worked but requires alot of colliders
@asclepiiusunknown10904 жыл бұрын
Huntsers TON that’s awesome, thanks for actually replying
Very late to the party on this one, but would it be more effective to add a mesh collider trigger to your procedural mesh in order to detect enemy in vision instead of a raycast? That way you'd be able to see the edge of them instead of just the centre.
@adilsonv77 Жыл бұрын
Wonderful tutorial... it saved me a lot of time. Next, I have a question: when the object in the target mask is very close to the owner of the field of view, it is no longer able to perceive its presence... as well as, when it moves further away, it is no longer recognized. Any suggestions?
@redsan16-783 жыл бұрын
For those (like me) that do not get the FieldOfViewEditor script to work: input into the top of your script: " using UnityEditor", he has this option already enabled in his script. It was not enabled for me so Unity did not recognize the editor file handler.
@TheKr0ckeR Жыл бұрын
Great! How can we calculate Camera's rotation with the aim function? If our camera is not 90 degree; it will be changing our X value of rotation since camera is angled.