The Cool Trick Game Developers Use to Make AI See You.
HTML-код
- Опубликовано: 4 сен 2024
- Learn game creation with Godot, for free: www.gdquest.co...
---
📚 THE LINKS
Our Godot courses: gdquest.mavens...
---
👥 CREDITS
Video edited by Nesi: / nesiawesomeness
---
💌 JOIN US
Come chat on Discord: / discord
Follow us on Twitter: / nathangdquest
---
📝 CC-BY LICENSE
This video is licensed under the CC-By 4.0 license: creativecommon...
You can attribute it to "CC-By 4.0 - GDQuest and contributors - www.gdquest.com/"
Swinging a ray in a Cone in front of the player is often many times faster than using many rays. also makes your AI look like it takes a moment to react and other times reacts instantly
That sounds really interesting
Was about to comment this as well 👍
Nice!!!
You also might not want to do it in a circle, if entity has defined fov, you just swing it back and forth in that triangle/cone(3d). Sometimes for accuracy and speed more than one ray with specific gap iterational scanning might be the best
@@iliagogotchuri7742 Yes, circle/cone was actually what I meant, feel most understood what I meant.
As I said, My suggestion wasn't for accuracy, was to make the AI have a little bit of randomness to it's detection.
I'd love to poke around in a bare bones demo project both in 2D and 3D as to how this works exactly. Or a video that goes more into depth on this subject in Godot :)
The video that covers Godot is the plan! Working on it this week.
@@Gdquest thanks a lot :) very curious how a proper raycast is set up in Godot! ^^
@@Gdquest bro i have a question that how i can fit my game screen to different DEVICE like tablet,mobile,pc, ipad?
@@Gdquestis using area 2d is same as raycast?
I'm working on an infiltration game. I found out that putting target on the bones on the player character and making the AI cast rays to those target when in range and checking if the target are in the FOV of the Ennemis, is way more efficient for precise detection than just trow a bunch of rays in the player direction.
I've even put scores on each Target to make each Target impact the detection rate differently !
Sounds a bit like how (at least from what I've heard) the Tom Clancy Splinter Cell games do stealth/detection
a cheaper method is to use fewer rays, but introduce randomization into the direction within a certain tolerance, and have this poll maybe every 1/5 of a second or so. this way you might be 'visible' but they don't immediately notice you, more like a real person or animal.
less code, less system resources, more realistic performance. win all around.
This is the only #Shorts on RUclips I've appreciated, not even lying.
Great job, more of these!
Some games do this by giving the AI a camera. The camera works the same as in Wolfenstein 3d; drawing "walls" based on distance from the entity. These walls are defined by two things. The 5 corners of the view-cone and, the amount of time defined by the programmer.
Thanks for sharing, I didn't know that!
yes because there was no 3D in wolfenstein it was all faked . If you would have a camera on all enemies your game will freeze . too much rendering .
@@hamza-trabelsi I think he means a "Camera".
@@HAWXLEADER He means raycasts
wolfenstein 3d used raycasting for graphics if i'm not wrong, so it would just be the same thing as shown in the video but more complicated
In VoidStrike(my game), i use two areas, one as a field of view on the enemy ship and other as proximity sensor in the player
The field of view is usually the same radius for every enemy, and entering it will make the AI starts chasing you, the proximity area radius is defined by what weapon the NPC ship will use, as every weapon has a different maximum range, if the NPC enters the proximity area, it will start to shoot in your direction, if you move but not far enough to make the NPC leave the proximity area, the NPC will try to keep distance, shooting and moving if necessary.
It ended being better than raycast because both yours and npc's vision aren't supposed to be constrained by anything but render distance.
Hey! I wanted to say that, if you want to implement something like what i have done but in a lightweight fashion i got a module in Godot Asset Library, it's called AreaProber2D, i hope you enjoy! Thank you
I personally make 2 vectors, one from the ai to the play and one from the ai's forward direction. I then measure the angle between these two. If the angle is out of range from the set FOV, i return the function. Else i proceed and cast a ray from the ai to the player. If it isnt obstructed then it means that the AI cant see the player. Else it means that the Ai has detected the player
Thats just an optimisation but it suffers from the same problems as casting a single ray
Smartest way to go about it
Isn't this just an implementation of the single-ray method?
@@mothcatcher893 tbh i dont know about it so i cant tell
You dont even need a single ray if you dont have obstacles. Give the AI the player position and then decide based on angle and distance. Of course you can then still cast a ray to check for obstacles
I usually use a bounding collision box/cone/sphere, and if there's something inside I cast a ray to be sure it's not against a wall, it's quite cheap and works well with Unity.
Yes, that's the bit shown at the end of the video, using an area in Godot. The number of rays on screen is probably overkill but that's mostly to get the point across and make it very visual.
@@ebistudiosoffical8447 ''Glitching'' is a not a good term to use when something doesnt work as intended on maching other than yours. Your question is random indeed.
It's a RUclips short! You still couldn't finish watching before adding a comment pretending the video didn't cover something that it did?
This channel is one of the best things to ever exist. God bless Godot and everything related to it.
Thanks! Your videos are super helpful!
I think a smart thing to do is also spread out different rays across frames, maybe half on one and then the other half on the next, that way you can have a lot of (mostly up to date) checks for like half the cost. You can also do awesome stuff like make the player MORE visible to enemies the more rays are hitting them, like maybe 1 isn't enough to be detected but if the ai hits with 5 diff rays then its easily spotting the player.
if i ever have to make a game with some enemy that can see you, i will remember to use this trick! thanks
Seems like it would make more sense to choose one of the rays for each check (perhaps randomly). Unless things are moving very fast you probably don't need to check all rays every time especially depending on the speed of the checks. I think I'd rather check further than all the rays every time as that would give the ai more a a natural feel. Even in real life when people are directly looking in your direction they don't immediately notice you, there is a delay before one of the ray casts hit.
In a 3D action FPS a single ray from enemy head to player head can do the job cause only if the player can see the enemy (through camera in the head) does the enemy have to look smart. AI not seeing player edges is more noticeable in top down or third person games like in the example.
And that's how you get stuff like Half-Life 2 enemies not seeing you if you block them from view by holding a small prop in front of you.
I think a good detection system is from Payday 2, the guard has a range of vision, front, left, right and up, the detection bar increases slowly or faster (in case the player has a heavy armor, by hitting the guard in encase mode or by running towards the guard or away)
It depends on your detection risk (3 being the minimum and 75 being the maximum) if you stand still near the guard depending on how much detection you have and if you are inside the guard range it can see you and if you get detected and another guard sees the alerted guard, he will be aware of a threat leading to a chain if you have bad luck
A very interesting and complicated way to explain Payday's detection
To emphasize why this is better:
Using multiple rays can also communicate the direction that the target is moving (if the leftmost ray stops detecting the target first, it's safe to assume they moved to the left), which can help the ratcaster know which direction *they* should also move, should they wish to pursue them (if there's an exit in the map that is open towards the left, the raycaster could be programmed to move in and intercept the target's path)
The game knows (or can at least easily determine) the velocity of all objects. No need to guess.
Really liked the area optimization for starting the Raycasts.
very informative, enjoyable and short. I love these
For me in unity I use a on trigger box collider and then a ray cast so when the player enters the box collider it draws a ray to see if it hits the player or a wall this way I have a semi realistic field of view and pretty good performance when there's alot of them
I’d use a state machine here. >65% rays must make contact with the player for >1 seconds to identify the player, anything less on either metric and they’re only investigating. After they’re on alert, you only need >10% coverage for >.1 seconds for them to clock your location, now that they’re looking for you. This doesn’t reset after they return to patrols; they know there was already one incident tonight which is more than they’ve had in months.
Loved the explanation and the visuals!!
You could actually just get the players position and then for each player in a certain range and between certain coordinates depending on the rotation of the AI agent you cast a single ray towards each player, if the ray hits the player then you see the player, if the ray doesn't hit the player there is no player
Man that’s so cool, it makes want to go back into Godot again
Absolutely love this! Very short-straight to the point, informative and understandable. Major plus!+++
using the verticies of the bounding box of whatever you are trying to detect line of sight of could also work.
For top-down 2D, I think you can also do angles between vectors.
I think having multiple rays doesn't completely solve the problem if the character hides behind an obstacle, so is it possible to make the enemy even smarter to analyze and learn how to follow a character?
I don’t know much about developing games but would it be a good idea to calculate the distance to the player, then use some trig (along with player size) to find the angles that form rays that touch the front and back of player?
Instead of many rays at once you can be checking one random point of the player each frame so the more visible you are the more likely you are to be seen.
So this is why some games let you hide just by standing behind a lamp or an object like in Amnesia or Half life 2
Using boxcast in the direction of player makes more sense to me than throwing multiple raycasts
Same thing how bombs / explosives in games work.
One neater trick is to do the same idea, but in reverse.
I thought that was cool when I first heard about it.
are we getting more of these videos these types of videos?
If you like them definitely!
@@Gdquestwill drop a like as soon as they, get released
Thx great information ♥️😊😊
This example seems to assume that the "AI" is always facing the "Player". Ray casts are definitely involved in determining vision; but the AI shouldn't always be facing the player. Seems like you should first do a cheap check to see if the Player is even in the AI's FoV first; then shoot rays to determine if vision is occluded. You should probably also cut the player a little slack and not check the extremities of the Player's model; just the "core".
Yea this ai uses wallhacks.
Then you should cast rays in players direction without rotating.
@@b.g.9187 way cheaper to check FoV angles first, like video and I said. You only shoot rays to determine if there are visual obstructions.
In this case the ray turns but independently from the tower. But the tower doesn't have a limited cone of vision. We only have a cheap prototype model I hope to replace with a 360 tower, some kind of ball or something
Please make more of this info videos about godot! ^^
you can also just use a custom collider
Very thanks from Venezuela 🇻🇪.
Very helpful!
The simply answer is - It doesn't knows
You just hit an invisible switch, and activated it, and caused everything under the rules the dev created
I used large invisible shape as a vision area and then just added a collision trigger. I know it's crap but it was my first game 😂
You can also sphere and cube cast.
That's a valuable tip! Thank you!!
I did not know about the multiple rays
i love the short vids 🥰 short but informative
Hi, how can I implement that semi transparent red cube around the player that only shows when and enemy is looking at it, I just want to know how to create that affect,,,please help
Love these shorts
So good! Ive wondered that for a long time now 😁
Basically a vision cone
👍
Not really, The AI always "Knows" where you are in simple games, Their code simply ignores you if you are out of the cone.
Is the area 2d same as raycast? In godot
A better way is to get dot product from ai forward vector and direction from ai to player, convert to degrees by multiplying by 180, then compare like a FOV angle I.e 70 degrees but divide by half, so dot(player direction, ai forward) * 180 vs ai fov... way cheaper than multiple Ray casts and far more accurate.
EDIT: For completeness, the correct math is dot(aiForward, aiToPlayerDirection) * 180 vs 180 - fovAngle
Oh and combine that with raycheck
Why can't we use only 2 rays and try to check if the player's position is between them? Is the video version more efficient?
The rays are bound to the player's position. The player's position is always between the rays. Multiple rays are used to see if part of the player's model is peeking behind cover. You could use two rays to see if either side of the player is unobstructed and that could still not work with differently shaped cover. Less or more rays is dependent on what you need for your game (how many types of cover, ai intelligence, enemy quantity, player hitbox).
very good, thank you for this.
I think another technique, which Godot has built in, is using the dot product of the basises (bases?).
throwing multiple rays will not work when there are many ai around
Amazing!!
Great explanation
Raycast are super heavy on performance tho so casting multiple isnt with the cost
Sure wish in Skyrim they would have programmed teh AI not to see you when you're in the shadows or behind walls.
Linear algebra has entered the chat
Or, occlusion culling before that, you know
In 3D, wouldn't it be better to cast a single shape like a cone and apply the same logic where if an obstacle is in between the player and itself then it doesn't see it. And cast a "shadow" inside the cone to represent what the AI can see.
This would be way more costly. Also this video hyper simplifies the issue anyways.
why make a sphere arrond them when the player can have the sphere?
(Just a beginner hobby game dev here)
For the shown scenario it probably does not matter much (and might even be better performance to have only one sphere on the player).
From a design perspective it might make more sense to give each enemy different detection ranges, thus the sphere on the NPC.
I could even imagine adding a sphere to both player and enemy, reacting to their overlap instead. The player could then have stealth stat which decreases its own sphere radius.
@@rabbyte3564 if u need different sight range for npcs u can make diferent size spheres on the player
Can you make a tutorial on how to make a html and CSS website for a game?
Is there a full video on this? what is this trick called so i can look up a tutorial lmao plz hlp
Could you do a tutorial of how to do this?
hey,what game engine is that? unity?
Okay, and answer to the question wasn't given: how in hell ai knows it sees ME not some obstacle? I'm against the wall in dark skin in dark corner of the map, barely any light comes in... I am noice on sensor of the most sensitive resolution. AI doesn't see me by definition, but it "randomly" headshots me!
Top content as always
There is any way to render out animations from Godot in our pc local drive
HI! great video, i have a question, how do you draw the rayscast in 3D, i mean, to be able to see them when you run the game?
I just open-sourced that code for you. You'll find some info in the README: github.com/GDQuest/godot-visualization-tools/
Having this many rays for every enemy is kinda bad
Meanwhile cs 1.6 expert bot ai: I don't see, I sense.
How does enemy hear the player?
raycasts with a shape exist. what is the performance difference between those two ?
You'd have to measure it in every given engine. The performance will differ based on the shape you cast too.
I have a problem hove does the enemi or the observer rotate as player move
Easy, get target rotation towards the player, set as target then gradually rotate towards it each frame. Learn about Lerp and Slerp functions.
And that's why the AI shot you right in the head through all that grass.
Why can't you just create a mesh that raycasts? I'm new to godot but that seems more logical than using 20 raycasts
How to make these lines?
Nice
a tutorial would be nice
How about using area2d in a top-down game?
Wouldnt the dot product be more efficient for this purpose?
How would you use Dot product to detect if the enemy see the player ? dot product just about vectors , it doesn't care about obstacles in between , or the distance . it's only about direction . So how would it help ?
@@hamza-trabelsi yeah you're right, I wasnt thinking straight when I wrote that
Gold
Hi, i try to put a character on my map, but i don't see the character, help please
What's tricky is making ai make mistakes.
This method does not use AI, it is just an algorithm.
Nothing uses artificial intelligence. It doesn’t exist yet. AI in videogames usually refer to enemy behaviour and has nothing to do with real artificial intelligence. People call chatgpt AI but it’s not
It's not a sophisticated ai, but ai is basically algorithms. The more algorithms it has the more believable it is.
This is what people meant by video game AI before the machine-learning stuff happened.
Why did he say "meow" in the middle of the explanation?
How to get rid of these rays because I hate it when a damn bot is on my 🍑 telling everyone where i am😂
Yeah sure. The real trick is that the ai also always knows where you are and will almost certainly just so happen to meander your way for no goddamn reason all that time
u wrong bro. GODOT is fast smooth and more important is free. u try it is wroset to use
is this considered ai? i think that is just really smart coding and not ai
You need only 2 lines in a 2d game.
This is not AI and this word gets misused a lot. There is no capacity for it to learn.
Nice and Short... CcccCool GgggGamer...
Noice
player = he
A cheap trick
"if it works it works"
- gamedevs
Like