How to fix movement script

hello BA
i was working on a player movement script to navigate , without any logic bricks but it only works when i press the buttons multiple times and not holding

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

speed = 0.2

keyboard = bge.logic.keyboard
JUST_ACTIVATED = bge.logic.KX_INPUT_JUST_ACTIVATED

if JUST_ACTIVATED in keyboard.inputs[bge.events.WKEY].queue:
     own.localPosition.y += speed
if JUST_ACTIVATED in keyboard.inputs[bge.events.SKEY].queue:
     own.localPosition.y -= speed
if JUST_ACTIVATED in keyboard.inputs[bge.events.DKEY].queue:
     own.localPosition.x += speed
if JUST_ACTIVATED in keyboard.inputs[bge.events.AKEY].queue:
     own.localPosition.x -= speed

Any help ?

try the game engine subforum.

sorry testure i hit the wrong reply button, it was for original poster.

“JUST_ACTIVATED” says it all, perhaps you should look for something that says “KX_INPUT_ACTIVE” instead.

the keyboard have 4 states.
the names explains what they does.

bge.logic.KX_INPUT_NONE

bge.logic.KX_INPUT_JUST_ACTIVATED

bge.logic.KX_INPUT_ACTIVE

bge.logic.KX_INPUT_JUST_RELEASED

Correct Way

2 equals bge.logic.KX_INPUT_ACTIVE

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

speed = 0.2

keyboard = bge.logic.keyboard

if keyboard.inputs[bge.events.WKEY] == 2:
     own.localPosition.y += speed
if keyboard.inputs[bge.events.SKEY] == 2:
     own.localPosition.y -= speed
if keyboard.inputs[bge.events.DKEY] == 2:
     own.localPosition.x += speed
if keyboard.inputs[bge.events.AKEY] == 2:
     own.localPosition.x -= speed

Thanks for the help guys , i tried changing " KX_INPUT_JUST_ACTIVATED " to " KX_INPUT_ACTIVE " and it do nothing , then i copied your script and it still idle , with no bugs showed in the console , what i am doing wrong ?

Srry. I’m a bit tired this week and for some reason my brain though “BGE” instead of “UPBGE”, which is weird, since I saw the keyboard.inputs which is only for UPBGE…

For UPBGE instead of doing == 2 you do .active.

Script

import bge

cont = bge.logic.getCurrentController()
own = cont.owner

speed = 0.2
keyboard = bge.logic.keyboard

if keyboard.inputs[bge.events.WKEY].active:
    own.applyMovement([0,speed,0],1)
if keyboard.inputs[bge.events.SKEY].active:
    own.applyMovement([0,-speed,0],1)
if keyboard.inputs[bge.events.DKEY].active:
    own.applyMovement([speed,0,0],1)
if keyboard.inputs[bge.events.AKEY].active:
    own.applyMovement([-speed,0,0],1)

I also replaced the localPosition transform with applyMovement(), since it’s easier to use.

Now it works ! :smiley: thank you so much ! and get well soon bud

1 Like

for an non-parented object, localPosition and worldPosition are basically the same thing.

also id advise to actually type out True where a boolean is expected, otherwise its confusing to those not fluent in the api. (yes, 0 is false and 1 is true, i know.)