How to makes a enemy follow the player

Talk about making games.

How to makes a enemy follow the player

Postby mindcontrol » Fri Aug 05, 2016 12:08 pm

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.
mindcontrol
 
Posts: 24
Joined: Sun Apr 26, 2015 12:54 pm
Score: 2 Give a positive score

Re: How to makes a enemy follow the player

Postby DeltaLeeds » Sat Aug 06, 2016 7:25 am

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...
Attachments
Demo.zip
(471.4 KiB) Downloaded 283 times
Currently: Semi-Active.
Working on: Maybe putting my test games in gamejolt, polishing them a bit, but I still don't know when.
User avatar
DeltaLeeds
 
Posts: 693
Joined: Fri May 06, 2011 12:45 pm
Location: In front of my computer.
Score: 38 Give a positive score

Re: How to makes a enemy follow the player

Postby mindcontrol » Sat Aug 06, 2016 11:27 am

When i try to open the ged file with game editor that happen :? but i already have the last version, i think
Cattura.JPG
mindcontrol
 
Posts: 24
Joined: Sun Apr 26, 2015 12:54 pm
Score: 2 Give a positive score

Re: How to makes a enemy follow the player

Postby mindcontrol » Sat Aug 06, 2016 11:41 am

btw, i dont know how to use angle direction ect. is there a tutorial for them?
mindcontrol
 
Posts: 24
Joined: Sun Apr 26, 2015 12:54 pm
Score: 2 Give a positive score

Re: How to makes a enemy follow the player

Postby DeltaLeeds » Sun Aug 07, 2016 6:01 am

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.
Last edited by DeltaLeeds on Sun Aug 07, 2016 2:16 pm, edited 1 time in total.
Currently: Semi-Active.
Working on: Maybe putting my test games in gamejolt, polishing them a bit, but I still don't know when.
User avatar
DeltaLeeds
 
Posts: 693
Joined: Fri May 06, 2011 12:45 pm
Location: In front of my computer.
Score: 38 Give a positive score

Re: How to makes a enemy follow the player

Postby mindcontrol » Sun Aug 07, 2016 7:17 am

Of course, this helped me a lot, thanks :)
mindcontrol
 
Posts: 24
Joined: Sun Apr 26, 2015 12:54 pm
Score: 2 Give a positive score

Re: How to makes a enemy follow the player

Postby DeltaLeeds » Sun Aug 07, 2016 2:14 pm

mindcontrol wrote:Of course, this helped me a lot, thanks :)

You're welcome. If there's anything unclear about this, just ask. ;)
Currently: Semi-Active.
Working on: Maybe putting my test games in gamejolt, polishing them a bit, but I still don't know when.
User avatar
DeltaLeeds
 
Posts: 693
Joined: Fri May 06, 2011 12:45 pm
Location: In front of my computer.
Score: 38 Give a positive score

Re: How to makes a enemy follow the player

Postby Zivouhr » Fri Aug 12, 2016 4:15 am

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)
City of Rott Game created on Game Editor http://cityofrott.wordpress.com/
User avatar
Zivouhr
 
Posts: 549
Joined: Sat May 17, 2014 2:12 pm
Score: 59 Give a positive score

Re: How to makes a enemy follow the player

Postby DeltaLeeds » Mon Aug 15, 2016 5:03 am

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:
Currently: Semi-Active.
Working on: Maybe putting my test games in gamejolt, polishing them a bit, but I still don't know when.
User avatar
DeltaLeeds
 
Posts: 693
Joined: Fri May 06, 2011 12:45 pm
Location: In front of my computer.
Score: 38 Give a positive score

Re: How to makes a enemy follow the player

Postby Zivouhr » Tue Aug 16, 2016 1:13 am

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)
City of Rott Game created on Game Editor http://cityofrott.wordpress.com/
User avatar
Zivouhr
 
Posts: 549
Joined: Sat May 17, 2014 2:12 pm
Score: 59 Give a positive score


Return to Game Development

Who is online

Users browsing this forum: No registered users and 1 guest

cron