I’m trying to get my movement script working, and stumbled on something interesting: With the script as I have it now, linear velocity is set explicitly to move my character in whichever direction the corresponding key tells it to… Which has the interesting effect of cancelling gravity and inertia! If you move in a circle you can pretty much float. It’s neat, but how do I fix it?
Here’s the code:
import bge
ctrlr = bge.logic.getCurrentController()
obj = ctrlr.owner
snsr = ctrlr.sensors["Keyboard"]
move = ctrlr.actuators["move"]
speed = 0.2
def movement (ctrlr):
if snsr.positive:
# If keys are pressed, figure which and move character accordingly.
for key,status in snsr.inputs:
if status is bge.logic.KX_INPUT_JUST_ACTIVATED or bge.logic.KX_INPUT_ACTIVE:
# ESDF FTW
if key is bge.events.EKEY:
move.linV = tuple (sum (x) for x in zip (move.linV, (0, 1*speed, 0)))
if key is bge.events.SKEY:
move.linV = tuple (sum (x) for x in zip (move.linV, (-0.6*speed, 0, 0)))
if key is bge.events.DKEY:
move.linV = tuple (sum (x) for x in zip (move.linV, (0, -0.8*speed, 0)))
if key is bge.events.FKEY:
move.linV = tuple (sum (x) for x in zip (move.linV, (0.6*speed, 0, 0)))
else:
move.linV = (0, 0, 0)
# Thought that if I added the linear velocity to the movement velocity I'd get the thing to respect physics.
# Instead I got a positive-feedback loop. What do?
# vel = obj.getLinearVelocity (0)
# move.linV = tuple (sum (x) for x in zip (move.linV, vel))
ctrlr.activate(move)
And here is the blend file if you wanna take a look.
resources.blend (6.58 MB)