You just need a couple of wrapvalue commands in there.
Sync On : Randomize Timer()
`Make Ground
Make Matrix 1,100,100,8,8
`Ghost
Make Object box 1,3,8,3 : Position Object 1,40,5,40
`Dummy Camera Object
make object cube 2,4 : hide object 2
`Set Follow Variable
Follow#=0
`Make Waypoints
Dim Waypoint(5,2)
For WP=1 to 5
Waypoint(WP,1)=(40-11)+rnd(22)
Waypoint(WP,2)=(40-11)+rnd(22)
Make object cube WP+10,2
Position object WP+10,Waypoint(WP,1),5,Waypoint(WP,2)
hide object WP+10
next WP
WP=1
point camera 40,camera position y(),40
Do
`Control Camera
move camera upkey()-downkey()*.3
Yrotate Camera wrapvalue(camera angle y()+(rightkey()-leftkey())*2)
`Place Dummy for Camera
position object 2, camera position x(), camera position y(), camera position z()
`If the ghost isn't following
if Follow#=0
`Find the angle from the ghost to the waypoint
ANG#=AngleY(1,WP+10)
Yrotate object 1, wrapvalue(ANG#)
`Move the Ghost
Move Object 1,.075
`Check if the ghost has reached the waypoint
Dis#=Distance(1,WP+10)
`If the ghost has reached the waypoint, choose a random new one
If Dis#<.1 then WP=rnd(4)+1
`Check if the player gets near the ghost
Dist#=Distance(1,2)
`If the ghost is near, then make him follow
If Dist#<18 then Follow#=1
`If the ghost is following you
else
`Find the angle from the ghost to the player
ANG#=AngleY(1,2)
Yrotate object 1, ANG#
`Move the Ghost
Move Object 1,.058
`Check if the ghost has reached you
Dis#=Distance(1,2)
`If the ghost has reached you, then its game over
If Dis#<2.5 then goto gameover
`If the ghost is too far, make him not follow anymore
If Dis#>50 then Follow#=0
endif
Sync
Loop
gameover:
cls
repeat
sync
until scancode()=0
repeat
text 20,20,"Game Over!"
sync
until scancode()>0
end
function Distance(Object1,Object2)
dist#=sqrt((object position x(Object1)-object position x(Object2))^2+(object position y(Object1)-object position y(Object2))^2+(object position z(Object1)-object position z(Object2))^2)
endfunction dist#
function AngleY(Object1,Object2)
angy#=wrapvalue(atanfull(object position x(Object2)-object position x(Object1),object position z(Object2)-object position z(Object1)))
endfunction angy#
Also made the turning slightly faster due to personal preference.