Тёмный

How to Add a Field of View for Your Enemies [Unity Tutorial] 

Comp-3 Interactive
Подписаться 16 тыс.
Просмотров 85 тыс.
50% 1

Enemies usually have eyes, right? So lets give your enemies some vision! In this video we're going to add a really simple but really effective field of view, field of vision, cone of view, line of sight... Whatever you want to call it, to your enemies!
We're also going to make sure that the enemies can't see you through walls and AS A BONUS! We're going to create an Editor utility so we can see the field of view bounds in the scene view!
Download the scripts here: github.com/Comp3interactive/F...
Join me and learn your way through the Unity Game Engine, the C# language and the Visual Studio editor. Remember, if this video was useful then DROP A LIKE! 👍
💯 Want to help me out and allow me to keep making these tutorials? Consider supporting the channel on Patreon:
/ comp3interactive
😍 AWESOME high quality 3D game assets available here:
shop.runemarkstudio.com/?ref=...
🤩 SPONSORED LINKS:
www.gigatank3000.com
/ gigatank3000
💬 Join the Discord community here:
/ discord
📱 Find us on social media for more Tips and Tricks:
/ comp3interactive
/ comp3interactive
/ comp3int
📱 Play our games for FREE here on Google Play:
bit.ly/2TisAQo #UnityTutorial #Unity3D #Comp3interactive #GameDev

Опубликовано:

 

18 фев 2021

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 219   
@its_just_matt
@its_just_matt 3 года назад
This tutorial has the best thumbnail I've ever seen.
@Ophelos
@Ophelos 3 года назад
Nothing like a little bit of trigonometry programming in the middle of a friday afternoon. Thanks for the video.
@comp3interactive
@comp3interactive 3 года назад
Did I mention I hate maths... 😂
@revoxmediaHD
@revoxmediaHD 2 года назад
@@comp3interactive luckily for you it wasn’t your maths or code but Sebastian Leagues
@hi-its-matt
@hi-its-matt Год назад
@@revoxmediaHD wdym, did he copy code?
@revoxmediaHD
@revoxmediaHD Год назад
@@hi-its-matt Copied it exactly
@adriatical9016
@adriatical9016 Год назад
woah, it's friday afternoon for me
@its_Wooddy
@its_Wooddy 2 года назад
Thank you so much for this tutorial! Really cool use of the gizmos for the FOV and very well communicated!
@jeffreydotson4842
@jeffreydotson4842 2 года назад
My kind of tutorial! Clear and to the point without excessive stammering and/or over explaining.
@paulberry3359
@paulberry3359 3 года назад
Really enjoyed this tutorial, simple and powerful and Modular! Since doing your fps tutorial really motivated me to try other things and see what I can put together !
@Bobster536
@Bobster536 Год назад
I get so drawn into your voice, that i forget that im watching this tutorial to actually learn and just sit through till the end... Getting through one of yours to the point where id be satisfied enough with understanding the concepts took me ~6 rewatches. Great content my man love you
@JimmyJohansson-pj1zx
@JimmyJohansson-pj1zx 3 месяца назад
Good programming tutorials are rare, so whenever I find videos like this I appreciate it a lot.
@lukastomasek8038
@lukastomasek8038 3 года назад
Great tutorial ! I would like to see more videos on custom editors for sure .
@Folghan
@Folghan Год назад
Exactly what I was looking for. Great job man, I've tried everything and couldn't make it work, and your video saved me from seppuku.
@DanielGarcia-ze9vy
@DanielGarcia-ze9vy 2 года назад
Actually the best tutorial on enemy AI I have ever seen so far! I've been wondering so long for help on making raycast detection, thanks a lot!
@bakedpizza4037
@bakedpizza4037 2 года назад
I am new to unity ... I am bit confused where to tell the enemy to do chase player or run any command on detection... Pls tell if you know
@welcomelittlefellow
@welcomelittlefellow 2 года назад
Just what I was looking for, thank you very much. Clear explanation!
@overcookedwalrus4482
@overcookedwalrus4482 Год назад
Amazing video! Thankyou for holding our hands through this!
@Demented_Chaos
@Demented_Chaos Год назад
Man's out here saving my project with expert guidance, and now I know at least five new things about Unity and C#.
@azrhyga
@azrhyga Год назад
Thanks very much for making this tutorial!! It is really helpful!! Thanks to this tutorial my enemies now have a field of view so they can detect the player more naturally :D
@genofelice6034
@genofelice6034 Год назад
Thanks. This was the most useful tutorial I've watched in a long time.
@ASeggsyPotat
@ASeggsyPotat 2 года назад
It took me a minute to get everything working, but this was exactly what I was looking for! Now I just need to combine this with a few other tutorials (Making the enemy walk around, making the enemy walk towards the player etc) and I'll have the logic for my very first game done! Thanks a billion!
@Rize-TheArtOfPlay
@Rize-TheArtOfPlay Год назад
Same. Combining the scripts is giong to be the hardest part I guess. Hope you had success!
@TwistdRabbit
@TwistdRabbit 2 года назад
DUDE!!! I am making a VR fps with my 11y/o son as a summer project between college and Uni and you just saved me weeks of stress!!
@ANGELB3AR
@ANGELB3AR Год назад
Fantastic tutorial :) I'll definitely be checking out your other videos for more content like this
@AJMarraffa
@AJMarraffa 2 года назад
Excellent tutorial, thank you! It was very easy to integrate into the code I had already written.
@washynator
@washynator 3 года назад
Awesome video and clear explanation! Thanks man!
@agnarzb
@agnarzb 2 года назад
Great tutorial. Very clean and to the point explanation. Would like to add that even it is not main concern of the video but I love the idea to add the editor part as well. Cheers.
@unityvictor787
@unityvictor787 3 года назад
Thanks for tutorial, Very simple to understand!!
@Dosterczuk1321
@Dosterczuk1321 Год назад
AMAZING! Thank you for sharing it! Works perfectly good!
@muriilouwu
@muriilouwu 2 месяца назад
Thank you for the video, I used this logic to implement an aim system in my game :)
@imeldaweijers3284
@imeldaweijers3284 Год назад
Just what I needed, thank you so much for this little gem.
@camelChase20
@camelChase20 3 года назад
This was perfect and immediately applicable for my AI controllers, thank you for posting!
@aussieraver7182
@aussieraver7182 2 года назад
This is beautiful! Thanks for the tutorial!
@andreamansi2441
@andreamansi2441 2 года назад
Damn, thanks, you saved me A LOT of time to build a decent AI visual detection algorithm for my university assignment. Clean and elegant implementation, easy to use (actually it cost me around 2 minutes ahah). Simple but powerful script! You should actually sell it, not joking. Thanks!
@thebeeshu771
@thebeeshu771 Год назад
This is a 10/10 tutorial. It's not the simplest thing to make, but everything is so well explained and the editor script is the cherry on top of the cake.
@chimz8057
@chimz8057 Год назад
Awesome script my man! Thank you!
@user-pp5lp8jy2l
@user-pp5lp8jy2l 4 месяца назад
Hi. This tutorial is really fantastic thanks!
@DTNenforcer
@DTNenforcer Год назад
Very well explained. Thank you very much for an excellent tutorial.
@HarrenTonderen
@HarrenTonderen 3 месяца назад
Much thanks, been struggling to find something like this ❤
@krakuwdegustacja
@krakuwdegustacja 2 года назад
best tutorial on enemy fov i've found
@kyrollosaziz7519
@kyrollosaziz7519 2 года назад
absolutely need videos on custom editor tools with, it would be very useful to watch tutorials with such important topic in your great and simple explanation way.
@ImFrantic
@ImFrantic 2 года назад
If anybody wants to know how to deal with multiple objects, here it is ( *in my case "playerRef" is "enemyUnits"* ): void FieldOfViewCheck() { Collider[] rangeChecks = Physics.OverlapSphere(transform.position, visionRadius, targetMask); if (rangeChecks.Length != 0) { for (int i = 0; i
@ImFrantic
@ImFrantic 2 года назад
@@Shadowjones You're very welcome mate ^^ That's the reason I posted this here. Enjoy :D
@whitedragon44
@whitedragon44 2 года назад
@@ImFrantic Really thank for this but I have one error. Cant use i++ in this line. for (int i = 0; i
@ImFrantic
@ImFrantic 2 года назад
@@whitedragon44 is your rangeChecks an array? If it's not "Collider[] rangeChecks" it can't work because there is nothing to count.
@whitedragon44
@whitedragon44 2 года назад
@@ImFrantic Collider[] rangeChecks = Physics.OverlapSphere(transform.position, radius, targetMask); You mean this?
@kioka2
@kioka2 Год назад
Thanks! How do I detect the closest object among all those detected?
@corneliusbuckley8897
@corneliusbuckley8897 2 года назад
Thanks for doing the maths for me! Such a simple yet super useful tutorial, I appreciate your work.
@comp3interactive
@comp3interactive 2 года назад
And thank you for calling it "maths" and not "math" 😂
@netanelkomm5636
@netanelkomm5636 8 месяцев назад
Works like a charm, thank you!
@olcaval
@olcaval 2 года назад
Thank you so much! You explained it perfectly. Now my enemy AI is finnaly working.
@AdamBarkerThe
@AdamBarkerThe Год назад
Excited for your game next week!
@unDeadGod69
@unDeadGod69 2 месяца назад
Really helpfull tutorial, thanks for uploading it
@kitsunedotexe3587
@kitsunedotexe3587 2 года назад
currently using this tutorial for a really strange mechanic xD haha thanks!
@nugraha000
@nugraha000 Год назад
thank yu so much, u help me a lot bro. keep it up.👍👍
@miikajuhala6748
@miikajuhala6748 Год назад
Thank you so much for nice and clear video!
@QuantumChord
@QuantumChord Год назад
Thank you for the tutorial, this should help me with designing enemy AI.
@pro100baobab
@pro100baobab 4 месяца назад
Good job! Thanks!
@mindergamestudios
@mindergamestudios 11 месяцев назад
I'm glad i found your channel.
@TheKr0ckeR
@TheKr0ckeR 2 года назад
Thanks! It was really helpful! Both for Editors and raycasting! I would love to see Custom Editor scripts in the future! Is there anyway to visualize this angles in game too? So that its like a flashlight of enemies. :)
@coloneljcd6041
@coloneljcd6041 Год назад
Great vid! Liked and subbed
@mrnickhox
@mrnickhox 2 года назад
Saved my live thank you so much.
@Vasquez2701
@Vasquez2701 2 года назад
Thank you very much! Saludos desde Guatemala.
@12edcorea54
@12edcorea54 11 месяцев назад
very nice guide!
@carpe.die.m
@carpe.die.m 2 года назад
MULTIPLE TARGETS CHECK Hi, I don't know if this is 100% correct but it is just my explanation of what was suggested in the video for detecting multiple targets. "FieldOfView" script: 1) Start( ) method: delete/comment out the "playerRef = GameObject.FindGameObjectWithTag("Player");" line -----> this reference is used just in the Editor script for drawing the Gizmo line but doesn't actually have to do anything with the detection of the target. 2) in the FieldOfViewCheck( ) function , as the author said in the video 8:32, the rangeChecks array holds anything that has the targetMask on it and was detected in the radius around your player/character. Then we get one of these detected targets from this array and calculate in what direction this target is and finaly in the if statement with the Vector3.Angle, we calculate if it is in our FOV angle (if it is in the see distance, was automatically calculated by putting targets in our radius in the rangeChecks array). -----> So, here we just have to check this for each of the detected targets in the rangeCheck array. 3) Create a for loop looping through all rangeCheck elements -----> after "if(rangeChecks.Length != 0)" and before "Transform target = rangeChecks[0].transform;". So, we can look at each element in rangeChecks and find out if we see it or not 4) Change "Transform target = rangeChecks[0].transform;" to "Transform target = rangeChecks[i].transform;" -----> So, we are able to calculate stuff for every element in the rangeChecks one-by-one 5) Inside the if statement where we set "canSeePlayer = true", assign the detected target gameObject from rangeChecks to our "playerRef" by adding "playerRef = rangeChecks[i].gameObject;" -----> So, we know we see a player/target/enemy and in the Editor script we can draw the gizmo line 6) add "break;" under assigning the playerRef gameObject -----> this should stop the for loop calculating stuff for the remaining found targets in the rangeCheck[], because we found (at least) one player/enemy/target that we can see. But this doesn't have much impact since it breaks just this one for loop and the next time this method will be called in the coroutine this for loop will run again. Also, this has to be there if you are going to do the remaining steps! 7) Inside the if statements where we set "canSeePlayer = false" write "playerRef = null" there (even to the very bottom else "if(rangeChecks.Length != 0){ }else{...here...}") -----> making sure PlayerRef doesn't hold any gameObject that we don't see anymore. So, the gizmo line is not drawn if there is no player/target/enemy in our sight. Please, read the coments to this (if there are any), maybe I made a mistake or didn't explain something in the correct way. Hope this helps and thank you for the great video, it has significantly improved my project and I learned a lot.
@ImFrantic
@ImFrantic 2 года назад
Hey, your example worked fine. One thing I had to change was: after the *for loop* to check our objects we can no longer say -"Transform target = rangeChecks[i].transform"- since "i" can't leave the for loop. Instead we have to just do *"Transform target = playerRef"* since it was already changed in the forr lopp it's possible.
@ImFrantic
@ImFrantic 2 года назад
In my Case *"playerRef"* is *"enemyUnits"* but anyways here's the finished code: void FieldOfViewCheck() { Collider[] rangeChecks = Physics.OverlapSphere(transform.position, visionRadius, targetMask); if (rangeChecks.Length != 0) { for (int i = 0; i
@jehdiaizon
@jehdiaizon 2 года назад
amazing! thank you!
@raphaelrtf
@raphaelrtf 3 года назад
OMG! You saved me! Thank you so much!
@comp3interactive
@comp3interactive 3 года назад
I got you bro 😉
@saintpatronum921
@saintpatronum921 11 месяцев назад
good job man!
@kioka2
@kioka2 2 года назад
Very good, I learned a lot from your video, thanks for clarifying how to create a FOV! I have a doubt.. how to detect multiple players within the FOV and choose the closest player?
@hewston1
@hewston1 3 года назад
excellent, thank you
@dopplegangerdavid
@dopplegangerdavid 2 года назад
Thanks for the info, Sean Connery.
@pixvee341
@pixvee341 3 года назад
Thanks you're AWESOME :)
@piyushdamania2087
@piyushdamania2087 3 года назад
Thanks a ton !
@parvane8608
@parvane8608 2 года назад
Amazing Thank you
@Mikelica69
@Mikelica69 3 года назад
Thanks a lot ! 🙏🙏
@troyna77
@troyna77 Год назад
This tutorial is better than some unity store assets. why ? 2 reasons: 1)you get to know the "nuts and bolts" of exactly what is going on. 2) and you get the code explained. With a little bit of tweaking, I added my own shooting component and basic health system components. Still trying to figure out how to represent the "fov" during runtime. but thanks !!!
@lerryotter1750
@lerryotter1750 Год назад
I have a problem converting this Script from 2d to 3d, i changed Vector 3 to 2 and forward to right burt it is still on the wrong axis...
@endxrseeker5051
@endxrseeker5051 3 года назад
My ai can still see me through walls, it shows the green line going through the walls. I have correctly labled and assigned the correct layer titles. Pls help
@patorino5295
@patorino5295 10 месяцев назад
Thanks for the awesome tutorial. I followed your coding, and I noticed something. Should it be transform.forward in the angle check?
@brianjirvin
@brianjirvin Год назад
So followed this tutorial and works mostly, however the distance before player becomes unseen is roughly double the radius
@halivudestevez2
@halivudestevez2 Год назад
Pretty advaced style tutorial. Just a bit of idea: at fov check, you can avoid the "else" hell spaghetti, if you set the canSee to false 8n the beginning and set again to true at positive check.
@davidbarnaba7229
@davidbarnaba7229 Год назад
I Ecounterred an issue. I'm using the code on a 2D game, just a couple of differences (vector2 insted of 3 and transform.right instead of forward). When the player is really close to the enemy (with pov) but still in the cone of view, canSeePlayer become false. Can anyone help please?
@lukdev
@lukdev Год назад
GOLDEN!
@javednoman3051
@javednoman3051 2 года назад
nice work, how to use it in case enemy is above or some ledge, and it can see player down or up, i short a 3d cone type field of view mechanic
@SystembolagetOfficial
@SystembolagetOfficial 2 года назад
Ive encountered a problem with this script that doesnt seem to have an clear answer. Whenever my enemy looses sight of my player it just stops and starts to shake in different directions. Like it doesnt seem to find the exact spot my player was on. Another thing is that the enemies path-find perfectly to my player while playing the game in the Unity Editor, but as soon as i export a build of the game, the enemies just kinda randomly decide where to go once the player has been spotted. They dont run off tho, they just kinda waddle around the player.
@AlexandreFPS_
@AlexandreFPS_ 3 года назад
Helo! I have a problem, I have a bot script already done (shoot and run) but I put the field of view of your video and it didn't work, I use the Nav Mesh Agent to follow the player, I wanted the bot to stay put and if the player stayed within his field of vision, he shoots, can you help me? I thank!
@slowman8385
@slowman8385 3 года назад
Awesome video!!! Can u make a video about enemy hearing range?
@comp3interactive
@comp3interactive 3 года назад
Dude, it's already on my to do list! 😂 Now I just need to find a celebrity with weird ears for the thumbnail 🤔
@lucasmattos5727
@lucasmattos5727 Год назад
Any way to have a solid color in the radius FOV?
@IvBallestero
@IvBallestero Год назад
I have an issue, sometimes the script detects the player and sometimes not, plus I had to comment the Editor Script part where player is spotted, as the Handles.DrawLine gives me an error that says: You're pushing more GUIClips than you're popping. Any solution or anyone that has the same problem?
@schizoscope8703
@schizoscope8703 5 месяцев назад
It's detect position of collider but not collider itself. How can I change the script to fix it?
@Bobster536
@Bobster536 Год назад
I know its bit silly when just copying the code, but ive messed something up and was spending like 10 minutes looking at this one file of code and some settings, only after forfeiting and going back to your video i realised i had everything setup perfectly, just didnt start the coroutine
@AliceMonUnitydeveloper
@AliceMonUnitydeveloper 2 года назад
Thanks!
@nulltensor
@nulltensor 3 года назад
Excellent tutorial. Very clear and well communicated. One minor nitpick though: "euler" is pronounced "oil er" rather the "you ler". It's a reference to the Swiss mathematician Leonhard Euler of Euler Identity fame.
@comp3interactive
@comp3interactive 3 года назад
Ah that's interesting, I'll be honest, didn't know that was the pronunciation (obviously). You-ler is pretty well engrained into me now so that might be a hard habit to break 😂
@rudefatty1766
@rudefatty1766 11 месяцев назад
🤓🤓🤓
@giacomociaramella6908
@giacomociaramella6908 11 месяцев назад
Does the physics have better performance over the mesh filter method?
@Bobster536
@Bobster536 Год назад
If i didnt know of your FOVRoutine() solution, I would use a function inside of MonoBehaviour::Update(), and make the function start with a if(bool) which gets set to false for x frames after function does its job. Your solution seems just much more clear, if im getting it right.
@berdikopgroudli1234
@berdikopgroudli1234 Год назад
Hello! I have an AI programmed using this raycast and some path script. The AI works great, but im wondering. Would it be possible to add a searchtime, so when the enemy spots you, and then looses you, he will still go after for lets say 3 seconds? Because when my enemy spots you, and then you hide, he instantly switches to his coded path I have made. Thus making the game really easy(you lose if he catches you) I have tried to add an Ienumerator, but that didnt work.
@wortwortwort117
@wortwortwort117 Год назад
How exactly can this be tweaked for multiple targets... 8:23 You mention adding a for loop but im unsure of how. I've added a for loop to determine the closest collider from the list of all colliders in range, but it will still only seek one regardless of if its visible. If i have 2 targets the enemy will lock onto the closest targe5 even if its behind a wall. The 2nd target can be entirely visible but will be ignored.
@BF-sl5uj
@BF-sl5uj 8 месяцев назад
I keep getting "The name 'angleInDegrees' does not exist in the current context" for the FieldOfViewEditor script. Is anyone else running into this problem? I'm using Unity 2021.3
@Bobster536
@Bobster536 Год назад
I was nearing the end of this tutorial and my player object is invisible on scene view, I didnt click the eye icon to make it so... big mess i could only assume some part of the scripts i wrote before messes up and modifies my player object transform to make it lose visibility or make it small to the point where i cannot see it, still great tutorial
@trentonweber4729
@trentonweber4729 2 года назад
How would you convert this to 2D?
@user-qg1kd4zi3g
@user-qg1kd4zi3g 2 года назад
I don't see the green line even though the bool canSeePlayer is enabled.
@satriare5107
@satriare5107 2 года назад
Help! I can't export my game with that. How to solve it?
@flamyboy8043
@flamyboy8043 2 года назад
i followed everything but my enemy still doesnt see my character ;-; (i enven checked the FAQ and still no dice)
@Bobster536
@Bobster536 Год назад
yield return got me very confused. I would assume return means quit out of this function, but yield return means: pause execution of this code for x amount of frames.
@thedrunkenknight
@thedrunkenknight Год назад
I want to have the enemy remember where it last saw the player, but it cant get it to implement. Can someone help?
@tobiasnilssenopedal6660
@tobiasnilssenopedal6660 Год назад
i got an error while making the editor script, " 'Handles' does not contain a definition for 'DrawWireArch' "
@xtheflash9313
@xtheflash9313 2 года назад
I dont know why, but the scene ist freezing when my target (Player) gets into the field of view of the enemy...
@tuseroni6085
@tuseroni6085 Год назад
what's the point of the playerRef?
@rakyb12
@rakyb12 Год назад
Hey, can this be applied to 2D??
@flipthebird6355
@flipthebird6355 2 года назад
Thanks you
@alvinopingvino4584
@alvinopingvino4584 2 года назад
Are we allowed to use this in titles that we are gonna release??
@arkhangelgeos120
@arkhangelgeos120 Год назад
i copied all the code word tp word, didnt work. I then copied the github link code, didnt helped either, if i dont check "canSeePlayer" myself line to player isnt drawing, and obsticruction isnt working too. Anyone had the same problem?
Далее
How to Make a Good 2D Camera
11:38
Просмотров 392 тыс.
How to ACTUALLY get into Gamedev
14:01
Просмотров 702 тыс.
World Record Tunnel Glide 🪂
00:19
Просмотров 23 млн
This or That 🛍️
00:52
Просмотров 6 млн
Coding Adventure: Portals
16:06
Просмотров 1,3 млн
Giving Personality to Procedural Animations using Math
15:30
Dear Game Developers, Stop Messing This Up!
22:19
Просмотров 695 тыс.
AI Learns What Pizza Is
8:03
Просмотров 344 тыс.
How to code SMARTER A.I. enemies | Unity Tutorial
32:49
Unity line of sight checking using sensors [AI #08]
19:24
3 Hours vs. 3 Years of Blender
17:44
Просмотров 4,3 млн
Can AI code Flappy Bird? Watch ChatGPT try
7:26
Просмотров 9 млн
World Record Tunnel Glide 🪂
00:19
Просмотров 23 млн