Code archives/3D Graphics - Misc/Entity Chase/Flee

This code has been declared by its author to be Public Domain code.

Download source code

Entity Chase/Flee by Shambler2003
Below are 2 routines.

Chase makes an entity slowly rotate and run after another until it is within a certain distance.

Flee makes an entity slowly rotate and run away from another entity until it is a certain distance away.

Since the routines only adjust Yaw they are suitable for entities which are on a terrain.

I thought I would add these as it wasn't as simple as I first thought =)
;Chase
;assumes earlier 'target=CreatePivot(Monster)'
;Run towards Player until they are 10 units away


If EntityDistance(Monster,Player)>10
PointEntity target,Player
targetangle#=EntityYaw(target,True)
currentangle#=EntityYaw(Monster)

If Abs(currentangle#-targetangle#)<180 Then turn=2 Else turn=-2 

If currentangle#<targetangle# 
TurnEntity Monster,0,turn,0 
Else
If currentangle>targetangle
TurnEntity Monster,0,-turn,0
EndIf
EndIf
MoveEntity Monster,0,-0.5,.5 
EndIf



;Flee
;assumes earlier 'target=CreatePivot(Monster)'
;Run away from Player if they are closer than 100 units

If EntityDistance(Monster,Player)<100
PointEntity target,Player
targetangle#=EntityYaw(target,True) 
currentangle#=EntityYaw(Monster)

If Abs(currentangle#-targetangle#)<180 Then turn=-2 Else turn=2 

If currentangle#<targetangle# 
TurnEntity Monster,0,turn,0 
Else
If currentangle>targetangle
TurnEntity Monster,0,-turn,0
EndIf
EndIf
MoveEntity Monster,0,-0.5,.5 
EndIf

Comments

None.

Code Archives Forum