Trying to make object stop when out of range.

TJKeets

Member
Hi,

I've got a player object and an enemy object.

I want the enemy to move towards the player when within a certain range and then stop when out of range.

This is the code I have:

if (instance_exists(oPlayer))
{
if (distance_to_object(oPlayer)< 100)
{
move_towards_point(oPlayer.x,oPlayer.y, spd)
}
else
{
spd=0;
}
}


When I add the 'else' the enemy object doesn't move at all no matter how close I am.

Can somebody tell me what I'm doing wrong or if there is a better way of doing this?

Thanks,

Tom
 

Simon Gust

Member
The function move_towards_point() will actually modify your instance's built-in "speed" variable. That means, instead of setting "spd" to 0, set "speed" to 0.
 
Top