you got a bit lucky here, i didnt thought i still had it but here is my movement script with joystick and/or keyboard controls.
only tested with xbox controller, i made this a few years back but should still work today (this is an old backup file due to hdd crash).
Maybe it helps you.
#edit
Not sure, search resource section, looks like i have a blend there
################################################## #
# MOVEMENT SCRIPT BY COTAX #
# Blender 2.6+ #
# #
#################################################
from bge import logic, events
def get_keys():
cont = logic.getCurrentController()
gamepad = logic.joysticks[0]
# Set the keys
if gamepad == None:
#keyboard
keyboard = logic.keyboard
# Make an (if key pressed) active check
active = logic.KX_INPUT_ACTIVE
just_activated = logic.KX_INPUT_JUST_ACTIVATED
forwards = keyboard.events[events.WKEY] == active
backwards = keyboard.events[events.SKEY] == active
left = keyboard.events[events.AKEY] == active
right = keyboard.events[events.DKEY] == active
sprint = keyboard.events[events.LEFTSHIFTKEY] == active
jump = keyboard.events[events.SPACEKEY] == just_activated
else:
#controller
joystick = cont.sensors["Joystick"]
dead_zone = 0.3
left = gamepad.axisValues[0] < -dead_zone
right = gamepad.axisValues[0] > dead_zone
forwards = gamepad.axisValues[1] < -dead_zone
backwards = gamepad.axisValues[1] > dead_zone
sprint = joystick.getButtonStatus(8)
jump = joystick.getButtonStatus(0)
return forwards, backwards, left, right, sprint, jump
def get_speeds():
cont = logic.getCurrentController()
own = cont.owner
if 'walk_speed' in own:
#use object properties as speeds
walk_speed = own['walk_speed']
run_speed = own['run_speed']
walk_back_speed = own['walk_back_speed']
run_back_speed = own['run_back_speed']
strafe_speed = own['strafe_speed']
jump_speed = own['jump_speed']
else:
#use default speeds
walk_speed = 2.5
run_speed = 4.5
walk_back_speed = 1.5
run_back_speed = 2.5
strafe_speed = 1.5
jump_speed = 4.5
return walk_speed, run_speed, walk_back_speed, run_back_speed, strafe_speed, jump_speed
def main():
cont = logic.getCurrentController()
own = cont.owner
# Run the script or not
if own['run_script'] == True:
# get the sensors.
groundcheck = cont.sensors['GroundCheck']
# get the keys
forwards, backwards, left, right, sprint, jump = get_keys()[0], get_keys()[1], get_keys()[2], get_keys()[3], get_keys()[4], get_keys()[5]
# get the speeds
walk_speed, run_speed, walk_back_speed, run_back_speed, strafe_speed, jump_speed = get_speeds()[0], get_speeds()[1], get_speeds()[2], get_speeds()[3], get_speeds()[4], get_speeds()[5]
if groundcheck.positive:
# turn sliding off
x = 0.0
y = 0.0
z = own.localLinearVelocity.z
# set speeds
if forwards:
if sprint:
y = run_speed
else:
y = walk_speed
if backwards:
if sprint:
y = -run_back_speed
else:
y = -walk_back_speed
if left:
x = -strafe_speed
if right:
x = strafe_speed
if jump:
z = jump_speed
else:
# turn sliding on when in air
x = own.localLinearVelocity.x
y = own.localLinearVelocity.y
z = own.localLinearVelocity.z
# set the speed
own.localLinearVelocity = [x, y, z]
and set the “axis number” on the joystick actuator, I get movement from the second analog stick. I know this page is ancient, but I’ve heard from another discussion about using Zrot from a “list.” So, how do I use a list to get Zrot?
Which one is first, second and so on depends on your joystick (and your joystick driver). You can’t guaranty the first two axis to be left/right and forward/backward axis. It is a good assumption as most manufacturer map the axis number that way. Additional you need to know the direction of the axis e.g. if positive values mean left or mean right direction.
You need to find the mapping of your joystick
[table=“width: 500, class: outer_border”]
[th]axis[/th]
[th]meaning (Joystick type A)[/th]
[th]meaning (Joystick type B)[/th]
[th]meaning (Joystick type C)[/th]
1
left .. right
right .. left
left .. right
2
forward .. backward
forward .. backward
forward .. backward
3
left twist .. right twist
paddle forward .. backward
n/a
4
paddle forward .. backward
left twist .. right twist
n/a
[/table]
Be aware the list (axisValues) starts with 0. So the index of axis 1 is 0, the index of axis 2 is 1 … . You need to substract 1 to convert the axis number to index:
im not sure but i do know that i had problems with it aswell, thats why i didnt use the axis from the joystick sensor but directly from the controller input.
gamepad = logic.joysticks[0]
and the buttons i grab them from tje joystick sensor
Hold on…so are “twist” and “paddle” just the 2nd analog stick’s left, right, forward, and backward? And are joystick “types” simply the way the controllers are made, and not code? I was hoping there was code for reading the stick’s position along the z-axis, so that I could convert it to object orientation.