Movement Script in WASD; FOR BGE 2.5!

Hello all!

I have developed a movement script for those who like much more effective means of character control. All I ask is that you give me credit. If credit is not given - I will find you.

Instructions:
This script is extremely easy to implement.

  • Created keyboard sensor named anything you want.
  • Create a controller that has the script attached to it.
  • Create a actuator named “Motion”.
  • You are done :slight_smile:

#Coded by: HyperReal
#Version: 0.1

import bge
from bge import logic
from bge import events

cont = logic.getCurrentController()

motion = cont.actuators['Motion']
key = logic.keyboard.events

WKEY = key[events.WKEY]
AKEY = key[events.AKEY]
SKEY = key[events.SKEY]
DKEY = key[events.DKEY]

yMove = 0.0
xMove = 0.0

if AKEY == 3:
    xMove = 0.0
if DKEY == 3:
    xMove = 0.0
if AKEY == 2:
    xMove = -0.1
if DKEY == 2:
    xMove = 0.1
if AKEY == 1:
    xMove = -0.1
if DKEY == 1:
    xMove = 0.1
    
if WKEY == 3:
    yMove = 0.0
if SKEY == 3:
    yMove = 0.0
if WKEY == 2:
    yMove = 0.1
if SKEY == 2:
    yMove = -0.1
if WKEY == 1:
    yMove = 0.1
if SKEY == 1:
    yMove = -0.1
    
motion.dLoc = [xMove, yMove, 0.0]
cont.activate(motion)

you ought to repost that in [ CODE] tags


#Coded by: HyperReal
#Version: 0.1

import bge
from bge import logic
from bge import events

cont = logic.getCurrentController()

motion = cont.actuators['Motion']
key = logic.keyboard.events

WKEY = key[events.WKEY]
AKEY = key[events.AKEY]
SKEY = key[events.SKEY]
DKEY = key[events.DKEY]

yMove = 0.0
xMove = 0.0

if AKEY == 3:
     xMove = 0.0
if DKEY == 3:
     xMove = 0.0
if AKEY == 2:
     xMove = -0.1
if DKEY == 2:
     xMove = 0.1
if AKEY == 1:
     xMove = -0.1
if DKEY == 1:
     xMove = 0.1
    
if WKEY == 3:
     yMove = 0.0
if SKEY == 3:
     yMove = 0.0
if WKEY == 2:
     yMove = 0.1
if SKEY == 2:
     yMove = -0.1
if WKEY == 1:
     yMove = 0.1
if SKEY == 1:
     yMove = -0.1
    
motion.dLoc = [xMove, yMove, 0.0]
cont.activate(motion)                      

Thank you!

Hey, good job. Some suggestions - if you’re going to do this much in code, you could cut out the Motion actuator by using the applyMovement() function (which, I think, is using relative location setting, not linear velocity) of the object. Still, good job on the script. It’s good to see people getting excited about using Python.

Thank you SolarLune! Yes, I’ve tried the applyMovement() function on the object - however, I can’t seem to get it to continuously apply the movement.

No problem. You could use an object variable to store the movement values. I’m not exactly sure what you mean, though - it should continuously apply the movement specified by default.

If you wanted to add a jumping movement would you just add a

SPACEKEY = key[events.SPACEKEY]
zMove = 0.0

if SPACEKEY = 1:
zMove = 0.1

???

dLoc is not the best choice. It makes the object teleport from one position to the next. This produces problems on collisions.

To make your code more flexible, you can put your max value in a variable e.g.


stepSize = 0.1
...
if AKEY == 2:
    xMove = -stepSize
...

that way you need to change only this variable to get other values.

or much more flexible: Read it from a property:


stepSize = cont.owner("stepSize", 0.1)