Page 1 of 1

How to makes a enemy follow the player

PostPosted: Fri Aug 05, 2016 12:08 pm
by mindcontrol
Hi, as a title, i'll like to get a metod to make an actor (the enemy) follow another one (the player).
I need the enemy also avoid the objects and follow the actor only if a variable is true.

For now i'm using this:
Enemy->Draw actor->Add action-> Move to.

'Move to' function work nice, but i can't make which it works only if there's a variable active nor make the enemy avoid objects.

Re: How to makes a enemy follow the player

PostPosted: Sat Aug 06, 2016 7:25 am
by DeltaLeeds
MoveTo isn't really recommended to make Enemy AI... I'd recommend using angle, distance, and direction.
Hopefully this demo might help you learn those codes. Not sure if this demo is very efficient but it kinda works.
P.S: Made in 15 minutes, so bugs are to be expected, or inefficient code...

Re: How to makes a enemy follow the player

PostPosted: Sat Aug 06, 2016 11:27 am
by mindcontrol
When i try to open the ged file with game editor that happen :? but i already have the last version, i think
Cattura.JPG

Re: How to makes a enemy follow the player

PostPosted: Sat Aug 06, 2016 11:41 am
by mindcontrol
btw, i dont know how to use angle direction ect. is there a tutorial for them?

Re: How to makes a enemy follow the player

PostPosted: Sun Aug 07, 2016 6:01 am
by DeltaLeeds
Newest version is here...
Everything you should know should be in the .ged, and I don't think there's a tutorial for it yet (Might consider adding it if I have the time...) but if you can't open it, I'll explain what those do here as much as I can...

All the scripts I'll be showing is applied in...

Enemy -> Draw Actor -> Add Action -> Script Editor

Code: Select all
angle = 0;//This will make the enemy move at an angle of 0, which is → (Right)
angle = 90;//Angle of 90, which is ↑ (Up)
angle = 180;//Angle of 180, which is ← (Left)
angle = 270;//Angle of 270, which is ↓ (Down)


With these angle codes, the enemy won't move, because you need to apply a directional velocity for the Enemy actor, add...
Code: Select all
directional_velocity=12;


This will make Enemy move towards the angle at a velocity (speed) of 12.

Now there's a handy script I found from the game Asceroid which made me learn how angle is actually used to replace and customize MoveTo...
Code: Select all
angle = direction(x,y,Player.x,Player.y);//This will make the actor Enemy follow the Player according to the player's x and y coordinates...


You can even customize it a lot more, like...
Code: Select all
angle = -direction(x,y,Player.x,Player.y);//This will make Enemy flee from player.


Now for distance. Distance is also a very handy script...
Code: Select all
if(distance(x,y,Player.x,Player.y)<100)
{
    angle = direction(x,y,Player.x,Player.y);//Enemy will follow Player when the distance is 100 pixels or less from the player.
    directional_velocity = 10;
}
else
{
    directional_velocity = 0;//If Enemy is far enough from player, it will stop and wait until Player is within Enemy's range of 100 pixels.
}

I hope this helped.

EDIT: Minor spelling errors.

Re: How to makes a enemy follow the player

PostPosted: Sun Aug 07, 2016 7:17 am
by mindcontrol
Of course, this helped me a lot, thanks :)

Re: How to makes a enemy follow the player

PostPosted: Sun Aug 07, 2016 2:14 pm
by DeltaLeeds
mindcontrol wrote:Of course, this helped me a lot, thanks :)

You're welcome. If there's anything unclear about this, just ask. ;)

Re: How to makes a enemy follow the player

PostPosted: Fri Aug 12, 2016 4:15 am
by Zivouhr
Good topic and tips.
I do like the move to command, but agree there are other efficient ways of accomplishing the task of an enemy following and avoiding the walls as well. 8)

Re: How to makes a enemy follow the player

PostPosted: Mon Aug 15, 2016 5:03 am
by DeltaLeeds
Zivouhr wrote:Good topic and tips.
I do like the move to command, but agree there are other efficient ways of accomplishing the task of an enemy following and avoiding the walls as well. 8)


MoveTo is a simple command, but it's just not customizable enough. The avoid part of MoveTo doesn't work well at all. Not only does it angle, directional_velocity and direction make the enemy avoid walls and works, it could also make enemies head below or above or at the left or right side of the player instead of touching the player, or make the enemy at sync with the player by doing (as in like a mime).
Code: Select all
angle = Player.angle;
directional_velocity = Player.directional_velocity;

So yeah those built in variables are awesome. :lol:

Re: How to makes a enemy follow the player

PostPosted: Tue Aug 16, 2016 1:13 am
by Zivouhr
Thanks for the tips Jonathang. I tried to open the file but it says I need an updated version of Game Editor, since I have the mac for now. I'll keep that code in mind and test it out. Thanks. 8)