Enemy Line of Sight, Stealth Game in Unity (Field of View)

✅ Get the Project files and Utilities at unitycodemonkey.com/video.php...
Let's take our Field of View script and apply it to Enemies in order to create a nice Stealth game.
If you have any questions post them in the comments and I'll do my best to answer them.
🔔 Subscribe for more Unity Tutorials / @codemonkeyunity
See you next time!
🤖 Join the Community Discord / discord
📦 Grab the game bundle at unitycodemonkey.com/gameBundl...
📝 Get the Code Monkey Utilities at unitycodemonkey.com/utils.php
#unitytutorial #unity3d #unity2d
--------------------------------------------------------------------
Hello and welcome, I am your Code Monkey and here you will learn everything about Game Development in Unity 2D using C#.
I've been developing games for several years with 7 published games on Steam and now I'm sharing my knowledge to help you on your own game development journey.
You can see my games at www.endlessloopstudios.com
--------------------------------------------------------------------
- Website: unitycodemonkey.com/
- Twitter: / unitycodemonkey
- Facebook: / unitycodemonkey

Пікірлер: 81

  • @hishighness1842
    @hishighness18423 жыл бұрын

    This guy when he was starting this channel: Hmm let me show my genius to the world Honestly, this guys is just amazing, thank you

  • @lukaasg3476
    @lukaasg34764 жыл бұрын

    you are have the best tutorials what i ever seen.. Thanks!

  • @johnx140
    @johnx1404 жыл бұрын

    This is so entertaining and helpful duuuuude

  • @lineshpatil2484
    @lineshpatil24844 жыл бұрын

    You are such an inspiration for me!!

  • @qadka9521
    @qadka95213 жыл бұрын

    You helped me a lot!

  • @doctorbronze1595
    @doctorbronze15953 жыл бұрын

    For anyone who is making a stealth game that's like a side scroller or something simmilar, you don't need to calculate the angles and stuff in order to try and check if the enemy can see the player, you can just compare the x and y coordinates, using the following few equations: (check if enemy is in a 90 degree angle to the left) player.y - enemy.y -player.y + enemy.y (check if enemy is in a 90 degree angle to the right) player.y - enemy.y -player.y + enemy.y you can also obiously try to change those euqations, and try to make it so that it can detect the player in say a 180 degree angle or a 45 degree angle but this is pretty much the basics. note that this only works on side scrollers because the enemy only has 2 basic directions it can face, left and right.

  • @user-dg5uo9ht4v
    @user-dg5uo9ht4v4 жыл бұрын

    love this video!!

  • @royorgames771
    @royorgames7712 жыл бұрын

    Thank you for the tutorial, and the original one. Is there any way to avoid the set origin and set aim direction, so they use the Transform direction itself (as this would make it even cleaner)? I tried, but I am getting many distortions on the FieldOfView mesh and it is getting really complex.

  • @mr.fakeman4718
    @mr.fakeman47184 жыл бұрын

    I need more practise but I think I can get my head around it.

  • @klarnorbert

    @klarnorbert

    4 жыл бұрын

    Before you write any code, you should take a look at what you want to achieve.

  • @lukeybabes7287
    @lukeybabes72873 жыл бұрын

    Awesome tutorial, have you got any ideas on how you could detect the player if they are only partially visible? The ray cast is aiming at the centre of the player so if the centre is not obstructed then they can be seen, but what if you want to detect if just a leg or arm etc is sticking out from behind another object?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    You can do more than one raycast. Fire one towards the center, another one towards center + (1, 0) another towards center + (-1, 0), etc

  • @ezydenias8505
    @ezydenias85054 жыл бұрын

    I really would love this in a version for 3D games, tried to make it work in 3d but it just didn't do properly. Also because the 3d raycast works a little bit different.

  • @starduststudiosgaming971

    @starduststudiosgaming971

    Жыл бұрын

    did you ever find a method that worked?

  • @Pedro_Marangon
    @Pedro_Marangon4 жыл бұрын

    Hey, what do you do in GetAimDir() in the enemy?

  • @DavidRenntWeg
    @DavidRenntWeg4 жыл бұрын

    Can you make a simple tutorial on where two entities with health and dmg component fight each other with ECS? Would appreciate it a lot :D

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    Entity interaction is definitely a topic Id like to cover more. Check out the Marines Vs Zombies video where I talk about how I implemented it in there.

  • @DavidRenntWeg

    @DavidRenntWeg

    4 жыл бұрын

    @@CodeMonkeyUnity I already watched it around 10 times, but it was a bit too complicated for me with the queuedActions.

  • @ryancooper4874
    @ryancooper4874 Жыл бұрын

    I'm having trouble with the script. I'm using the parent objects euler angle of z for my rotation and the triangles rotate twice as fast then it should.For example if I'm at z rotation 45 my mesh would be facing 90 degrees while the raycasting is aiming at 45 degrees. Do you know any fix to this?

  • @StigDesign
    @StigDesign4 жыл бұрын

    Maby do a video on how to implement this in a 3D fps game enemy sight, maby you can use a hidden tapered cylinder/cone or tapered cube for better performance? :)

  • @folkenberger
    @folkenberger4 жыл бұрын

    You say the way its implemente it doesnt affect, but it does

  • @Pedro_Marangon
    @Pedro_Marangon4 жыл бұрын

    I'm having some problems with the code. I copied exactly like both videos the FOV.cs (the only changes I maded was putting some Debug lines/logs and changing the variable names to stay consistent with the rest of the project) but the graphics are pointing in one direction, while the actual detection is in another (and it detects through walls, even with the LayerMask). Could you help me?

  • @RTWrename
    @RTWrename3 жыл бұрын

    hey man, do you know how can I use a 2d renderer with this so I could apply 2D lighting?, I am asking because I see allot of tuts that tell me to use a 2D renderer, but I need the forwarder to make the fog effect

  • @zyous98
    @zyous982 жыл бұрын

    is there a way to implement this into a 3d plane? its still top down thus i thought this would work instantly but the mesh is always in the wrong axis, when giving value to vertex at 6:42, the equation gives values to the x axis and y axis, is there a way to change it from y axis to z axis?

  • @mfawtut3308
    @mfawtut33083 жыл бұрын

    Why when i play and i zoom my camera too close to enemy, this sight is gone, but still in hierarchy and when i zoom out can back again, but sometimes gone again and back again. How to fix this?

  • @MrGamer-rs3br
    @MrGamer-rs3br Жыл бұрын

    when doing this tutorial i couldn't get the "if player is behind the fov don't chase" part to work it was just chasing it when ever my player was in the view range and also I don't know what aimdirection is doing because you never showed it and how it works in this video I think

  • @arabiannights7183
    @arabiannights71833 жыл бұрын

    it keep saying validate your email to download the project file. i have validated over 100 times by now. i need the script

  • @zbeb118
    @zbeb118Ай бұрын

    Hello, for context I am making a hide and seek game. Got any tips on how I could go on and make this effect visible only when I want to be a seeker and not when I want to hide? It seems tricky as its on the renderer. Thanks! Edit:I kinda found a solution which is creating 2 cameras 1 for each player (hider, seeker) and 2 renderers 1 with the effect and one without (each camera are assigned the correct renderer) and when selecting hider I enable the hiders camera without the fov effect and vise versa

  • @min-nu5hj
    @min-nu5hj4 жыл бұрын

    Cool only one question is this can apply to mobile or will kill performance

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    The Field Of View script has a rayCount variable that you can easily edit. If you want to have a lot of units with the FoV active at once you can reduce the rayCount by quite a bit and the effect will still look very nice.

  • @wes5457
    @wes54574 жыл бұрын

    Sweet

  • @tyrussell3516
    @tyrussell35163 жыл бұрын

    would i be able to implement this into 3d? i would to make something like this in vr in unity!

  • @VezixHaikal
    @VezixHaikal3 жыл бұрын

    the script doesn't work, the mesh never renders infront of my grid/tilesets

  • @GabrielS87
    @GabrielS874 жыл бұрын

    First off great tutorials. I've been following along with a few others in my efforts to build a FoV tool. My question is in the 3D real and I apologize if you get this often. How can I get the FoV to work on vertical walls , terrain , holes in the ground etc ? Ex. If I have a Guard tower that is 10m tall and my protagonist needs to see his FoV and if he can see engage the Tower clearly I'd like for my FoV mesh to draw along side of the tower up to visualize that yes it's in his visual area. Same would go for a guard look down from the perch and what his FoV would be as well. Hope this makes sense and sorry if I confused you. Keep making great content.

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    If you want it to be a complete volume then you likely need to use a different approach, using Raycasts in a 3D volume will result in a monumental amount of Raycasts which will really destroy your framerate. Another approach is using Cameras to get what each enemy sees, it's a more complex approach but works in 3D Check out this article which talks about how they did it in Shadow Tactics kosmonautblog.wordpress.com/2017/01/09/shadow-tactics-rendering-breakdown/

  • @GabrielS87

    @GabrielS87

    4 жыл бұрын

    @@CodeMonkeyUnity thank you. Gives me something to look into.

  • @Elnino2014

    @Elnino2014

    4 жыл бұрын

    You can also shoot 2 Raycasts: 1 from enemy's chest Vector 3 origin for raycast will be (transform.position + transform.up (if height is 2 word units)) and 2 raycast try to shoot from head * by 2 transform.up. Check if raycast 1 && 2 is true then object is fully visible, if 1=false, 2= true - object is crouching and so on)

  • @thealexguy1486
    @thealexguy14863 жыл бұрын

    How do I now get an object to appear over the top of the whole thing?

  • @ginodekovic2733
    @ginodekovic27334 жыл бұрын

    Great turtorial! I know its been 6 months, but i have a problem. My LOS on enemy is never directly on the enemy, first it starts x = -10 relative to enemy while enemy moves, it floats to x = +10 relative to enemy. What did i mess up? Edit: for some reason he is drawing LOS from some other location

  • @BrandonCoulter

    @BrandonCoulter

    2 жыл бұрын

    I am having a similar issue, did you ever find a solution?

  • @92Roar
    @92Roar Жыл бұрын

    Dang, so close to the final effect! I've just got some problem with my cone not lining up with my "FindPlayer" function. It's off by like, 26 degrees or so and I can't figure out why! Alas, back to the drawing board let's see haha

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    Жыл бұрын

    Perhaps the game object itself is rotated? Add just 2 rays and some Debug.Log to see what the code is doing

  • @92Roar

    @92Roar

    Жыл бұрын

    @@CodeMonkeyUnity Ooh good call on the double Ray approach! I’ll jump on it and report back, maybe my insights will help someone else

  • @92Roar

    @92Roar

    Жыл бұрын

    @@CodeMonkeyUnity Finally got it! Re-wrote it twice before finding the (primary) culprit. I was setting my initial angle directly without taking the FOV into account and it was causing a plethora of knock on effects! SetAngle() and startingAngle = are not equivalent! Thanks for the swell tutorials! My stealth game hybrid wouldn't be off the ground without it!

  • @phannhat9506
    @phannhat95063 жыл бұрын

    Hey man, please help me attach this to my character in 2d platformer, I can not make it to work

  • @teddyGros
    @teddyGros3 ай бұрын

    Can you show us how to do this for 3d games?

  • @zakwanmasud2406
    @zakwanmasud24064 жыл бұрын

    Hello Sir, while downloading the files, some of the assets become pink. Can you please help me out? Thanks. If anyone can solve this problem, please help me out.Thank you :)

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    Either you don't have a material assigned to a mesh or you have a shader error

  • @extrahammer5735
    @extrahammer57354 жыл бұрын

    my Raycast will cast but it wont detect the player for some reason. Any tips?

  • @jeremiahmiranda431

    @jeremiahmiranda431

    3 жыл бұрын

    I am having the same issue, any luck fixing?

  • @jeremiahmiranda431

    @jeremiahmiranda431

    3 жыл бұрын

    Physics2D.queriesStartInColliders = false; try placing in Start()

  • @jeremiahmiranda431

    @jeremiahmiranda431

    3 жыл бұрын

    worked for me!

  • @MaximEyes

    @MaximEyes

    2 жыл бұрын

    @@jeremiahmiranda431 You're a lifesaver mate. I had no idea why it wasn't working and this insta-fixed it! Thank you so much!

  • @jeremiahmiranda431

    @jeremiahmiranda431

    2 жыл бұрын

    @@MaximEyes Hey thank you for comment! Also look more into LayerMask.GetMask() and try setting things up using that struct from UnityEngine.LayerMask , it will help specify layers to use in a raycast.

  • @nvbchhgjhg8640
    @nvbchhgjhg86403 жыл бұрын

    how about next level tutorial plss

  • @produccionesr7168
    @produccionesr71683 жыл бұрын

    Failed to import package with error: Couldn't decompress package

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    What Unity version are you using? Make sure you use the same or later as used in the video

  • @mokigames1818
    @mokigames18184 жыл бұрын

    Piece of cake.

  • @OAlexisSamaO
    @OAlexisSamaO3 жыл бұрын

    how does they have enough money to pay armed security but not for some lights

  • @planetdilien2932
    @planetdilien29324 жыл бұрын

    What would 480 degrees be like?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    That would be a bit less than a complete circle. When making super wide FOVs make sure you pick the correct rayCount amount

  • @92Roar
    @92Roar Жыл бұрын

    Hello! It's me again, coming right at ya with the inquisitive questions! So this works perfectly fine in editor yet for some reason when I build it, it grinds to an absolute halt and barely runs at all? In my own implementation and the version I downloaded from your website. Is there some physics setting I'm missing since I believe the culprit is the FindTargetPlayer function and it's checks, when that's disabled it runs fine

  • @92Roar

    @92Roar

    Жыл бұрын

    Aha! I figured it out! Not sure if anyone else will come across this problem BUT turns out, my Debug.Logs in the find player/cone functions to figure things out were EXPLODING everything in build form, no real clue why but there you go! If you're having massive lag spikes in builds, try comment out all your Debug.Logs!

  • @horizon_gamedev
    @horizon_gamedev2 жыл бұрын

    Any way one enemy can detect another dead enemy?

  • @92Roar

    @92Roar

    Жыл бұрын

    You should be able to use the exact same code pretty much! But instead of raycastHit2D.collider.gameObject.GetComponent() != null you replace Player with whatever script is on your enemy that you want to call a function for

  • @sniperisaac1268
    @sniperisaac12684 жыл бұрын

    Code Monkey JFrame game 2d

  • @liaxo6555
    @liaxo65554 жыл бұрын

    The enemies looks like purples blobs - can you help?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    Check their material

  • @liaxo6555

    @liaxo6555

    4 жыл бұрын

    @@CodeMonkeyUnity It has mask material which is just purple.

  • @thitibhumchaisrisawatsuk5181
    @thitibhumchaisrisawatsuk51814 жыл бұрын

    Code monkey where do you get enemy movement

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    I covered Pathfinding here kzread.info/dash/bejne/k6CJkpahpt2cfJc.html

  • @DerAbsender

    @DerAbsender

    3 жыл бұрын

    @@CodeMonkeyUnity Thank you, I am alreadyusing the ready to use Asset, but for the moment with that asset I have two problems: First the Field of View cone is changing the aim direction verry erratically, which is ugly at the minimum. Second, which is more game breaking, the enemy can still see through walls. I will build you Version of the algorithm, use it with the described field of view of yours and hope that does the trick already. But if not I hope you can help a little.

  • @user-nd7go5bw4r
    @user-nd7go5bw4r7 ай бұрын

    2:31 what is vObject?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    7 ай бұрын

    That's my custom object class for how I handle my animation system, you can just work with transforms instead

  • @user-nd7go5bw4r

    @user-nd7go5bw4r

    7 ай бұрын

    🐵

  • @ReyBanYAHUAH
    @ReyBanYAHUAH3 жыл бұрын

    Always remember to repent of your sins (sin is transgression of YAHUAH’S LAW: Genesis, Exodus, Leviticus, Numbers, & Deuteronomy) And Have Belief On YAHUSHA HAMASHYACH. HE Died and Rose Again On The Third Day So that you can be forgiven of your sins! HE Loves you! Come to HIM!🙂🙂🙂🙂🙂🙂🙂🙂🙂🙂🙂🙂🙂

  • @arabiannights7183
    @arabiannights71833 жыл бұрын

    your site system is not just bad but worst i have ever sceen. not validating, asking to log in when i logged in it just does nothing to download project file. you lost my trust on your site to make any future purchase

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    My website does not handle any purchases, all my courses are hosted on Udemy. Check your spam folder, it's not my fault if your email provider rejects the validation email, I can't force it to be accepted. Add contact@unitycodemonkey.com to your whitelist.

  • @arabiannights7183

    @arabiannights7183

    3 жыл бұрын

    @@CodeMonkeyUnity how do you make games with this brain? i said when i click the link it takes to your site and gives message "Invalid link". if i didnt receive your email how did i click the link? it does not click your brain? there is nothing to do with spam folder. your email came to my inbox with no problem. anyway doesnt matter. the useless utility you gave is full of code money, code monkey lines...

  • @Cortus

    @Cortus

    3 жыл бұрын

    @@arabiannights7183 You're such an entitled little prick. "It does not click your brain? " that he's giving away so much for free? The least you could do is be nice. You don't deserve any of his time or help. Sorry you're too much of a moron to make a simple email sign up work to get free stuff lol