• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Trying to make a follower follow my player

S

Soco

Guest
I'm making a platformer with a player that has a cat following him around, but I'm having trouble getting the cat to follow him in a natural way. I'm currently using a ds_queue(a function that I only just discovered) to get the cat to follow him, but if anyone has a better idea, I'm open to suggestions. Anywho, here's my code as it stands now.

Code:
var dist = point_distance(x,y,obj_Player.x, obj_Player.y)
var dir = obj_Player.image_xscale;

if (dist >= 48){
    //Follow
    ds_queue_enqueue(followQueue, (obj_Player.x));
    ds_queue_enqueue(followQueue, obj_Player.y);
    lag_steps = 6;
   
    if (ds_queue_size(followQueue) > lag_steps*2) { 
        x=ds_queue_dequeue(followQueue)-obj_Player.image_xscale*48;  //xoffset
        y=ds_queue_dequeue(followQueue)+2;  //yoffset
        image_xscale = dir;  //makes the cat look the same direction as the player
    }
}else{
    ds_queue_dequeue(followQueue);  //don't log while the player is within 48 pixels of the cat
}
However, the cat doesn't react to solid objects...which is wierd sometimes. Also, if the player lands on a ledge, the cat just kinda floats in midair instead of falling to the ground. Could anybody tell me if I'm heading in the right direction to make the desired effect happen, or is there an easier function/code that I could be trying instead?
 
S

Snail Man

Guest
The short answer is that it depends on what exactly you want the following to look like: Do you want the cat to sort of follow alongside the player a bit behind, or do you want the cat to exactly mimic the player's moves, following exactly in his footsteps a second or two behind? The latter is much easier (and what you appear to be doing right now), but it also has major issues if your game world is very dynamic. For example, if there's any sort of moving block, the cat will likely jump on thin air, because if it has no physics of its own, it will just act as if there's a platform there, like there was when the player was there.
If this is a major issue, or if you prefer the former option, then you'll have to do the much more complex task of coding a following platform AI for the cat, so that he doesn't exactly follow in the player's footsteps, but just uses the player as a sort of goal to navigate to. I don't know a ton about platform AI, but a good start would be having the cat follow the player's left and right movements, then making it jump if the player is above it, then implementing some sort of path finding.
 
R

rui.rosario

Guest
Taking the AI idea of @Snail Man, one way you could go about it is thinking of the platformer as being a series of blocks (much like the grid in the room editor).

You would then calculate the MP grid from the cat to the player and attempt to follow it. However, you could not follow it blindly or the cat would hover/fly vertically.

You could instead follow all horizontal differences but when the difference was vertical you would just jump to get over with it. This could be some rudimentary path finding. Of course it is sub-optimal and in its simplest version a simple coded AI would do the same. The main pro I think it is that it would react to dynamic worlds and could react to a situation were if the cat didn't move it would get squished. It also uses functions from GameMaker that at a least you would learn to use (if you don't know already).
 
S

Soco

Guest
Hey thanks both of you. I going to read up on those MP grids because those look like a much better option. Good to know when I'm on the right path.
 
Top