A distance check should suffice. Find the distance between the player and the enemy:
function within_dist_objects(obj1,obj2,maxdist#)
result=0
x1#=object position x(obj1)
x2#=object position x(obj2)
y1#=object position y(obj1)
y2#=object position y(obj2)
z1#=object position z(obj1)
z2#=object position z(obj2)
dx#=x2#-x1#
dy#=y2#-y1#
dz#=z2#-z1#
dist#=maxdist#*maxdist#
if ((dx#*dx#)+(dy#*dy#)+(dz#*dz#)) <= dist# then result=1
endfunction result
This is really the pythagrean formula without the square root. It takes the input of 2 objects, and the maximum distance you want to check between the 2. If the objects are within that maximum distance, the function returns a 1. Here is and example of it working:
set display mode 800,600,32
sync on
sync rate 60
autocam off
hide mouse
rem make a matrix for reference = each square is 100 units
make matrix 1,1000,1000,10,10
rem make a cube to move around
make object cube 1,25
position object 1,0,13,25
rem make a sphere to get the cube close to
make object sphere 2,25
position object 2,500,13,500
color object 2,rgb(255,0,0)
rem the maximum distance to test closesness in
maxdist#=100
do
rem move object around
gosub _move_object
rem test distance between cube and sphere
nearby=within_dist_objects(1,2,maxdist#)
if nearby
a$="TRUE"
else
a$="FALSE"
endif
text 0,0,"Are they objects within "+str$(maxdist#)+" ? "+a$
sync
loop
end
_move_object:
rem forward backward
move object 1,(upkey()-downkey())*5
rem rotate with mouse
yang#=wrapvalue(object angle y(1)+mousemovex())
yrotate object 1,yang#
rem make camera follow cube
ox#=object position x(1)
oy#=object position y(1)
oz#=object position z(1)
set camera to follow ox#,oy#,oz#,yang#,200,50,10,0
return
function within_dist_objects(obj1,obj2,maxdist#)
result=0
x1#=object position x(obj1)
x2#=object position x(obj2)
y1#=object position y(obj1)
y2#=object position y(obj2)
z1#=object position z(obj1)
z2#=object position z(obj2)
dx#=x2#-x1#
dy#=y2#-y1#
dz#=z2#-z1#
dist#=maxdist#*maxdist#
if ((dx#*dx#)+(dy#*dy#)+(dz#*dz#)) <= dist# then result=1
endfunction result
Each square on the matrix is 100 units, so when the cube is within 1 square of the sphere, the distance check should flag true (I set maxdist# to 100).
Enjoy your day.