

Let's playtest the game in its current state. Notice that while I'm adding an artificial think time before the AI can attack, there is no such think time for movement, so the NPC will start moving as soon as the game begins. If is_in_range & ai_think_time_timer.time_left = 0: If is_in_range & state = 0 & target.get_state() = 0:įinally let's write the _process() function that will orchestrate these NPC actions. Now we can write our decide_to_attack() function in the NPC script, and the trigger for the actual attack() function when the AI think time timer completes. We'll be checking the state value of characters often so let's add a getter on this attribute in the Character script. If both characters are still alive and in range by the time the timer finishes counting, the NPC will attack the player.Ī character will only be allowed to attack another character if their target's state is 0, meaning that the target character is alive. The _process() function will call a decide_to_attack() function that starts the timer. The timer will be started when the NPC comes into range of the player. We don't want it to be able to attack as soon as it becomes in range, otherwise it would be unplayable, so we'll set an AI think time attribute and set up a timer as soon as the game loads.Īi_think_time_t_wait_time(ai_think_time)Īi_think_time_nnect("timeout", self, "on_ai_thinktime_timeout_complete") We're using the move_and_slide() function from the KinematicBody2D object just like we do for the Player. These two functions will get the NPC moving in the Player character's direction when they are called. Var motion = direction.normalized() * speed For the moment we want NPCs to move towards the player at all times in a straight line. As long as the NPC is alive it will either move or attack. The AI will be controlled in the _process() function that's executed at every frame. I'm going to build a basic AI that will control the NPC character and make it move and attack the Player character. Now as long as the NPC doesn't fight back this won't be much of a game, so let's change that today. When that happens, the NPC character disappears and the game is finished. When the NPC takes damage, an animated head-up display registers its decreasing health until it has no hit points left. The Player character can be controlled by the player it can move and attack the NPC. We have an arena with a Player and an NPC character. If you're tuning in for the first time, I'm building a game called Cyberglads using the Godot game engine.Īfter the previous episode we have something that's starting to look like a game, but that doesn't offer a challenge to the player. Thanks for tuning in to this fourth installment in the Making Cyberglads series.
GODOT PATHFINDING SERIES
It's the fourth part of a series on game development where I'll be building an entire game step by step and sharing the process in public. For this reason, we’ll instead use the path’s points as “targets” for the body to move towards.This is a lightly edited transcript of the above Youtube video. However, if you’re using a kinematic body, this will cause problems with collisions, because you’re not using the body’s movement methods. You can use PathFollow2D to automatically move along a path. Use the “Control points” mode to adjust the “curviness” of the line. If you want a closed curve, the “Close curve” button will connect the last point to the first one. Select the “Add points” button and click to start adding. Don’t make it a child of the patrolling entity, though - or the path will move along with the player! Drawing the pathĪfter adding the Path2D node, you’ll see some new buttons appear above the viewport: You can add the Path2D as a child of your main scene, your map, or another location that makes sense. In this solution, we’ll use Godot’s Path2D node (or Path for 3D) as a convenient way to draw paths in the editor.

There are many ways to approach this problem. You want a character to follow a pre-defined path, such as a guard patrolling or a car following the road.
