I could use some help with my chase state.
Here's what I'm trying to get this code to do:
A: Chase the player (which is working)
B: When the enemy is within range (enemyAttackRadius), it will stop moving and change to the attack state (which is working)
C: If the player moves too close to the enemy, I want the enemy to back up and position itself back to that range (enemyAttackRadius). This way, the player and enemy are always kept apart and can't stack on one another, by having the enemy reposition itself. (this is sort of working)
The enemyAttackRadius = 32
The idea is to always keep the skeleton at 32 pixels from the player, no matter where the player moves.
Here's what I'm trying to get this code to do:
A: Chase the player (which is working)
B: When the enemy is within range (enemyAttackRadius), it will stop moving and change to the attack state (which is working)
C: If the player moves too close to the enemy, I want the enemy to back up and position itself back to that range (enemyAttackRadius). This way, the player and enemy are always kept apart and can't stack on one another, by having the enemy reposition itself. (this is sort of working)
The enemyAttackRadius = 32
The idea is to always keep the skeleton at 32 pixels from the player, no matter where the player moves.
GML:
// Exit Script if Player Doesn't Exist
if !instance_exists(oPlayer) exit;
var _distanceToPlayer = point_distance(x, y, oPlayer.x, oPlayer.y);
if (_distanceToPlayer <= enemyAttackRadius) && (_distanceToPlayer > enemyAttackRadius)
{
// Change to Attack State
state = attack;
}
if (_distanceToPlayer > enemyAttackRadius)
{
// Move and Face Forward
xTo = oPlayer.x;
yTo = oPlayer.y;
var _distanceToGo = point_distance(x,y,xTo,yTo);
dir = point_direction(x,y,xTo,yTo);
if (_distanceToGo > speedWalk)
{
hSpeed = lengthdir_x(speedWalk, dir);
vSpeed = lengthdir_y(speedWalk, dir);
}
else
{
hSpeed = lengthdir_x(_distanceToGo, dir);
vSpeed = lengthdir_y(_distanceToGo, dir);
}
// Sprite Directions
if (dir > 45 && dir < 135)
{
sprite_index = sSkeleton_Walk_U;
}
if (dir >= 135 && dir <= 225)
{
sprite_index = sSkeleton_Walk_L;
}
if (dir > 225 && dir < 315)
{
sprite_index = sSkeleton_Walk_D;
}
if ((dir >= 315 && dir < 360) || (dir >= 0 && dir <= 45))
{
sprite_index = sSkeleton_Walk_R;
}
// Collision
Skeleton_Collision();
}
if (_distanceToPlayer <= enemyAttackRadius)
{
// Move and Face Backward
xTo = -oPlayer.x;
yTo = -oPlayer.y;
var _distanceToGo = point_distance(x,y,xTo,yTo);
dir = point_direction(x,y,xTo,yTo);
if (_distanceToGo > speedWalk)
{
hSpeed = lengthdir_x(speedWalk, dir);
vSpeed = lengthdir_y(speedWalk, dir);
}
else
{
hSpeed = lengthdir_x(_distanceToGo, dir);
vSpeed = lengthdir_y(_distanceToGo, dir);
}
// Sprite Directions
if (dir > 45 && dir < 135)
{
sprite_index = sSkeleton_Walk_D;
}
if (dir >= 135 && dir <= 225)
{
sprite_index = sSkeleton_Walk_R;
}
if (dir > 225 && dir < 315)
{
sprite_index = sSkeleton_Walk_U;
}
if ((dir >= 315 && dir < 360) || (dir >= 0 && dir <= 45))
{
sprite_index = sSkeleton_Walk_L;
}
// Collision
Skeleton_Collision();
}
Last edited: