I wrote such a code once. For a guy that needed a missile to follow it's target. It didn't work for moving missiles though, but it worked great for turret-gun AI.
*Searching my pc for the code now
[edit]
found it!
sync on
`make "bullet"
make object box 1,1,1,10 : point object 1,0,0,100
position object 1,0,0,0
`make target
make object cube 2,5
position object 2,0,0,100
do
`CALL FUNCTION
lock_on(1,2,0.1)
`press left/right to strafe the target left/right
if leftkey()=1 then position object 2,object position x(2) - 2,object position y(2),object position z(2)
if rightkey()=1 then position object 2,object position x(2) + 2,object position y(2),object position z(2)
if upkey()=1 then position object 2,object position x(2),object position y(2) + 2,object position z(2)
if downkey()=1 then position object 2,object position x(2),object position y(2) - 2,object position z(2)
sync
loop
`in a function
function lock_on(id,target,turningspeed#)
`first of all: get -id- data
posx#=object position x(id)
posy#=object position y(id)
posz#=object position z(id)
angy#=object angle y(id)
angx#=object angle x(id)
`get -target- data
targetx#=object position x(target)
targety#=object position y(target)
targetz#=object position z(target)
`calculate the angle between -id- and -target-
yangle# = wrapvalue( angy# - atanfull(targetx# - posx# , targetz# - posz#) )
xangle# = wrapvalue( 90 + angx# - atanfull(targetz# - posz# , targety# - posy#) )
`if angle <180 / >180 then turn left/right or up/down
if yangle# >=180
if yangle#<359.0 then angy# = wrapvalue( angy# + turningspeed# )
else
if yangle#>1.0 then angy# = wrapvalue( angy# - turningspeed# )
endif
if xangle# >=180
if xangle#<359.0 then angx# = wrapvalue( angx# + turningspeed# )
else
if xangle#>1.0 then angx# = wrapvalue( angx# - turningspeed# )
endif
`update object
rotate object id,angx#,angy#,0
endfunction
I knew it worked
[edit] sorry, code not working. Fixed
Immunity and Annihalation makes Immunihalation...