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 - Навчання та стиль
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!
This tutorial was insane. I can usually work most stuff out myself but this was a blessing. Thanks! churr!
You’re welcome Poole!
ako rin tol naging insane dahil dito gagoo
Best tutorials on tps and AI, keep going mate! ;)
Thanks man 👌
Thanks for this awesome tutorial!
You cant tell how much time you saved me trying to figure this out on my own. Thank you very much
Excelent! Thank you very much for this lesson!
I havent started following this series yet becouse of your tps one lol but this will be sooo usefull thank you!
lol no worries! They will be ready and waiting. Good luck!
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.
Awesome they are some nice improvements!
game link ? 😒😒
Psychedelic pizza slices! Great stuff, love your humor!
Haha you made it! Cheers Ted!
Ohhan keep it going very helpful 🔥❤️
Thanks!
woah, you can box select in visual studio?
ps. for those also unaware of this feature, hold alt + shift after initiating a selection.
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?
Thank you! Ahh game jams.. the old coke and pizza partys. It’s been a while since I’ve done one!
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
You're a legend! How was the process coming up with the logic?
How can I make the sight of vision to rotate with the AI mesh rotating?
Please can you make a video on Menu screen where you can change all post processing settings using URP
Can you do the tutorial about automatic position points generating on map and AI position picking system for AI shooter?
where did the layer pickup came from? i dont have it
so, i am getting a small error where the top side of the mesh doesnt render, does anyone know how to fix this
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.
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!
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'
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?
i am trying to follow this tutorial, but i am getting this error "NullReferenceException: Object reference not set to an instance of an object"
Can this be applied to 2D without using height/z axis?
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!
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
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.
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!
thanks had the same problem.
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'
Interesting. I've been using a dot product to allow mobs to see in a hemisphere in front of their eyes.
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
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
Can this be used as dynamic range which means for example if player is hidden agent can't see him without using mesh
Hey yes, you can use this to detect if the agent can see the Player.
@@TheKiwiCoder I know but what if player is behind a wall or something
@@ahmadgamal9587 yes it handles that too, check section: 16:08
Noice😘👍🤘
how would i make so the ai can tell what tag the player has?
Tags can be read from the game object
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?
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
how to make it work in case of enemy is above some ledge
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.
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😂
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"
im gonna guess typos if that is the exact syntax you used
Can you make a battle royale series, or just multiplayer in general pls
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;
}
Um, could it be the triangles array is smaller than 'numVertices'?
replace ++i with i++
@@FelineRaptor-gv4te try that but still not work
@@TheKiwiCoder can u tell me how to fix it?
@@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
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
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 :)
How do I make the wedge visible in game view?
Gizmos are only drawn in the scene view. You’d need to draw a mesh using Graphics.DrawMesh or use the mesh renderer component.
@@TheKiwiCoder Thanks! I am making a game with enemy vision and this was really helpful!
No errors but the wedge isn’t appearing. What?
Selecting the gizmo box in the editor might fix it!
very late response but for me the alpha value of my colour was default set to 0
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.
Nice idea! I wanted to keep everything in unity for the video 🙂
@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
@@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.
@@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.
@@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!
Doesn't the sensor go through walls ?
No!! We’ll it shouldn’t. I think it does.
@@TheKiwiCoder what do you mean bruh ?
@@starplatinum3305 been a while since I made the vid. 10:40 does the occlusion pass
@@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 ?
Sorry for asking too much about that
the field of view should not go through walls🤔
Yes you're 100% right. In this case though, it's just debug rendering so It doesn't really matter.
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
then that better be a separate series because this series has already many episodes
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?
same