GMS 2 Motion Planning and Pathing

Discussion in 'Programming' started by Tyson Boyer, Apr 17, 2019 at 6:43 PM.

1. Tyson BoyerMember

Joined:
Feb 22, 2017
Posts:
37
I am using a motion planning grid and the pathing functions to have my A.I chase the player. As far as I can tell, the path_start function only uses a positive speed to accomplish this motion, which means I cannot directly use the change in x or y (like I would with the player) as a way to know the direction the A.I is moving.

I was able to work around this by comparing the players x and y to the A.I's, but this way seems convoluted (maybe it is not, I am new to programming so all of my "work arounds" seem convoluted). Is there a way to get the A.I's x and y rate of change when using M.P grids and paths?

2. AlexxMember

Joined:
Jun 21, 2016
Posts:
392
If it's moving on a path,
Code:
`direction`
should tell you the direction it is moving.

3. Tyson BoyerMember

Joined:
Feb 22, 2017
Posts:
37
That makes it really simple, thank you.

4. CoreyMember

Joined:
Apr 8, 2019
Posts:
19
I have something to consider, too. Like if you have a multitude of objects that you want the enemy to face at, I have an example of the enemy facing the nearest object. You can use this from a code of mine:

Step Event:
Code:
```var bowangle = direction-90; // create a local variable 'bowangle' to set whatever direction to -90 (because my sprite is facing (up) 90 degrees, for some reason GMS likes sprites that are facing towards the right (0 degrees)).
if instance_exists(object) //check for the enemy object if it exists
{
if (distance_to_object(object) <= finalRange) //finalRange = 600, this is defined in the create event.
{
inst = instance_nearest(x,y,object); //declare 'inst' as a variable for instance_nearest function
var bowdirection = point_direction(x,y,inst.x,inst.y); //create a local variable 'bowdirection' to point towards the nearest object
image_angle = bowangle + bowdirection; //set the image angle to 'bowangle' and 'bowdirection'. This is not necessary if your sprite is facing towards the right (0 degrees).
}
else
{
inst = noone; //reset inst variable to noone if there is nothing in range, but the object exists.
}
}
if !instance_exists(object) //reset inst variable to noone if the object does not exist.
{
inst = noone;
}
```
Create Event:
Code:
```inst = noone;
finalRange = 600;```
Keep in mind that the distance_to_object checks in all directions (like a circle) at the objects origin. Also, my object has a unique identifier assigned through a ds_map array.

Last edited: Apr 18, 2019 at 2:03 PM