BGE: Movement Script

I want My cube to move on the ‘Z’ Axis my 1 blender unit only when I press the ‘W’ Key. In the logic of the cube I have a keyboard sensor named ‘Keyboard’ connected to a python controller selecting the script i want to run. i am new to programming/scripting and i have been trying to learn BGE python for a while now

Can someone please tell me why this script is not working?

I made it, by going back a forth to different pages in the BPY python documentation

OMG i just realized i didn’t even but the script i wrote in the post lol

so this is the first script i ran

import bge

#class Player(bge.types.KX_GameObject):
cont = bge.logic.getCurrentController()
sensor = cont.sensors[“Keyboard”]
obj = cont.owner

for key,status in sensor.events:

if status == bge.logic.KX_INPUT_JUST_ACTIVATED:
    if key == bge.events.WKEY:                
        obj.localLinearVelocity.y += 20
        print("Key Pressed")

then this is the second script i ran

import bge
import mathutils

cont = bge.logic.getCurrentController()
obj = cont.owner
sensor = cont.sensors[‘Keyboard’]
key = sensor.events

if key == WKEY:
obj.worldPosition.z += 1

use a motion actuator. 1 on the brick is 1 bl unit.

but if you have to use python, DO NOT use BPY, use BGE.

import bge

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

keyboard = bge.logic.keyboard.events  # lets use the py kb

wkey = bge.events.WKEY

tap = bge.logic.KX_INPUT_JUST_ACTIVATED
press = bge.logic.KX_INPUT_ACTIVE

if keyboard[wkey] == tap:
    owner.applyMovement((0,0,1), True)

So I forgot to enter the script I made, but it is there now in the original post. But it didn’t work when I used your script either, but just so you know what I’m working with here is the .blend

Attachments

bge move script.blend (453 KB)

fixed some typos and updated the logic. it works now.

Attachments

bge move script.blend (524 KB)

thank you :smiley: