This tutorial provides a solid architectural foundation for beginners by effectively bridging the gap between animation timing and collision logic. It demystifies the essential interplay of state transitions and spatial queries required for responsive 3D combat.
Deep Dive
Prerequisite Knowledge
- No data available.
Where to go next
- No data available.
Deep Dive
Улучшаем поведение мобов в 3D игре, урок по Godot 4Added:
Hi all. You are on the Gamecrafter channel. We will continue our work with mobs and make their behavior, yes, ours and more like this, [music] yes, as if natural. Before this, we had very primitive behavior. They just ran up and started scratching, like, yeah, they had an old model there, old collisions and so on. Now I have made it so that they look for us, no matter where we go. As soon as they find it, they stop, begin the attack, and the attack happens. That is, yes, the damage is inflicted only at the moment of the attack itself, right at the moment of contact between the palm and our player.
Yes, it looks [the music] much more interesting, of course. Yes, their animation is very slow, it all looks very easy, but it depends on the animation, the speed and so on, yes, but overall it’s already cool to dodge and so on. We'll do this today. I hope this information will be useful. Let's get started.
Let's start by adding new area 3D with collisions to our mob. which will act as detectors for us. That is, one of us will detect, uh, contact with the player, and the second will be a hitbox, that is, a collision, which, when it comes into contact with the player, will cause damage to the player. Let's add them right away. Okay, I'll choose Enemy. Ctrl A.
Let's search for Area 3D here right away, right? Ctrl A.
Select Collision here.
Collision shape 3D.
So, let's add shape to it right away. Instead of Ti, I'd probably choose boxing here. Our box is the simplest figure, without any unnecessary angles. And I'll make it smaller right away.
You can hold down Alt and it will narrow evenly. So.
M. Well, here it is, yes, an arbitrary form. Here, if only it would protrude a little from our colizhina.
Here, by the way, you can determine the distance at which the mob will be in contact with the player. That is, if we set it up like this, then the mob will, yes, detect the player a little in advance. And, let's say, we then write in our code that as soon as the player enters this zone, the mob will stop.
That is, so that it doesn’t just time the player right away, but a little bit in advance.
That is, at some distance from our player, he stopped and began his attack. Well, let's say it's like this. This can be adjusted later. Let's rename it right away. Instead of are 3D I'll write, for example, player detector. Well, you can add are. Area. And here I will write detector collision. Collision detector.
And let's create the second one right away. Ctrl A 3D. Again Ctrl A immediately collision shape 3D. Let's add shape here too.
Boxing again.
I'll rename it right away. Let me write. This will be heitbo for us. And so I will write here heitatbox area. And here I will write Hitbox Collision.
Heit box collision. And here this box can be made even smaller. This is our collision box, which, when it comes into contact with our model, yes, that is, with the collision of our player, will cause damage.
We will turn it on exactly at the moment of impact, yes. And here again, yes, it depends on the hardcore, yes, we can make it really huge there, yes, around there is a mob. And during the attack, even if the player is not standing in front of the mob itself, but somewhere to the side, or even behind it, he can scratch it and so on, that is, it is already at your discretion. But it is possible to make it more, yes, closer to reality, make it smaller and make it exactly, let’s say, where the impact itself will occur. That is, just like this before the bomb. Well, it will be quite simple, of course, to dodge, but this is purely to show how it works, that it can be made really small.
Here we have a palm strike, yes, with the hand and somewhere right here. But we'll straighten this out later. OK. Okay, let's save. And now that we have our collisions, we can add them to our code. So, let's open our Enemy Script. And here let's go below. For now, we wo n't change anything from above. We'll need to add something here.
Well, for now we'll just add a couple of functions here, yes, which will come from our signals from our collisions. That is, the first signal will occur when the player enters the zone of this detector of our mob. We select, yes, player detector area. Select the node tab here. Select signals. And here we look for body entered. And we insert this function into our script enemy. Click connect. Here. Yes, our function. Ah, that is, this is a function that is triggered by a signal when our player detector area, that is, this collision detector, will detect someone, some body. Let's first make sure that the buddy must be a player, because if he suddenly collides with another mob there, it will also be detected as a buddy, and again, damage will be inflicted on the player there, yes, or on someone else, well, or he will stop and act stupid.
Therefore, we need to limit this detector to the player only. To do this, we write the following condition.
Let's write if body. Body is a parameter built into our on body entered function.
Here in brackets we have body - this is a 3D node. Here we can define who the body is. The point is in group. And here we will write player in quotation marks. Yes, we are not writing this for nothing, because at some point we will open our player, here it is. We added him to the group when we created him, right? Here he is. This icon means that he is in a group. And the group is called Player. Here in the groups there is a group called Player. And in this group we only have the player itself. And this gives the opportunity, uh, to our engine to determine, yes, who is in this group, what note, yes, and with this command, it turns out, we limit our detector to only those nodes that are in the Player group. There we have only Player, accordingly. And only in this case will something happen for us, right? For now, let's just write pass here.
And we can also immediately create a function for our hitbox, right? Again, select the heitbox here, go to signals and also add the on body entered function.
Body entered. We insert it into the enemy script. And here everything is the same. You can even copy this command. That's it, we can put it this way for now, right?
Here we will determine, again, if it is a player, then the player's health will be taken away. Let's move on. So, here we need to adjust our code. In general, everything works very primitively now. And here we have, yes, if the chase and the distance are less than the attack range, our animation player starts playing, right? Here is our animation player. Here we have attacks.
And here we simply assumed that it doesn’t matter where the player is located.
And here our attack function comes into play.
We have this function. Here it’s simple, yes, regardless of whether it touches the player somewhere, it just takes away health from the player. We need to change all of this, to make it so that our mob will run not until the distance is shorter, but precisely when it comes into contact with our player. And when there is contact with the player, the attack animation will begin. And during the attack animation itself, the function will not be triggered, yes, just take away health, our hitbox will be triggered. And only if the player is within the hitbox's range, only then will the attack function be triggered. Yes, you can even write here for now about attacks. We also need to fix something. What we have is that when a mob is aggroed, it runs and constantly attacks, attacks, attacks. And we need to make it so that he runs up, yes, and moves to another stage, namely the attack stage, so that he doesn’t always run, run, run, yes, and as if constantly attack.
We need to make it so that Mo runs up, stops when he spots a player, and starts his attack. He completes the attack, yes, regardless of whether it was successful or unsuccessful, he completes it, and then he goes through a kind of attack cooldown. If he spots a player in his attack zone again, he continues to attack. If he doesn’t notice, then he has to run again to catch up with the player until he catches up with him. And for this I will create one more variable. Here at the beginning we have somewhere m here, where we have chase, let's say, I'll create a bar variable, I'll call it can like this. Colon space. This will be a Boolean value for us, so I'll make it false initially. That is, can attack fse, that is, cannot attack, right? When it is true, it means he can attack. And here below, where we have az, yes, here is ifйe.
And now here below, where we have, yes, and where he aggroes our mob, that is, when the distance is less than the agroange. Agroange also remains with us. That is, if a player gets too close, he aggroes and his chase becomes true.
That is, our chase responds, yes, when there is a chase, it means he is chasing the player. That is, when our distance is less than aggro, he runs after the player. And here we also have chзue - this is when his health drops, right? That is, his player is from afar, even there he is not yet in agrorenche, but he shot at him, and he runs after us. And here we also need to weed out that when he is in the can attack state, that is, when he is in the attack state, he should not also aggro, he should kind of ignore, yes, the player’s movement. Therefore, here you can add and write can attack. So now it's a contradiction, that is, when he can attack, he chases, right? But we need to do the opposite. That's why I put an exclamation mark here. This means that when can attack is false, that is, can't attack, only then do we have change true. Now let's go down lower. Let's try to put all this into use. I'll create a couple more functions. And our first function will be, let it be attacked, yes, the beginning of the attack, while the pass and the end of the attack.
Типанатак fin. Yes, the finish is also a pass for now. And here I noticed that if you immediately, yes, turn on the attack started, so that it immediately begins to attack, we still have animation in the process, there are some other things in the process. And they need, in short, to finish first in order to begin these new actions. Therefore, it is advisable to call this function as call defer. Call de we will do. Here we will write started in quotation marks. Yes, it's exactly the same, yes, calling the attack started function, only exactly like in safe mode. She will wait until all these millisecond actions are completed, and only then will she launch a new one. And this will allow us to avoid various bugs and errors there. OK. Here we call attack started. Here, logically, we will need to disable collision so that it can no longer detect the player. This is done to update, right? So, look, this is what happens, right? Our mob runs up, spots the player, starts an attack, attacks, the attack ends, and, let's say, the situation is such that the player remains in the zone of its detector. he didn't come out, that is, buddy didn't come out or come in.
It turns out that this function will not work again and a new attack will not begin. Therefore, when we start an attack, we can turn off this detector and then, when the attack is over, we can turn it on again here. And then, if the player suddenly remains in the same place, this detector will turn on again and will detect the entry of the Buddy, our player, into its zone again. And this function will sort of start up again, and the attack will begin again. So we'll add our collision detector as a variable here. On we remember, yes, how it's done.
Hold down Ctrl and drag here. We release it and get a new variable detector collision. [snorts] Yeah. And in the collision detector, if we go to the inspector, we have this disabled parameter here. We can turn it on and off.
It's on by default, so we can turn it off, right? Where attack started we write, yes, detector collision is disabled, yes, e equals true. All. Thus, when we begin an attack, our mob can no longer detect the player. And also here we can enable our can attack function.
N and is equal to true. And now, when our attacked function is triggered, we turn off ours, that is, this one, update our detector and also turn on can attack, which prevents the next movement. But we need to change the chase itself a little. Here, all the movement happens, yes, when there is a direction, it looks in the direction, and here, the movement itself happens, yes, and we can also turn on all this only when we have an activity, right? This is what we don't need in the chase. We can select all of this, press tab, and delete these extra lines. And now it turns out that we have any movement of the mob only when our chase is active. But here you can do, yes, that else ac equals false. That is, if we don't meet the conditions of aggro, rage, and can, then our mob can't move at all and look for any new directions. In this way we had to restrict movement. That is, our mob now runs up to the player, detects him, and his can attack turns on, which turns off our chase, becomes a fall, and all movement of our mob is disabled. Yes, that's what we wanted, to make our mob stop and start attacking. Now we need to start the attack animation. Yes, we already had it in advance.
Here we have the Animation Player, which we loaded along with the model. Here we have an Id that should play. By the way, you can make it autoplay, right? And so that all the mobs are in our Idle. And we have, uh, a strike animation. We call it punch. And this is what we must turn on when an attack begins. In order to enable these animations, we will need to add a variable, yes, for this animation. That is, we have an animation player here, yes, but this animation player is old, where we simply had this primitive attack. In general, we don’t need it anymore, so, okay, let it be there for now, but here we’ll just replace it, right? I'll make an indent here, click and drag a new variable here.
Yes, they have the same name, so one needs to be deleted. That's it, now we can start animations from here. Let 's say somewhere where the attack started. Let's start the animation first. We write animation player toplay and write in brackets.
And immediately, when our attack ends, so that he starts running again, so that the running animation turns on, yes, somewhere where we have ifйe. Here you can, for example, enable the running animation. Animation player toplay. And let there be run here.
All. Now that we have a chase active, we have the run animation playing. We must have it in a loop. We seem to have made it, yes, a looped run, yes, we have a looped one running here.
And our id is also looped, but our punch is not looped.
Next, in attack finished, we will need to turn on our detector again, so that it updates, yes, to update the player search and, accordingly, turn off can attack.
Let's do it. Here we will simply write detector collision, then disabled is equal to and can attack is also equal to fse. But for now we can’t enable our attack finished function anywhere. That is, yes, we need this to work precisely at the end of the attack. And now we also have, yes, this thing, it is always permanently turned on.
We also need to turn it off by default and turn it on only during our hit animation at a certain moment. So, yes, initially we select this collision from the hitbox and make it disabled, right? If we switch to 3D view, we see that it is sort of gray in color, transparent, but still gray. And we need to turn it on here, and during the animation, for a short period of time, so that it detects the player or not. If it was detected, then our attack function was triggered and the player's health was taken away.
Yes, we can do all this in the animation player, but since this is, uh, imported animation, we need to do some manipulations first, right? We select this file, yes, our mob.
We select the Punch animation we need. And here on the right there is a Save to file enabled option. We need to turn it on and save this animation somewhere in our project. Let's say, you can save it somewhere here in scenes and in Enemy.
Call it something like Punch Animation or something. Animation save. We select skip custom trck here and create a report.
So, we choose punch.
And now we can add new tracks here. First, we need to add our boxision here.
We select it, yes, with animation turned on. And here, where we have disabled, we put the key, click create, right? And here comes a new hitbox track.
It is initially disabled, yes, because we have the disabled box checked. Here, let's say, there goes, goes, goes, there comes a blow somewhere here. At this point we can turn on our collision.
Add the key here. Insert key. Let 's add one more key right away, say, somewhere here almost right away, yes, just a little bit. While holding down Ctrl, I use the wheel to zoom in.
Somewhere around here we insert another key. So.
And I select this middle key and turn it off. I set Value to disabled.
All. Now let's look, yes, at our cube here. At first it’s turned off, then we run the animation like this, it turns on at this moment and turns off again.
In theory, this is all we need.
If I press play, we see that it quickly turns on and off. Let's go to the script, and then this function will be triggered, the attack will be triggered, that is, the damage will be taken away from our player.
And also through this animation we can turn on this attack finished function. To do this, we click on add truck here, select call method truck here, that is, call a function, yes, from some script, we select enemy here, yes, by the way, it sometimes does not appear right away.
Yes, I changed it, yes, I made an idol, turned on punch and this new track enemy appeared. So, and here at the very end, let's say, we can add the animation key here, insert the key, which function we want to call. And our function is called attack finished. Attack finished.
That is, right here at the end, yes, when the animation is played, and the attack finished function is called, and our collision detector is turned on again, and can becomes false. That is, these two conditions must be met, and change will become true again. And as long as че чй is true, he will chase the player. Let's save everything and try to test it.
So here it runs.
We see, yes, that the damage is triggered precisely during the attack, we can dodge. Now, of course, he’s being stupid, yes, the whole thing has become a little easier, but all of this can be customized. That is, the animation here is simply very slow. If the animation was fast, then, of course, it would be possible to make it more complicated, yes. But now everything works correctly.
Here he hits, hesitates, hesitates, and then looks for our player again. Yes, let's check what happens if we stand still. Oh, here comes another one. They will kill us quickly. And here he struck.
Yes, let's reboot. Okay, we'll leave one alive to test. He hit. I'm staying put.
Once the cooldown is over, the attack starts again because its collision has been refreshed.
And to make this all clearer, we can enable visible collision shapes here in the debug.
Then by pressing Play we will see all our collisions, right? Here is a red beam, all the hitboxes, here they are turning on and off. We see everything. We see, yes, that the damage is happening, right? While he is running, the collision is active, then it turns off.
All.
At first glance it seems that this is, of course, generally, yes, very simple.
It would seem that nothing could be simpler, yes, just run up, [music] and, stop, attack and run on. But the problem here is that we need to update, yes, our [music] this collision detector.
Otherwise, if we stand still, the mob will not attack and will be slow, because it will not be able to detect our player again until he leaves and re- enters his m [music] collision. Also, yes, here is the point that our chase needs to be completely disabled by this variable canta, then when he can attack, he cannot move. [music] That is, you need to switch these moments, otherwise the code will also work incorrectly. [music] Also, yes, the moment with the animation player. That is, if we don’t save it here and re-import our animation, we won’t be able to edit it. Here we won’t be able to add our own tracks, like [music], triggering functions or disabling, for example, our hitboxes.
With the help of this we can quite dynamically, [music] yes, control our animation, that is, turn on and off hitboxes at certain moments of the animation. Well, it's cool, in my opinion, it's, yes, quite a delicate work, that it's precisely when the palm comes into contact with the player, only [the music] at that moment will cause damage. And you can also add the triggering of the finished attack function at the end of the animation. That is, the animation itself works as a function call signal. That is, we do not write it through code, but through animation. These are the important points to remember.
I hope this information was useful for you. Yes, of course, I understand that she came out six months later. I don’t know, honestly, whether it [the music] will be relevant to anyone or not. Well, I [snort] hope that it will still be useful to someone. I was absent for a long time, I apologize for that.
Thank you all for waiting, yes, for your support.
I will continue to try to [music] release videos. Of course, not so often anymore. There are problems with time now, but I will still try, [music] ah, to justify at least some of your expectations. Thank you all very much and see you again. Bye bye.
Related Videos
Agentforce NOW AMA: Build with React and Salesforce Multi-Framework
SalesforceDevs
490 views•2026-05-28
How agent o11y differs from traditional o11y — Phil Hetzel, Braintrust
aiDotEngineer
450 views•2026-05-28
WEB TECHNOLOGIES UNIT-2 | Degree 4th sem BCOM Computers web technologies unit-2 full explanation💯✅
LearnwithSahera
1K views•2026-05-29
More tests are always better? How to use AI to identify tests that bring little value
Alliance4Qualification
335 views•2026-05-29
Search Algorithms Explained in 60 Seconds! 🤖💨
samarthtuliofficial
218 views•2026-06-01
People of Game of Thrones using JavaScript DOM
AltCampus
296 views•2026-05-30
Introduction to Problem Solving Part - 1 | Lecture 1 | Intermediate DSA
ascensionix
107 views•2026-05-29
🚀 BCS613C Compiler Design | Module 1 to 5 Schema Evaluation 🔥 | VTU 6th Sem 💯 #VTU #bcs613c #exam
Pranavaa-y4y
104 views•2026-06-02











