• 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!

GameMaker NEED HELP WITH ENEMY AI

E

emiran

Guest
I want the enemy ai to follow the player until the position it can hit but while the player is heading rights it works but unfortunately it doesn't function while heading left.
here is my code:

if State == enemyState.Playerseen{
if (can_see == true){
var lastPlayerPosition = inst.x
if instance_exists(inst){
var distance = point_distance(x,y,inst.x,y);
image_xscale = sign(MeleeCombatPlayer.x-x);
if image_xscale == 0 image_xscale = 1;
if (abs(distance)>30){
var Path = path_add();
mp_linear_path_object(Path,lastPlayerPosition,y,4,oWall);
path_start(Path,image_xscale *walksp,path_action_stop,false);
sprite_index = sSkeletonWalk;
State = enemyState.Playerseen;
//move_towards_point(lastPlayerPosition+(image_xscale * 70),y,walksp);
//sprite_index = sSkeletonWalk;
}
else if (abs(distance) <= 30){
path_end();
State = enemyState.Attack;
}
}
}
}
if State == enemyState.Attack{
sprite_index = sSkeletonAttack;
if (image_index > image_number - 1){
var distance = point_distance(x,y,MeleeCombatPlayer.x,MeleeCombatPlayer.y);
if (abs(distance)>30) State = enemyState.Playerseen;
if (abs(distance) <=30) State = enemyState.Attack;
}
}
 

Anixias

Member
I honestly cannot read that code at all. Why didn't you post it in a code block?
Code:
like = this;
 

Yal

🐧 *penguin noises*
GMC Elder
Why do you use paths for so short distances when you could just use mp_potential_step?
 

Yal

🐧 *penguin noises*
GMC Elder
how to implement it in this code?
Just call it with the proper coordinates, speed, precision and object to avoid, and it will change speed and direction each time it's called. It works by checking if the line from current position to goal intersects the object-to-avoid (e.g., the wall parent), and if there's an object-to-avoid in the way, it changes the movement direction left/right until it's free. It gets around simple obstacles pretty well.
 
Top