Enemy sprite jitter when following player (SOLVED)

jorex

Member
(this is a video example of my problem)

The enemy object seems to jitter up and down on the yaxis when following the player from the direct left or right.

here is his movement code:
dir1 = sign(obj_player.x - x);
dir2 = sign(obj_player.y - y);

hspd = rspd * dir1;
vspd = rspd * dir2;

x = hspd + x;
y = vspd + y;

x = clamp(x,32, room_width);
y = clamp(y,32, room_height);
 

Roldy

Member
If you were to make a guess then why do you think it jitters like that?

HINT: The enemy could have a greater y than your player's y. Or it could have a lesser y than your player's y. But there is a third possibility isn't there?
 

jorex

Member
If you were to make a guess then why do you think it jitters like that?

HINT: The enemy could have a greater y than your player's y. Or it could have a lesser y than your player's y. But there is a third possibility isn't there?
The hints you've given me are things I've thought of and attempted to control but unsure of how to do. do you think checking that the enemy's y is close to players why and just adjusting for that would be a good solution?
 

jorex

Member
I solved it with this line of code and now I'll add delay to the enemy following on the y
if (y < obj_player.y + 4 && y > obj_player.y - 4)
{
y = obj_player.y;
}
 
Top