EDIT: I have created a more detailed tutorial on this along with additional options on what the AI actor would do if the player moves out of range. You can read this tutorial here.Hi Raven,
If you're looking to make the AI controlled actor move to the player controlled actor AND there is some way he can actually get there (which means the AI actor is not totally sealed off), then you can achieve this using the techniques described below.
First, you will need to understand how to set up basic pathfinding for your actors.
This tutorial explains how to do this in detail. Just remember to use clones with animations instead of clone arrays as highlighted at the start and end of this tutorial.
Once you know how to do this, the rest is really simple. The AI actor only needs to figure out:
1) When to move
2) Where to move
You can achieve both these in a single, easy step. The following code shows how (I'll explain WHERE to use this code later in this thread).
- Code: Select all
if ( distance(Soldier.x, Soldier.y, AI_Actor.x, AI_Actor.y)<=170) {
MoveTo("AI_Actor", 0, 0, 2.5, "Soldier", "Avoid_This");
}
The IF-Statement in the above code checks whether the two actors are close enough to initiate the move. I've used a distance of 170 pixels in the above example, but you can replace this value with anything you require. When the distance between the two actors is less than or equal to the specified distance, the AI actor will start moving towards the player controlled actor (which, in this example, is a Soldier).
The MoveTo function consists of the following parameters:
"AI_Actor" - This is the actor that will be moving
0, 0 - These are the relative X and Y co-ordinates of the actor we will be following
2.5 - This is the speed at which the AI actor will move. I've kept this value 0.5 lower than the player controlled actor so the player can outrun it and so that the AI actor does not stick like a leech to him after reaching him
"Soldier" - This is the player controlled actor that the AI actor will be following
"Avoid_This" - This is the set of cloned objects that will serve as the obstacles to be avoided. In case of this example, they are walls
Now for the but about WHERE to use this code. There are two places you can use this code.
The first is in the "Draw" event. This event executes every frame so long as the actor to which it is assigned is visible on the screen. Using the above code here ensures that the AI actor follows the player's path as closely as possible. It is also the easier of the two methods but has one major drawback. Because the MoveTo command is called every frame, the AI actor tends to move into the actor to be avoided (in this case, the walls) and tends to get stuck there at times. This is due to a bug in this command. This is the main reason why I do not recommend this method although it is easier than the next one.
The second method is the one that I recommend. Create a timer with a duration of 500 (the time is in milliseconds so this is equal to half a second) and set it to recur indefinitely. This timer should be created using the "CreateTimer" function in script editor. The event that causes this timer to be created is the AI actor's "Create Actor" event. This means that as soon as the AI Actor is created (which means that it exists in the game), the timer will execute every half a second. Place the code provided earlier in this post in the Timer event (let me know if you need help with how to work with Timers). The code will execute every 0.5 seconds, which means that the AI actor will follow the player as before but any course correction will take 0.5 seconds to occur. In practice, this is a small amount of time and actually adds a feeling of realism to the game as it doesn't appear unrealistically snappy. Most importantly, this approach eliminates the bug encountered in the first method because the MoveTo function is no longer called on every frame. The result is smooth pathfinding with realistic course correction.
I have tested all that I wrote above and confirmed that it worked as expected. The attached zip file contains a working version of the above example using the second method (which involves a timer). You can left-click a spot on the floor (NOT the walls) to make the player controlled actor (the soldier) move there. Try moving the soldier to the top right corner of the passageway (the AI-controlled actor - the guy in the blue shirt - is on the other side with a small bathroom with checked tiles between the two). When the distance between the two is less than 170 pixels, the guy in the blue shirt (the AI actor) will follow the soldier by walking around the walls and through the doors. Even if you move the soldier away into another room, the AI actor will keep following him until the distance between the two exceeds 170 pixels. Even then, the AI actor will continue moving until the point that corresponds to the soldier's position when he was last 170 pixels away. If you do not want this to happen, you can do either of the following:
1) Add an "else" statement telling the AI actor to either stop moving (use the same MoveTo function with "AI_Actor" in place of "Soldier" so he moves to 0,0 relative to his own position - which means he stops moving)
2) Use different co-ordinates with the view actor or Game center as reference if you want the AI actor to return to a predefined point on the map.