We get a lot of questions about AI. Here's a simplistic algorithm that produces great results. Text version & link to download the sample project: godotrecipes.co... Support me on Patreon: / kidscancode
Пікірлер: 107
@DriftWare6 ай бұрын
How is this done with a 3d object? There's all kinds of vector functions that don't translate from 2d to 3d.
@syllight90533 жыл бұрын
This is one of the most valuable programming channels out there! These ones are rare and is like a jewel. I am 14 and started gamedev on 13 and I love this channel for supporting kids like us to get onto programming! Keep up the great content!
@theeox5 ай бұрын
I would have really liked to see an implementation for 3D. I'm not really grasping it, doesn't help that godot 4 has different nodes and functions.
@jmbiv_dev3 жыл бұрын
Glad to see videos coming again! I've been seeing context-based algorithms (as opposed to/in modification of traditional steering behaviors) gaining a lot of attention recently, such as Game Endeavor's AI video. I've been working on a couple implementations of my own and have enjoyed seeing how others have gone about doing it. Great video that explains a nice, scalable way to implement this.
@JahznCentral11 ай бұрын
hi everyone. if you want the code to add interest to the opposite ray of the ray that sees danger, write the choose direction function like this func choose_direction(): > for i in num_rays: > > if danger[i] > 0.0 > > > if i > num_rays/2: > > > > interest[i - (num_rays/2) - 1] = added_interest > > > else: > > > > interest[i + (num_rays/2) - 1] = added_interest > > > interest[i] = 0.0 added_interest will be any arbitrary high number. you will have to fiddle with this. i found 20.0 works for my project.
@ollereberg78282 жыл бұрын
Could you do a version of this for 3D pathfinding, like for space ship where you you actually need to use x, y, and z? Been trying to figure out proper 3D pathfinding in not just this but in Unity too and I always get stuck
@jordymendoza8192 Жыл бұрын
Can I get the code for the debugdraw node u had on the agent
@neto93487 ай бұрын
Lol, that’s exactly what I need for a game that I’m making on Godot and it just randomly appeared on my home page. I had a rudimentary code using the same concept, this will help me a lot, thanks.
@normaldefault22063 жыл бұрын
So, I'm working on an RTS-style thing, and I figured I'd add this to my pathfinding to stop units from bumping into each other and generally getting in each others way. When it works, it's awesome, I love making little crowds of units cross paths with each other and shuffling out of each other's way; however, if two happen to collide head on, and ONLY the forward facing ray is blocked, the average vector is still straight ahead and they don't get anywhere. I can add if cases to get around this, but it doesn't feel quite so elegant...
@andrewvickerman19402 жыл бұрын
Instead of the 0deg ray, make it -0.1deg or 0.1deg randomly. This is almost the same except that agents will favor one side slightly more than their other.
@TheDuckCow3 жыл бұрын
Really appreciated this - both the video and the webpage! Though I had to take a few extra steps to make it applicable for my own vehicle body-based car, was able to get this working nicely.
@vatrenikrug2 жыл бұрын
any chance that you can share some project? i cant get it working :( and i was copying from website :(
@TheDuckCow2 жыл бұрын
@@vatrenikrug sadly not open sourced or avail right now, although if you are curious how far I was able to get with it, you can check see it here: kzbin.info/www/bejne/nme6lHWAfsqKiNE It's slightly more than a copy paste though, I recommend just going one step at a time, and really make use of those debug drawing vectors.
@yatchan3 жыл бұрын
Thanks for the great video! I've tried it out and in a scenario like this, with a simple obstacle course, you don't even have to set up a track. The boids are managing very well with just going forward :) With a little tweaking, it can be applied for example to a monster wandering randomly around a cave.
@couscous43493 жыл бұрын
Great video! Any chance you could upload the project so I could take a look at the code you use to draw the arrows? Thanks!
@JamesKelly8911 ай бұрын
This seems like it would work well using a navmesh where you want to follow the path given by the navmesh agent but also want to avoid obstacles and other agents. So the get path direction could just just a vector to the next waypoint given by the navmesh.
@mrrandle57764 ай бұрын
Yo dude, im trying this right now and cant for the love of me get it to work, any clue how you would write the get_path_direction to work with the navmesh? I have the nav agent drawing the path to the target. but nothing i write in the get path direction seems to make the interest follow it
@loot6 Жыл бұрын
Seems good in theory but in practise has flaws. For example if the target is east and east is blocked by a small NPC in front of the target, just large enough to block only one ray, the best directions will be diagonal NE and SE to take you around the object. But if you take the average of those it's obviously east...which moves you into the NPC...
@cheapmeow8478 Жыл бұрын
Constraint AI that if you can't "see" target, then you can't be interested by it, may help avoid this problem.
@raremc16203 жыл бұрын
Hey, thanks I was literally just planning of doing something similar before I noticed your new video!
@Knutmeister3 жыл бұрын
Thanks a lot! I like the direction, all those car related video are going.
@vatrenikrug2 жыл бұрын
your recepies are amazing,i do hope to make my first games and to be able to give some back for your amazing work. thanks for being a teacher on godot :)
@tPlayerioT10 ай бұрын
it seems godot has now a built in simple steering behavior. Its called "Navigation obstacle" and you put them inside the thing you want to avoid. then enable "Avoidance" in your navmesh character.
@lumenwrites3 жыл бұрын
What an amazing tutorial, thank you so much!
@TackerTacker3 жыл бұрын
Thanks for the great video. I have one question. How would you handle tracks made out of tiles? Is there a way to combine multiple Path2D nodes into one continues path, or would it be better to keep them separated and somehow set the position to the different tiles one after the other?
@Kidscancode3 жыл бұрын
As recommended in the video, it wouldn't affect the agent itself at all. It would be the responsibility of the Tilemap node to provide a `get_path_direction()`. It's hard to be specific, because it depends on how your Tilemap actually works, but maybe you have a direction vector mapped to each tile index.
@TackerTacker3 жыл бұрын
@@Kidscancode I meant something like track segments not single directions, tiles with right and left turns, S curves, etc. maybe a crossroad. If I would prepare little track piece tiles in scenes, and every track piece scene has it's own Path2D with some kind of curve, I'm not sure how I would go about connecting these pieces together. I guess I would do path follow and at the end of the a path if there is still movement left I transfer it over to the next segment and start from there with that offset?! What would I do if there is someone fast enough to overshot an entire tile? Hmm, that would probably not happen unless the tiles are pretty small. I'll need to play around with it when I got some time. Again thanks for the tutorial, very inspiring.
@bamtoday3 жыл бұрын
Excellent tutorial!
@szokedencsimarci56683 жыл бұрын
thank you for this video!
@gameanddata31943 жыл бұрын
I'm doing my game thanks to this tutorial, awesome.
@JorgeRosa3 жыл бұрын
GREAT tutorial! Thankyou!
@n014874773 жыл бұрын
Hi - thanks for all of your really informative tutorials. I note that this one doesn't seem to have a download link. Am I mistaken?
@pascalcasier9593 жыл бұрын
Excellent ! thank you so much.
@NicholasOrlowski3 жыл бұрын
Another amazing video, thanks!
@brianbatton99503 жыл бұрын
Hi! I know I'm kind of late but I would just like to ask where I can download the source code for this so I can test it out. I went to your website but did not see any download links. If you could help me out I would be very grateful!
@JohnnyCashOriginal Жыл бұрын
the position variable is not defined. var path_direction = owner.get_path_direction(position) ... what is this supposed to be? global position of the object?
@boerbol94222 жыл бұрын
Awesome video. Bless you!
@rosva4331 Жыл бұрын
You sir, just saved my ass. Thank you.
@juanloutech28643 жыл бұрын
Brilliant as usual. Thanks man! Very usefull stuff
@Marduk401 Жыл бұрын
10:22 but what happens if you place an obstacle that only blocks the middle raycast without touching the others?
@staticsteam636 Жыл бұрын
how set this up in 3d???
@DA-cl4ww3 жыл бұрын
They seem to get stuck when running into more complicated collision shapes. Is there a better way for them to perhaps consider a direction opposite of all those blocked?
@gabrielpsych6 Жыл бұрын
I really can't understand what are these "rays" since there are no rays being cast from the tree nor code. Where are they coming from? I tried following the code but nothing seems to happen.
@the_cheese_cultist Жыл бұрын
you cast the ray through the intersect_ray function.
@michaelthelen52973 жыл бұрын
Wow very cool! How did you deal with the extra axis in 3d? (like avoid the rays hitting the road on an up slope, or missing a wall when cresting a hill) I imagine this could be handled with collision layers and raising collision boxes... just curious
@toms2oo83 жыл бұрын
Not sure but I imagine the same concept works in 3d space, eg. you just project rays from a sphere rather than a circle around the object. And then your follow path can also exist in 3d.
@Kidscancode3 жыл бұрын
Depending on your setup, you don't even need that. A 3D car, for example, would still just need a ring of rays horizontally.
@thelumberjack88382 жыл бұрын
Please make a recipe for how to do this in 3D because the difference between 2d and 3D is night and day
@KibanyaG2 жыл бұрын
Here's a basic implementation in 3D
@lutiyoffnik86613 ай бұрын
needed in version 4 godot
@froggyCodingClub3 жыл бұрын
Thanks, clear explenation
@jlebrech Жыл бұрын
can the steering angle change the wheel angle and in turn the wheels affect physics of the car?
@bruceparker6142 Жыл бұрын
Has anyone got this to work on a 3d track in godot 4? I'm getting errors in the .rotated(rotation) parts.
@ericcollinseh89163 жыл бұрын
Hey, how do you make the characters animation track the mouse ui?
@piousthepious2 жыл бұрын
Great tutorial! I do have one question. How do you handle avoidance with the danger? It seems to be the danger simply gets negated but how would I go about making it so the complete opposite direction is desired? My first thought is to add interest to the opposite ray but I have no idea how I would even get the opposite ray. Is there something I'm missing?
@andrewvickerman19402 жыл бұрын
Just search how to get the opposite of a ray. There are a bunch of videos about it.
@nicolassoriano26218 ай бұрын
it doesnt work, it says "Parser Error: The identifier "position" isn't declared in the current scope."
@thelumberjack88383 жыл бұрын
Thank you so much
@viniguerrero3 жыл бұрын
What is the equivalent in 3D for this? var d = ray_directions[i].rotated(rotation).dot(transform.x)
@Kidscancode3 жыл бұрын
Dot product is the same in 3D. To convert a vector to a local rotation, you could use `transform.basis.xform(ray_directions[i]`
@viniguerrero3 жыл бұрын
@@Kidscancode thank you! But then how do you rotate the vectors for intersection here if they should be vector3? ray_directions[i] = Vector2.RIGHT.rotated(i * 2 * PI / num_rays)
@bruceparker6142 Жыл бұрын
Will this work with the vehiclebody3d node?
@Kidscancode Жыл бұрын
This is about choosing what direction to move. It is completely independent of what kind of movement you then do.
@Edel993 жыл бұрын
OMG... this is hard stuff!! I find it hard to visualize all this vector stuff... :(
@Kidscancode3 жыл бұрын
This is why I typically recommend beginners start working in 2D first. It's easier to get a handle on vectors, transforms, and the like and then move to 3D where it get significantly more complex.
@kuroodo_11 ай бұрын
Do you still have the code for this? Would love to check it out
@anindyaroythetechmaster3 жыл бұрын
Can you please make a video on in app purchases in godot. Your mobile ads video is one of kind still on KZbin. Also, there are no good tutorials for in app purchases. Can you make it for us?
@andrewvickerman19402 жыл бұрын
This is very cool. How is it with performance? I'd like to see the agent code. What you posted doesn't show the rays visually.
@vatrenikrug2 жыл бұрын
no there is no visualy... its just a animation for us i gues..? but dint find project to download and i cant geth it working...
@1littlemaxy3 жыл бұрын
Great! And how can I make AI to choose exactly one of 8 directions, not a one between them? My game is isomectic 2d with 8 directions
@Kidscancode3 жыл бұрын
Maybe just choose the longest interest direction, or round the result to the nearest one.
@itsME-dc4vm3 жыл бұрын
Nice ;D
@CarstenNikiel3 жыл бұрын
Wouldn't it be better to not cast rays backwards at all? None of the rays that are 90 degree or higher to the forward direction will be able to add to the choosen direction. So you might need less rays for those mainly foward driving agents.
@Kidscancode3 жыл бұрын
In this specific example, probably - but this is general algorithm that applies to more than just a forward-moving situation. You could use your backwards-facing rays to decide to hit the gas when somebody's on your tail. You might have multiple "targets" and so your interest array could point in many directions - if one is blocked you'd choose the other.
@CarstenNikiel3 жыл бұрын
@@Kidscancode Yes definitely. But in your example with the dot product you eliminate everything that is backward facing. Just thought about the optimization possibilities. But of course you are correct that in other circumstances you might have need for the rays pointing backwards. Thanks for the awesome video, as always. ;-)
@xegrand75483 жыл бұрын
Can you teach how to make a speedometer in 2d ?
@ShootYourBricks3 жыл бұрын
Just have the arrow set its rotation to -90 + speed or something like that
@ShootYourBricks3 жыл бұрын
The speed would be the linear velocity added together
@temporal_paradox2 жыл бұрын
This is really useful, but you don't actually include the project files like you say, and you don't explain how we can actually see the arrows visualized. There's also the 'debugging' with the mysterious variable and key press that makes it all visible.
@trakegaming792 жыл бұрын
Can yo help me to make the car chase the player by some of your codes I really need that 3d
@Red_Eagle3 жыл бұрын
we want gears and drift!
@re-dxealhgamemaker3070 Жыл бұрын
LIKE.
@elzemgames3 жыл бұрын
Nicee
@bruceparker6142 Жыл бұрын
I wonder how much of this has changed since godot 4.
@Kidscancode Жыл бұрын
The concept of context based steering is the same regardless of game engine. Some minor implementation details will change from Godot 3 to 4, but the general idea works the same way.
@oreomoreo21103 жыл бұрын
sounds expensive
@vaibhavpandey66403 жыл бұрын
how to use raycast in pygame as we use in godot?
@Kidscancode3 жыл бұрын
Pygame doesn't have rays. You'd have to manually build such functionality yourself.
@vaibhavpandey66403 жыл бұрын
@@Kidscancode then what can i use for checking collisions near by to turn my npc
@Kidscancode3 жыл бұрын
Same answer. You have to do something like use a collision shape (ie an invisible sprite) and move it around, checking if it collides with anything.
@hasaranger98453 жыл бұрын
Hey man I have a big request ,i want you to make a sidescroller pygame game tutorial series , you are the only guy i can understand and your code is simple. So PLEASEEE MAKE A PLATFORMER TUTORIAL SERIES I BEG U
@nigeltsuro86313 жыл бұрын
The funny thing is I'm a kid🤣🤣
@saulnores34773 жыл бұрын
This is too complicated for me. I've seen easier methods.
@ShootYourBricks3 жыл бұрын
I made a 3d car game where I had an area in front of the car and if it was touching something, than it would run a function. When the function ran, it would turn right if a raycast on the right side of the vehicle wasn't touching. If the raycast was touching something, than it would turn left if a second raycast wasn't touching. If both raycasts were touching, it would brake. (That was probably super confusing lol.)
@saulnores34773 жыл бұрын
@@ShootYourBricks Hi. Your method is simpler than the one in this video. I like it.
@ShootYourBricks3 жыл бұрын
@@saulnores3477 Thanks! I'm glad you liked it.
@Kidscancode3 жыл бұрын
Use what works for your specific application. This is a generic algorithm that scales well and can be applied to a wide variety of situations, not just cars.
@saulnores34773 жыл бұрын
@@Kidscancode Your channel is great but this method is above my gdscript knolewdge and I don't understand some things in it. May be in the future I will.
@yametekudasai25633 жыл бұрын
please make a full mobile fps game in godot please i beg you😢
@Kidscancode3 жыл бұрын
My channel is about learning how to make games. If you learn *how* to code and use the tools, you can make any game you want. If I just make a full game and you copy it, will you have learned anything?
@yametekudasai25633 жыл бұрын
@@Kidscancode but i dont know how to code😢
@Kidscancode3 жыл бұрын
@@yametekudasai2563 My channel and many others can help you learn.
@yametekudasai25633 жыл бұрын
@@Kidscancode but how😥😥
@nurwaneduardo78312 жыл бұрын
How to make a 3D ai car. Because you toturial in 2D.