Motion and Track to python questions

dual_test.zip

This blendfile has two scenes and two problems.

In the motion scene I am using python to reduce
the amount of keys I use, but the problem is
that you have to fully depress one key before
going to another. You cannot slide directly from
one key to another.

In the trax scene I have an object tracking to
an empty, but the object will not stop tracking
or moving when it gets to the object.


w = [119, 2]
d = [100, 2]
a = [97, 2]


if w in button.events:
	move.linV = 0.0, own['speed'], 0.0
else:
	move.linV = 0.0, 0.0, 0.0

if a in button.events:
	rot.dRot = 0.0, 0.0, 0.1
elif d in button.events:
	rot.dRot = 0.0, 0.0, -0.1
else:
	rot.dRot = 0.0, 0.0, 0.0

that fixes the motion script. The main problem is that you had if w: followed by elif a and then elif d:
logically, what you were saying is that if w, a, and d are being pressed at once, only w matters.
also, you had the “stop” condition only occur when no button is being pressed.
My fix is to say of all of the linv keys, only one matters, and if none of the linv keys are pressed, stop linv. Then of all of the drot keys, only one matters, and if none of them, stop the drot. Whether a button is being pressed or not is fairly irrelevant when you care about specific buttons.

(also, it’s a good idea to remove print statements when they’re no longer needed, as they’ll slow things down a bit)

in your tracking scene:

My recommendation is to adapt your script to have something like the following:

scene= GameLogic.getCurrentScene()
oblist = scene.objects
target = oblist['OBout']

if own.getDistanceTo(target) < 1: move.linV = 0.0, 0.0, 0.0	

although if you want to keep it like it currently is,

import GameLogic as g
cont = g.getCurrentController()
own = cont.owner

destination = own['destination']
speed = own['speed']
move = cont.actuators['move']
track = cont.actuators['track']

coll = cont.sensors['coll']

arrived = False#default value
for ident in coll.hitObjectList:
	if str(ident) == 'OB'+str(destination):
		arrived = True
	
if arrived == False:
	move.linV = 0.0, speed, 0.0
	track.object = destination
else:
	print 'there'
	track.object = None
	move.linV = 0.0, 0.0, 0.0

I use the arrived variable much like you were using the motion variable… you could probably still use just the motion variable, but I found it simpler this way. Please note that variables can be defined in the script itself, but there final value will not be remembered for the next time the script runs around. (Similarly, you could just say speed = 10 in the script instead of speed = own[‘speed’])
The main flaw in your script was that coll.hitObject only shows one of the hit objects, and in this case you were hitting multiple objects (the ground and the destination) coll.hitObjectList is what you want

I had solved my motion script problem last night, but your way looks cleaner. Thank you for the help Almost. I’ll try these right now.