• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GMS 2 NEED HELP WITH ENEMY AI

emiran

Member
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