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

Поделиться
HTML-код
  • Опубликовано: 27 сен 2024
  • 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/Com...
    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.runemarks...
    🤩 SPONSORED LINKS:
    www.gigatank30...
    / 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

Комментарии • 221

  • @its_just_matt
    @its_just_matt 3 года назад +31

    This tutorial has the best thumbnail I've ever seen.

  • @Ophelos
    @Ophelos 3 года назад +77

    Nothing like a little bit of trigonometry programming in the middle of a friday afternoon. Thanks for the video.

    • @comp3interactive
      @comp3interactive  3 года назад +18

      Did I mention I hate maths... 😂

    • @revoxmediaHD
      @revoxmediaHD 2 года назад +3

      @@comp3interactive luckily for you it wasn’t your maths or code but Sebastian Leagues

    • @hi-its-matt
      @hi-its-matt 2 года назад +1

      @@revoxmediaHD wdym, did he copy code?

    • @revoxmediaHD
      @revoxmediaHD 2 года назад

      @@hi-its-matt Copied it exactly

    • @adriatical9016
      @adriatical9016 2 года назад

      woah, it's friday afternoon for me

  • @TwistdRabbit
    @TwistdRabbit 2 года назад +5

    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!!

  • @ASeggsyPotat
    @ASeggsyPotat 2 года назад +23

    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 2 года назад +2

      Same. Combining the scripts is giong to be the hardest part I guess. Hope you had success!

  • @JimmyJohansson-pj1zx
    @JimmyJohansson-pj1zx 5 месяцев назад

    Good programming tutorials are rare, so whenever I find videos like this I appreciate it a lot.

  • @carpe.die.m
    @carpe.die.m 2 года назад +4

    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 года назад +1

      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

  • @Bobster536
    @Bobster536 2 года назад

    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

  • @ImFrantic
    @ImFrantic 2 года назад +10

    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 года назад +1

      @@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?

  • @muriilouwu
    @muriilouwu 4 месяца назад

    Thank you for the video, I used this logic to implement an aim system in my game :)

  • @lukastomasek8038
    @lukastomasek8038 3 года назад +10

    Great tutorial ! I would like to see more videos on custom editors for sure .

  • @jeffreydotson4842
    @jeffreydotson4842 2 года назад

    My kind of tutorial! Clear and to the point without excessive stammering and/or over explaining.

  • @AdamBarkerThe
    @AdamBarkerThe Год назад

    Excited for your game next week!

  • @Folghan
    @Folghan 2 года назад

    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.

  • @nulltensor
    @nulltensor 3 года назад +2

    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 года назад +1

      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 Год назад

      🤓🤓🤓

  • @paulberry3359
    @paulberry3359 3 года назад +2

    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 !

  • @agnarzb
    @agnarzb 2 года назад +1

    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.

  • @HarrenTonderen
    @HarrenTonderen 5 месяцев назад

    Much thanks, been struggling to find something like this ❤

  • @genofelice6034
    @genofelice6034 Год назад

    Thanks. This was the most useful tutorial I've watched in a long time.

  • @elric7371
    @elric7371 3 года назад +12

    Good tutorial. Noticed an bug, when the player is slight above the enemies fov radius (still in the angle). It cannot detect player.

    • @SetharofEdgarDead-Rose
      @SetharofEdgarDead-Rose 2 года назад +1

      needs to add Verticle element to this, or this only works on a flat ground game! Otherwise, this is an Ace Tutorial!

    • @somustache
      @somustache 2 года назад

      It's probably because the radius is spherical and not circular for vertical movement. idk

    • @welcomelittlefellow
      @welcomelittlefellow 2 года назад +2

      For future reference, you could try to set the y value on the transform.position and rangechecks[0].transform in the FieldOfViewCheck to make sure that the ray triggered higher above the ground (make sure to create new vector3 objects when doing that as you do not want to manipulate the gameobject transform, only for the check).

  • @Dosterczuk1321
    @Dosterczuk1321 Год назад

    AMAZING! Thank you for sharing it! Works perfectly good!

  • @washynator
    @washynator 3 года назад +1

    Awesome video and clear explanation! Thanks man!

  • @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.

  • @netanelkomm5636
    @netanelkomm5636 10 месяцев назад

    Works like a charm, thank you!

  • @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!

  • @imeldaweijers3284
    @imeldaweijers3284 Год назад

    Just what I needed, thank you so much for this little gem.

  • @corneliusbuckley8897
    @corneliusbuckley8897 2 года назад

    Thanks for doing the maths for me! Such a simple yet super useful tutorial, I appreciate your work.

    • @comp3interactive
      @comp3interactive  2 года назад +2

      And thank you for calling it "maths" and not "math" 😂

  • @camelChase20
    @camelChase20 3 года назад

    This was perfect and immediately applicable for my AI controllers, thank you for posting!

  • @pro100baobab
    @pro100baobab 6 месяцев назад

    Good job! Thanks!

  • @AJMarraffa
    @AJMarraffa 2 года назад

    Excellent tutorial, thank you! It was very easy to integrate into the code I had already written.

  • @lerryotter1750
    @lerryotter1750 Год назад +1

    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...

  • @ANGELB3AR
    @ANGELB3AR Год назад

    Fantastic tutorial :) I'll definitely be checking out your other videos for more content like this

  • @matzo3679
    @matzo3679 2 месяца назад

    Thank you brother

  • @DTNenforcer
    @DTNenforcer Год назад

    Very well explained. Thank you very much for an excellent tutorial.

  • @aussieraver7182
    @aussieraver7182 2 года назад

    This is beautiful!
    Thanks for the tutorial!

  • @dopplegangerdavid
    @dopplegangerdavid 2 года назад

    Thanks for the info, Sean Connery.

  • @Vasquez2701
    @Vasquez2701 3 года назад

    Thank you very much! Saludos desde Guatemala.

  • @nugraha000
    @nugraha000 Год назад

    thank yu so much, u help me a lot bro. keep it up.👍👍

  • @TheKr0ckeR
    @TheKr0ckeR 3 года назад +3

    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. :)

  • @flamyboy8043
    @flamyboy8043 2 года назад +1

    i followed everything but my enemy still doesnt see my character ;-; (i enven checked the FAQ and still no dice)

  • @12edcorea54
    @12edcorea54 Год назад

    very nice guide!

  • @themari666
    @themari666 2 месяца назад

    Maybe I should just use collider with set trigger?

  • @brianjirvin
    @brianjirvin Год назад

    So followed this tutorial and works mostly, however the distance before player becomes unseen is roughly double the radius

  • @hewston1
    @hewston1 3 года назад +1

    excellent, thank you

  • @Mikelica69
    @Mikelica69 3 года назад +1

    Thanks a lot ! 🙏🙏

  • @raphaelrtf
    @raphaelrtf 3 года назад

    OMG! You saved me! Thank you so much!

  • @lukdev
    @lukdev Год назад

    GOLDEN!

  • @jehdiaizon
    @jehdiaizon 2 года назад

    amazing! thank you!

  • @thebeeshu771
    @thebeeshu771 Год назад +19

    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.

  • @trentonweber4729
    @trentonweber4729 2 года назад +1

    How would you convert this to 2D?

  • @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?

  • @samuelgirmagirma4019
    @samuelgirmagirma4019 28 дней назад

    I was about to add this to my enemy ai. but then I realized, WHY IN THE WORLD WOULD a futuristic robot not have 360 vision.

  • @chimz8057
    @chimz8057 Год назад +1

    HELP - Question for anyone who can help:
    I'm using this script, however my Player (and enemy for that matter) have their Pivot Points (or transform origins) at their feet.
    I managed to fix the Enemy's Raycast to be shot out of his eyes, but it's being aimed at the player's feet because of the above.
    What would you suggest I do to offset the transform in the script so it aims at the middle of the player. Changing the player's origin isn't possible so I really need to offset it in the script.
    Basically "target.position" needs to have its Y axis offset by +0.9, and I don't know how to apply it.

    • @chimz8057
      @chimz8057 Год назад

      Well no one answered so here's the best way:
      Add a child object and place it in the middle (or where ever you want), and add an empty script to it.
      In the enemy vision script, set its target to that script, so it will look at the object which is placed where ever you wanted.
      The reason I say use a script and not tags, names, etc. is because scripts are easier to find. You can find it using "GetComponentInChildren" and do not require any Strings to work with which are a big no-no.

  • @pixvee341
    @pixvee341 3 года назад

    Thanks you're AWESOME :)

  • @piyushdamania2087
    @piyushdamania2087 3 года назад

    Thanks a ton !

  • @BF-sl5uj
    @BF-sl5uj 10 месяцев назад

    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

  • @uv-al
    @uv-al 2 года назад +1

    Umm, im using HDRP, there is no editor folder, in the HDRP folder, (the one where the only editor folder is) has an editor folder but i cannot change anything in it, so i cant put a script in there, what do i do?

    • @comp3interactive
      @comp3interactive  2 года назад +4

      You just need to create your own folder named "Editor", it doesn't matter where it is and you can have as many as you want, but anything in that folder/folders will be ignored when you build your game 👍

  • @davidbarnaba7229
    @davidbarnaba7229 Год назад +1

    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?

  • @slowman8385
    @slowman8385 3 года назад

    Awesome video!!! Can u make a video about enemy hearing range?

    • @comp3interactive
      @comp3interactive  3 года назад +4

      Dude, it's already on my to do list! 😂 Now I just need to find a celebrity with weird ears for the thumbnail 🤔

  • @AliceMonUnitydeveloper
    @AliceMonUnitydeveloper 2 года назад

    Thanks!

  • @Bobster536
    @Bobster536 2 года назад

    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.

  • @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.

  • @simuloid
    @simuloid 3 года назад +5

    Thanks for the tutorial, this is a nice solution, but I'm encountering some issues. When the player target moves outside of range, the raycast apparently still detects it. Also, strangely enough, the raycast appears to be triggered when the player is outside of the detection angle on the left side. Have you experienced this? Thanks.

    • @comp3interactive
      @comp3interactive  3 года назад +1

      Double check you have all of the scenarios in your script where canSeePlayer is set to false. I did actually have this exact issue prior to recording and I forgot to set it to false in 1 place

    • @simuloid
      @simuloid 3 года назад

      @@comp3interactive thanks for the quick reply! I actually didn’t change your script- using what you had on Git. I tried setting the canSeePlayer bool to false in Start, but didn’t appear to have any effect. Any other thoughts?

    • @comp3interactive
      @comp3interactive  3 года назад

      If that's the case, the only thing I can think is have you set up an obstruction and target layer and assigned the layers to the relevant objects?

    • @simuloid
      @simuloid 3 года назад +1

      @@comp3interactive All set- it was my error- sorry. Thanks again for the amazing tutorial and outstanding support!

    • @comp3interactive
      @comp3interactive  3 года назад +2

      No worries man. Just glad it's working!

  • @kitsunedotexe3587
    @kitsunedotexe3587 3 года назад

    yessssssssssssssssssssssssssssssssss editor tutorial ♥.♥

  • @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?

  • @aokisea
    @aokisea 3 года назад +1

    Mine doesn't trigger the "Can see player" bool even though there are no obstructions and the player is inside the view of the enemy with the script. Help please...

    • @aokisea
      @aokisea 3 года назад

      Actually nevermid, I just forgot to put in the "StartCoroutine(FOVRoutine());" on the start. XD

    • @aokisea
      @aokisea 3 года назад

      Thanks for the tutorial dude! Works very well!

    • @Mikelica69
      @Mikelica69 3 года назад +1

      @@aokisea OMG THANKS I DID THE SAME ERROR XD

  • @КолянКоляныч-б1ь
    @КолянКоляныч-б1ь 2 года назад

    I don't see the green line even though the bool canSeePlayer is enabled.

  • @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...

  • @tobiasnilssenopedal6660
    @tobiasnilssenopedal6660 Год назад

    i got an error while making the editor script, " 'Handles' does not contain a definition for 'DrawWireArch' "

  • @flipthebird6355
    @flipthebird6355 2 года назад

    Thanks you

  • @johnnyjosefsen7644
    @johnnyjosefsen7644 2 года назад +1

    It worked perfectly, until it just kinda broke. I probably did something, but not sure what. The script is identical (with the forward edit) but enemy sees player no matter how far away he is.

    • @johnnyjosefsen7644
      @johnnyjosefsen7644 2 года назад +1

      God dammit, i put the enemy on the same layer, which broke everything. Now it works again.

    • @comp3interactive
      @comp3interactive  2 года назад

      Good catch! 😂

  • @Bobster536
    @Bobster536 2 года назад

    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

  • @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.

  • @satriare5107
    @satriare5107 2 года назад

    Help! I can't export my game with that. How to solve it?

  • @drimp4551
    @drimp4551 Год назад

    It kinda works...still a really good tutorial tho!
    (the line doesnt show up unless I press the CanSeePlayer button...7/10 not to bad

  • @lucasmattos5727
    @lucasmattos5727 2 года назад

    Any way to have a solid color in the radius FOV?

  • @Boris99999
    @Boris99999 Год назад

    There’s also an interesting idea to have four different states of spotting the player:
    - player is out of range (the enemy has no idea where the player is)
    - player is in the center of the fov (the enemy now has the player in complete view and can freely attack)
    - player is at the “corner” of the fov (the enemy has to turn to the player first to look at him)
    - player is outside the fov but inside the range (the enemy feels the player is near but doesn’t know where exactly he is)

  • @giacomociaramella6908
    @giacomociaramella6908 Год назад

    Does the physics have better performance over the mesh filter method?

  • @sillyninja65
    @sillyninja65 Год назад

    if the player is on a platform higher than the enemy, will the enemy see the player

  • @nobodythenothingian
    @nobodythenothingian 2 года назад

    Well i m using Visual Studio too, but it has no auto-write unlike others. Is there a problem with me or what?

  • @viledeg2569
    @viledeg2569 3 года назад

    Nice tutorial, but i wonder how to implement player detection that would work if any part of the player comes in view? Right now it's just detecting if the center of the player is in line of sight but in reality i think a more advanced approach may be needed when the enemy reacts even if only a part of the player is visible.

  • @msipp3790
    @msipp3790 2 года назад

    i mostly want to use this to find every 'wall' the enemy can see

  • @IvBallestero
    @IvBallestero 2 года назад

    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?

  • @zparklyztars
    @zparklyztars 3 года назад

    I found out that sometimes the green line doesn't appear in the scene view until you press play. Could be worth checking if your green line isn't showing.

    • @espress0_1
      @espress0_1 2 года назад

      Little late but thats because the functions update method doesn't run in editor mode only in game mode

  • @kermitfroggers4879
    @kermitfroggers4879 2 года назад +2

    I have 2 problems with guards being able to see my player:
    1) I found out by accident if the player is close enough to the enemy, he won't be seen by the enemy when standing in their field of view. I have to walk almost near the edge of the FOV's radius for the enemy to see me.
    2) The second problem is enemies can only see my player if the FOV's angle high enough (at least 80 degrees). Any lower and my player won't be seen by the enemy even when I stand inside the enemy's FOV viewcone.
    How do I fix these 2 problems?

    • @karolykiss2862
      @karolykiss2862 2 года назад

      I just tested these, I dont have these problems.

  • @henrymontalvo384
    @henrymontalvo384 3 года назад

    Will this work if you allready have an enemy ai script and you just add this script to the enemy?

  • @CodeConnor
    @CodeConnor 3 года назад

    Any way to do this in 2D

  • @dertobbe1176
    @dertobbe1176 6 месяцев назад

    Neat

  • @ceru6973
    @ceru6973 2 года назад

    I don't believe this example accounts for rotation. What happens if the enemy gets rotated? For me it definitely wasn't working.

    • @comp3interactive
      @comp3interactive  2 года назад +2

      It definitely does account for rotation, is it attached to the object you're actually rotating?

  • @alvinopingvino4584
    @alvinopingvino4584 2 года назад

    Are we allowed to use this in titles that we are gonna release??

  • @mrmony7069
    @mrmony7069 2 года назад

    I want to add a sound effect when the enemy spots me, how would I do that

  • @BlackMantisRed
    @BlackMantisRed 3 года назад

    why do you need to do the sphere check? couldn't you just calculate the distance to the player to tell if they are in range?

    • @comp3interactive
      @comp3interactive  3 года назад +1

      You could for sure, but adding the sphere check allowed for more flexibility when checking for more than 1 item, for example if your enemy AI was checking for the player and also cover points or if you hsd multiple players in a multiplayer setting then the sphere would be the better option in my opinion, if you're only ever going to check for your player then yes, a simple distance check will do

    • @BlackMantisRed
      @BlackMantisRed 3 года назад +1

      @@comp3interactive agh okay thanks,

  • @d2b906
    @d2b906 3 года назад

    Does this work for 2D

  • @berdikopgroudli1234
    @berdikopgroudli1234 2 года назад

    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.

  • @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!

  • @HojpMania
    @HojpMania 3 года назад

    How can we render the field of view in game so its visible to the player?

    • @BlackMantisRed
      @BlackMantisRed 3 года назад

      It depends what you want? you could use a mesh or a light to represent the fov and perant it to the head.

    • @soulblaze5716
      @soulblaze5716 3 года назад

      you can use the line renderer...

  • @DerAbsender
    @DerAbsender 3 года назад

    Can somebody tell me if this set up should work for moving enemies as well?
    It could very well be that it is not intended to do so, but my field of view is only facing in one direction, I don't know if it is a "bug" in my code or in the code, since the enemy doesn't move in his code.
    I could very well have made a mistake, since I translate the script from 3D to 2D, so there is a massive source for mistakes.

    • @ImFrantic
      @ImFrantic 2 года назад

      transform.forward gives you the direction, a GameObject is looking at. Like the face of a Character for example. Well each GameObject has a defined forward location. The angle will always start from the face. I mean you can't look behind you without turning either :D

    • @sgtrice1
      @sgtrice1 Год назад

      If you're in 2d you may need vector2.right

  • @user-fe1ke6qe7f
    @user-fe1ke6qe7f 3 года назад

    this is awsome!牛逼!

  • @LLITALKER
    @LLITALKER 3 года назад

    Знакомый дядька на заставке.

  • @nightcrow578
    @nightcrow578 11 месяцев назад

    "i promise you it will not take long"
    "uploaded a 23mins video"

    • @comp3interactive
      @comp3interactive  11 месяцев назад +3

      If you think 23 minutes is a long time to add a base function to a game then I hate to break it to you but you might not enjoy the more advanced levels of game dev