Track to closest object containing ‘Player’
Mario cart style RedShell
import bge
cont=bge.logic.getCurrentController()
own=cont.owner
if 'Init' not in own:
own['Target']="Empty"
D=40
scene=bge.logic.getCurrentScene()
for item in scene.objects:
if 'Player' in item:
Dist=own.getDistanceTo(item)
if Dist<D:
own['Target']=item
D=Dist
own['Init']=True
print(own['Target'])
else:
if own['Target']!="Empty":
x=own.getVectTo(own['Target'])
own['TargetName']=own['Target'].name
own.alignAxisToVect(x[1],1,.5)
own.alignAxisToVect((0,0,1),2,1)
own.applyForce((0,10,0),1)
I chose this design, so that it would not iterate through all objects each frame, saving cpu,by only iterating one time on spawn, this could be rethought out and track to nearest by building a list, and then iterating each frame through a smaller list then scene.objects
the only way I can think of making it leaner, would be to use the track to actuator on 2d mode, and just feed it the target, however, if the target dies, the track to will throw a error, so… I chose slightly more logic per frame, so that in the end it was fault tolerant.
Attachments
RedShell (1).blend (465 KB)