I am attempting to write python,
And im not that good at it…
I want to make an object(missile) use a near sensor to feed targets with the property enemy to a track to actuator
from what I see it’s pretty simple, For some reason I just can’t handle another language at the moment
Create a property called ‘property’ on the object running the track to.
Make the value of the property the property you want to track.
Delete your current track to stuff
Add an ALWAYS True Pulse enabled sensor.
Connect it to a PYTHON controller.
Make a new text document in the text window, and paste the code below.
Select the name of the text document in the PYTHON controller.
import bge
cont = bge.logic.getCurrentController()
own = cont.owner
scene = bge.logic.getCurrentScene()
objects = scene.objects
distance=None
object=None
for ob in objects:
if ob==own:
continue
if not own['property'] in ob:
continue
dist = own.getDistanceTo(ob)
if distance==None:
distance = dist
object = ob
if dist<distance:
object = ob
distance=dist
if not object==None:
own.alignAxisToVect(own.getVectTo(object)[1],1,1)
the script of agoose is right
only I change those variable…
import bge
c= bge.logic.getCurrentController()
ob= c.owner
target=0
minDist=9000#(9km)
for i in bge.getCurrentScene().objects:
if "enemy" in i:
dist=ob.getDistanceTo(i)
if dist<minDist:
minDist=dist
target=i
if target!=0:
ob.alignAxisToVect(ob.getVectTo(target)[1],1,1)
ob.linearVelocity[1]=10#ahhhhh ... that chicken!! this line is wrong!
little explanation for the loop…much important for write
XXX=[6,2,4]
XXX is a LIST of number
you can schearc in XXX the number too big with loop in this mode:
CODE
###
XXX=[6,2,4]
big=0
for i in XXX:
if i>big:
big=i
###
so, big=6
HOW WORK THE LOOP REALLY? in this mode:
i=6
if 6>0 #true, pass
big=6
i=2
if 2>6 #false,stop
i=4
if 4>6 #false,stop
repeat 3 time , 3 time is the lenght of the list
"big" get a value only the first time in this case
you can see , the piece of code is fairly ever the same
python required only a bit pratice
(unfortunately is the manual of BGE which lack)
Here ya go:
Just hit P and watch the missile kill all the cubes in the scene.
If the missile’s choices seem erratic it’s just because more than one missile locks on to an object at the same time, and then when it is destroyed has to pick another target.
There is now a few more modules in that script, but I’ve left them out of the blend because they aren’t necessary. (AI against the player mainly.)
I would like to change the homing to have it activate at X distance from an enemy, so the missile flies in one direction, and locks on when its near, I have tried editing the code you provided, but am have some trouble.
Here is what I was going to change that is not working
if ob==own:
continue
if not own[‘property’] in ob:
continue
dist = own.getDistanceTo(ob) if dist<3
continue
if distance==None:
distance = dist
object = ob
if dist<distance:
alright so now I have the missile working but need to set a delay on allowing the missile adder to fire,
So if I fire I have to wait 5 secs before firing again I thought I could do it with frequency trigger on the keayboard, but If I tap the
key rapidly, it still fires
hey, I’m so used to using the static objects(more light on CPU) that I could not move the missile, really, I did not know how!
must be a obj dynamic the missile
this is fully functional and
tested now (2.59)
script for missle:
from bge import logic as BG
from bge import events
c=BG.getCurrentController()
ob=c.owner
sce=BG.getCurrentScene()
if not "TARGETFOUND" in ob:
ob["TARGETFOUND"]=0
if ob["TARGETFOUND"]==0:
minDist=5000
target=0
for i in sce.objects:
if "enemy" in i:
dist=ob.getDistanceTo(i)
if dist<minDist:
minDist=dist
ob["TARGETFOUND"]=i
if ob["TARGETFOUND"]!=0:
ob.alignAxisToVect(ob.getVectTo(ob["TARGETFOUND"])[1],1,0.2)
ob.localLinearVelocity[1]+=5
if ob.getDistanceTo(ob["TARGETFOUND"])<30:
#sce.addObject("framment",ob,50)
ob["TARGETFOUND"].endObject()
ob.endObject()