Making an AI Actor Follow the Player Controlled Actor

Learn how to make certain types of games and use gameEditor.

Making an AI Actor Follow the Player Controlled Actor

Postby bamby1983 » Fri Feb 22, 2013 4:10 pm

Scenario Covered by This Tutorial:
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).


Prerequisites:
In case there are obstacles (such as walls, rocks or other objects) between the two actors (i.e., there isn't always an open, straight route between them), 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.

You do not need this if your game does not involve any obstacles and the path between the player and AI actors is always expected to be a straight line.


Procedure:
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 (and NOT recommended) method is to place the above code in the AI actor's "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.

The attached zip file contains a working version of the above example using the second method (which involves a timer). The soldier at the bottom-left of the screen is the player controlled actor, while the three actors in the top-right of the screen are AI controlled. 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 spot indicated in the screenshot below. When the distance between the player and an AI controlled actor is less than 170 pixels, the AI actor will start following the soldier by walking around the walls and through the doors.

Image


Each of the 3 AI-Controlled actors behaves differently. We'll take a closer look at each of them below.


1) AI_Actor: The guy in the light blue shirt:
Even if you move the soldier away into another room, this 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. This is not what we usually want, so the following actors show us how to avoid this from occurring.


2) AI_Actor_3: The smiley face:
As soon as distance between the soldier and the smiley face actor exceeds 170 pixels, the smiley face AI Actor will stop moving immediately and remain at its current position. If the soldier comes into range once more, the actor will resume motion.

This can be achieved by adding an "else" statement telling the AI actor to "move to" its current position. To do this, use the same MoveTo function with "AI_Actor_3" in place of "Soldier" so it moves to 0,0 relative to its own position, which means it stops moving. The following is the code within this actor's Timer that can be used to achieve this.

Code: Select all
if ( distance(Soldier.x, Soldier.y, AI_Actor_3.x, AI_Actor_3.y)<=170) {
    MoveTo("AI_Actor_3", 0, 0, 2, "Soldier", "Avoid_This");
                                                                  }
else {
    MoveTo("AI_Actor_3", 0, 0, 2, "AI_Actor_3", "Avoid_This");
     }


The problem with this method is that the AI actor will stop following the player if the path along which it is travelling to get to the player takes is over 170 pixels away from him. If this occurs, the AI actor will just freeze at the spot until the player moves into range again.


3) AI_Actor_2: The girl in the blue t-shirt:
As soon as the distance between the player and this AI-controlled actor exceeds 170 pixels, the AI-controlled actor starts moving back to a predefined position. This can be achieved by inserting predefined "return" co-ordinates with the "Game Center" as the reference. The following code shows how this can be achieved.

Code: Select all
if ( distance(Soldier.x, Soldier.y, AI_Actor_2.x, AI_Actor_2.y)<=170) {
    MoveTo("AI_Actor_2", 0, 0, 1.6, "Soldier", "Avoid_This");
                                                                  }
else {
    MoveTo("AI_Actor_2", 175, -119, 1.6, "Game Center", "Avoid_This");
     }


In the above code, 175 and -119 are the X and Y co-ordinates of the point to which we want the AI-controlled actor to return to when the player is no longer within range. The disadvantage of this method is that if the player moves out of range while the AI-controlled actor is moving towards it as a result of the path along which it needs to move, the AI-controlled actor will move to-and-fro in a narrow region around the area where the range is exceeded.


In order to work around the issues for the last two actors above, it is advisable to ensure that the path along which the AI-controlled actor needs to travel does not take it beyond the distance needed to initiate this move. Otherwise, you may need to use additional code to work around this.

In case you're wondering why the latter two AI actors have slower movement speeds than the first, it is to help them stand a better chance at moving out of range when the player is moving away from them.

This concludes our tutorial on how to make an AI actor follow a player-controlled actor.
Attachments
Pathfinder Basics.zip
(595.69 KiB) Downloaded 279 times
bamby1983
 
Posts: 112
Joined: Tue Jul 31, 2012 11:36 pm
Score: 8 Give a positive score

Re: Making an AI Actor Follow the Player Controlled Actor

Postby RCKTNEO » Tue Jul 18, 2023 3:10 am

Oh, god! I was searching for an tutorial like this a lot of time! Thanks bamby1983. I am new in this forum but i knew a bit about GE, anyway, thanks for this. :D
RCKTNEO
 
Posts: 2
Joined: Fri Nov 04, 2022 9:54 pm
Score: 0 Give a positive score


Return to Tutorials

Who is online

Users browsing this forum: No registered users and 1 guest

cron