Unity line of sight checking using sensors [AI #08]

Поділитися
Вставка
  • Опубліковано 6 сер 2024
  • This Unity tutorial implements a line of sight check by attaching sensors to the Ai agents. The agents receive information from the sensor about it's surroundings, and use this information to make decisions.
    Project files available here!
    ► / thekiwicoder
    Follow Me:
    ► Discord: / discord
    ► Patreon: / thekiwicoder
    ► Twitter: / the_kiwicoder
    ► Instagram: / thekiwicoder
    ► TikTok: / thekiwicoder
    ► TheKiwiCoder: www.thekiwicoder.com/
    Kia ora! 0:00
    Wedge Mesh: 01:14
    Subdivide Wedge Mesh: 05:16
    Sensor Overlap check: 06:22
    Objects list: 08:15
    Field Of View : 09:25
    Agent Integration: 11:16
    Random Wander behaviour: 13:27
    Player Line of Sight checking: 16:08
    Sensor Filtering: 17:03
    Ka kite!: 19:13
    #unity #ai #lineofsight #aisensor #thekiwicoder
    Thank you!
    TheKiwiCoder
  • Навчання та стиль

КОМЕНТАРІ • 95

  • @zartl10
    @zartl10 2 роки тому +3

    I can't thank you enough mate. Code is beyond my skill level, but not so much that I can't understand and further study it; and it was incredibly easy to implement into my own AI controller code. Thank you SOOO so much!

  • @iampoole
    @iampoole 3 роки тому +26

    This tutorial was insane. I can usually work most stuff out myself but this was a blessing. Thanks! churr!

  • @adeveloper7184
    @adeveloper7184 3 роки тому +2

    Best tutorials on tps and AI, keep going mate! ;)

  • @davids3282
    @davids3282 3 роки тому +1

    Thanks for this awesome tutorial!

  • @Killerkraft975
    @Killerkraft975 2 місяці тому

    You cant tell how much time you saved me trying to figure this out on my own. Thank you very much

  • @artursoares86
    @artursoares86 3 роки тому

    Excelent! Thank you very much for this lesson!

  • @dripyman6146
    @dripyman6146 3 роки тому +2

    I havent started following this series yet becouse of your tps one lol but this will be sooo usefull thank you!

    • @TheKiwiCoder
      @TheKiwiCoder  3 роки тому +1

      lol no worries! They will be ready and waiting. Good luck!

  • @nicoh6819
    @nicoh6819 3 роки тому +10

    Great tutorial!
    Used it for my the AI of my fps game.
    I tried field of view visualisation trough Raycasting before but I had a lot of performance issues that way. Also i wasn't able to have it extended on the y-axe.
    I modified this code to that way so it's like a real eye. The sight gets higher the further it goes which isn't important if all objects are on the same height but essential in fps games.
    I reduced the height of bottomCenter and topCenter to Vector3.up * -1 and also moved the y-coordinate of bottomLeft and bottomRight with Vector3.up * (-1/2 * height -1) so the view isn't only going up but also down.

  • @tedsmall5360
    @tedsmall5360 3 роки тому +1

    Psychedelic pizza slices! Great stuff, love your humor!

  • @aniketashis670
    @aniketashis670 3 роки тому

    Ohhan keep it going very helpful 🔥❤️

  • @blipblub2419
    @blipblub2419 2 роки тому +3

    woah, you can box select in visual studio?
    ps. for those also unaware of this feature, hold alt + shift after initiating a selection.

  • @mhrohan917
    @mhrohan917 3 роки тому +2

    I just love this @TheKiwiCoder. Hopefully This Channel Becomes The Next Brackeys. I mean more Subscriber and lots of content.
    And by the way, did you participate in the Game Jam?

    • @TheKiwiCoder
      @TheKiwiCoder  3 роки тому +1

      Thank you! Ahh game jams.. the old coke and pizza partys. It’s been a while since I’ve done one!

  • @sdev6124
    @sdev6124 2 роки тому +1

    Many thanks for your guide. I have a problem, when I build the game, the color of the sensor doesn't show up even though I can still scan other objects. If you know then tell me how to fix it. thanks a lot

  • @LostSol25
    @LostSol25 Рік тому

    You're a legend! How was the process coming up with the logic?

  • @saulotrujillo8142
    @saulotrujillo8142 2 роки тому

    How can I make the sight of vision to rotate with the AI mesh rotating?

  • @vick3554
    @vick3554 2 роки тому

    Please can you make a video on Menu screen where you can change all post processing settings using URP

  • @mrthaw5240
    @mrthaw5240 2 роки тому

    Can you do the tutorial about automatic position points generating on map and AI position picking system for AI shooter?

  • @basvm6123
    @basvm6123 3 роки тому

  • @VanessaE-hm7cv
    @VanessaE-hm7cv Рік тому

    where did the layer pickup came from? i dont have it

  • @echodev7318
    @echodev7318 2 роки тому

    so, i am getting a small error where the top side of the mesh doesnt render, does anyone know how to fix this

  • @XxAnaoGuerreroxX
    @XxAnaoGuerreroxX Рік тому

    Great video, it helped me do exactly what I wanted.
    I was just wondering, would it be too hard to add an Enum to switch from horizontal (as demonstrated on the video) to vertical? I am working on a 2.5D game, and it would fit perfectly, but if I rotate the FOV object, the detection does not work properly.

    • @TheKiwiCoder
      @TheKiwiCoder  Рік тому +1

      Hey shouldn’t be too hard. If you replace occurances of Vector3.Up/Forward etc with transform.up/forward etc then you should be able to rotate the game object however you like. Thanks for the idea!

  • @Whitewolfgaming
    @Whitewolfgaming Рік тому

    im getting 2 errors at the end of the scan overlay section, error cs1501: no overload for method OverlapSphere takes 5 arguements and error CS0165 use of unassigned local variable 'i'

  • @willydevz
    @willydevz Рік тому

    Hey i have a problem. If the player walks in the view it gets spotted but if he walks out its still spotted do you know why this is happining?

  • @theashbot4097
    @theashbot4097 2 роки тому

    i am trying to follow this tutorial, but i am getting this error "NullReferenceException: Object reference not set to an instance of an object"

  • @rakyb12
    @rakyb12 Рік тому

    Can this be applied to 2D without using height/z axis?

  • @BrandonCoulter
    @BrandonCoulter 2 роки тому

    This was crazy good, is there anything this advanced for like a 2d game(Top down or platformer style). Idk if that is even possible, but this was inspiring either way!

    • @Memes4daysz9
      @Memes4daysz9 Рік тому

      I’m guessing you only really need to do maybe three at most triangles for the left, right and middle or one of you get at it correctly

  • @coregamestudio
    @coregamestudio 2 роки тому

    The problem Im having is the AI enemy will walk forward a couple of meters and just stop, will not chase or shoot. tripple checked the code, set all ground and walls correctly. Once it stops it unchecks the script from the object which is weird and also if I recheck the script to active and move the ai enemy around a little bit he will chase the player but not shoot.

  • @ty-xq7bl
    @ty-xq7bl 2 роки тому +3

    I've been trying to follow this video for two hours now but I just can't seem to get the Scan function to pickup any collider. The triangle mesh shows up properly in my scene but the count function is 0 for this code:
    count = Physics.OverlapSphereNonAlloc(transform.position,distance,scanned,~ignoreLayer,QueryTriggerInteraction.Collide);
    What I don't understand is that there is no reference to the triangle mesh built anywhere. Is OverlapSphereNonAlloc creating the triangle?
    Thanks in advance.
    UPDATE: I get it now you are casting a sphere around the character then determining it with the triangle mesh. I switched to Physics.OverlapSphere and it started detecting colliders so i'll stick with that. thanks!

    • @letisschiller5185
      @letisschiller5185 2 роки тому

      thanks had the same problem.

    • @nitro5gaming463
      @nitro5gaming463 2 роки тому

      im having an error, can u tell me what to do?
      Assets\game\scripts\AISensor.cs(45,25): error CS1501: No overload for method 'OverlapSphere' takes 5 arguments
      edit: i removed colliders and now my code is this:
      count = Physics.OverlapSphere(transform.position, distance, layers, QueryTriggerInteraction.Collide);
      but i get this error now: Assets\game\scripts\AISensor.cs(45,17): error CS0029: Cannot implicitly convert type 'UnityEngine.Collider[]' to 'int'

  • @BlackJar72
    @BlackJar72 2 роки тому

    Interesting. I've been using a dot product to allow mobs to see in a hemisphere in front of their eyes.

  • @thebulletkin8393
    @thebulletkin8393 4 місяці тому

    Does this essentially serve the same purpose as holding a reference to the player, checking the distance, then using the dot product to find the angle with a raycast to finish it off? I've been looking around to see if there are any other potential ways of doing it but they all seem to follow a similar method

    • @TheKiwiCoder
      @TheKiwiCoder  4 місяці тому

      The sensor volume can be attached to any character (player or Ai) to capture a set or arbitrary objects in view, which could include players, Ai, pickups, or other points of interest

  • @ahmadgamal9587
    @ahmadgamal9587 3 роки тому +1

    Can this be used as dynamic range which means for example if player is hidden agent can't see him without using mesh

    • @TheKiwiCoder
      @TheKiwiCoder  3 роки тому +2

      Hey yes, you can use this to detect if the agent can see the Player.

    • @ahmadgamal9587
      @ahmadgamal9587 3 роки тому

      @@TheKiwiCoder I know but what if player is behind a wall or something

    • @TheKiwiCoder
      @TheKiwiCoder  3 роки тому +3

      @@ahmadgamal9587 yes it handles that too, check section: 16:08

  • @farrukhmalik8747
    @farrukhmalik8747 3 роки тому

    Noice😘👍🤘

  • @Crocc101
    @Crocc101 4 місяці тому

    how would i make so the ai can tell what tag the player has?

    • @TheKiwiCoder
      @TheKiwiCoder  4 місяці тому

      Tags can be read from the game object

  • @aquafina2on217
    @aquafina2on217 2 роки тому

    The video was very good but i keep getting this error.
    Gizmos.DrawMesh requires a mesh with positions and normals how do i fix it?

    • @Randommmszz
      @Randommmszz 3 місяці тому

      i doubt you still need help but the way i fixed it was adding mesh.RecalculateNormals(); instead of whatever you wrote with the autocorrected hah

  • @javednoman3051
    @javednoman3051 2 роки тому

    how to make it work in case of enemy is above some ledge

    • @DarkM0ds
      @DarkM0ds 2 роки тому

      You can make a trigger box at the end of the ledge and then extend the height of the AiSensor, with the OnTriggerEnter callback, and when the AI leaves the trigger box, you can reset it using the OnTriggerExit callback. Idk if this is the best way but something i thought of when reading your comment.

  • @vlogershloger7608
    @vlogershloger7608 Місяць тому

    Can this tutorial be followed even if I don't have Weapon Pickup script?Because I wanna make a horror ai who's looking for the player.
    PS:I'm new with Unity and developing😂

  • @oblo4952
    @oblo4952 3 роки тому

    I probably just made a dumb mistake, but Im getting lots of errors on the first code
    The words Quarternions, meshColor, vertices, bottom center, and bottom left "don't exist in the current context"

    • @blipblub2419
      @blipblub2419 2 роки тому

      im gonna guess typos if that is the exact syntax you used

  • @itoroenang8266
    @itoroenang8266 3 роки тому

    Can you make a battle royale series, or just multiplayer in general pls

  • @warunanirmal9537
    @warunanirmal9537 3 роки тому

    Excellent tutorial !
    but there is a little problem,
    I'm getting "index out of range error" in console.
    for(int i=0; i < numVertices; ++i)
    {
    triangles[i] = i;
    }

    • @TheKiwiCoder
      @TheKiwiCoder  3 роки тому +2

      Um, could it be the triangles array is smaller than 'numVertices'?

    • @FelineRaptor-gv4te
      @FelineRaptor-gv4te 2 роки тому

      replace ++i with i++

    • @Meokhongten33
      @Meokhongten33 2 роки тому

      @@FelineRaptor-gv4te try that but still not work

    • @Meokhongten33
      @Meokhongten33 2 роки тому

      @@TheKiwiCoder can u tell me how to fix it?

    • @FelineRaptor-gv4te
      @FelineRaptor-gv4te 2 роки тому

      @@Meokhongten33 can you share the entire code here? because i need to know what you wrote in the beginning to know why this is happening

  • @maazbinshahid649
    @maazbinshahid649 10 місяців тому

    Instead of manually calulating the position of the gameobject and compare it to check if it is in the sensing range or not why didnt you put a trigger on the mesh and used onTriggerEnter

    • @TheKiwiCoder
      @TheKiwiCoder  10 місяців тому

      I wanted to have more control over how frequently the sensor was checking for objects. I suppose I could have disabled/enabled the trigger component but felt like doing it by hand instead :)

  • @CodingKricket
    @CodingKricket 3 роки тому +1

    How do I make the wedge visible in game view?

    • @TheKiwiCoder
      @TheKiwiCoder  3 роки тому +1

      Gizmos are only drawn in the scene view. You’d need to draw a mesh using Graphics.DrawMesh or use the mesh renderer component.

    • @CodingKricket
      @CodingKricket 3 роки тому

      @@TheKiwiCoder Thanks! I am making a game with enemy vision and this was really helpful!

  • @masterreeses
    @masterreeses 3 роки тому +1

    No errors but the wedge isn’t appearing. What?

    • @TheKiwiCoder
      @TheKiwiCoder  3 роки тому +1

      Selecting the gizmo box in the editor might fix it!

    • @chrislee1774
      @chrislee1774 8 місяців тому

      very late response but for me the alpha value of my colour was default set to 0

  • @WelshGuitarDude
    @WelshGuitarDude 3 роки тому +1

    Why not make the mesh in blender and mark the vertex indices that you want to move in the mesh data so you know which vertices to move in code if you ever change the angle. Would be far less code to run all the time.

    • @TheKiwiCoder
      @TheKiwiCoder  3 роки тому

      Nice idea! I wanted to keep everything in unity for the video 🙂

    • @FelineRaptor-gv4te
      @FelineRaptor-gv4te 2 роки тому

      @Mee please no. For people like who don't have a good pc, blender doesn't open or run. so best to do and keep everything in unity

    • @CrAzYpotpie
      @CrAzYpotpie 2 роки тому

      @@FelineRaptor-gv4te Blender and Unity go hand in hand, and not to sound mean, but if you can't run blender, then you must be working with the laggiest Unity ever. Might want to upgrade.

    • @FelineRaptor-gv4te
      @FelineRaptor-gv4te 2 роки тому

      @@CrAzYpotpie been meaning to. but i work with a small game and scene window so not much lag, and it takes around 5 mins to open a project that over 4 GB. i think its the gpu thats the problem. its a DirectX 10 gpu with only 256 mb vram. processor and ram might also be a problem though as they be 2.6 GHz dual core and 3 GB respectively
      I have a very old machine from around 2010 with only minor upgrades [almost negligible] over the years. meaning to upgrade it for a while though.

    • @CrAzYpotpie
      @CrAzYpotpie 2 роки тому

      @@FelineRaptor-gv4te Hope you can soon! I know what it's like to always be developing on a sub-par system, as well. Never fun, but at least it sounds like your project isn't bogging it down too much yet. Good luck with it!

  • @starplatinum3305
    @starplatinum3305 Рік тому

    Doesn't the sensor go through walls ?

    • @TheKiwiCoder
      @TheKiwiCoder  Рік тому

      No!! We’ll it shouldn’t. I think it does.

    • @starplatinum3305
      @starplatinum3305 Рік тому

      @@TheKiwiCoder what do you mean bruh ?

    • @TheKiwiCoder
      @TheKiwiCoder  Рік тому

      @@starplatinum3305 been a while since I made the vid. 10:40 does the occlusion pass

    • @starplatinum3305
      @starplatinum3305 Рік тому

      @@TheKiwiCoder oh fck, i see, theres a thing at the end of the sensor that detects too, nice. Btw u have any idea about making a DND thing ?

    • @starplatinum3305
      @starplatinum3305 Рік тому

      Sorry for asking too much about that

  • @MASTONGAMES
    @MASTONGAMES 3 роки тому +1

    the field of view should not go through walls🤔

    • @TheKiwiCoder
      @TheKiwiCoder  3 роки тому +3

      Yes you're 100% right. In this case though, it's just debug rendering so It doesn't really matter.

  • @brodiemorris2081
    @brodiemorris2081 2 роки тому +1

    It's a bit too fast imo. Maybe try dividing an episode like this into multiple parts so you can actually explain how it works because rn it is way too hard to understand what is happenibg

    • @FelineRaptor-gv4te
      @FelineRaptor-gv4te 2 роки тому

      then that better be a separate series because this series has already many episodes

  • @grp6073
    @grp6073 2 роки тому +3

    Great tutorial but I've got a problem. It always gives me an error that says "IndexOutOfRangeException: Index was outside the bounds of the array.
    Eye.CreateWedgeMesh () (at Assets/Scripts/Eye.cs:83)
    Eye.OnValidate () (at Assets/Scripts/Eye.cs:95)"
    Is there any way to fix this problem?