Jump script problem

Hey, for a school project I’m working on a simple game. I want to make the character jump using the KX_CharacterWrapper. (it’s necessary to use python since the project is about coding a game)
For the same character, I have a movements script using walkDirection which works fine but the character just won’t jump.


import bge




def main():


    cont = bge.logic.getCurrentController()
    own = cont.owner
    me = bge.constraints.getCharacter(own)
    keyboard = bge.logic.keyboard
        
    me.gravity = 9.81
    me.maxJumps = 1
    cont.jump_speed = 2
    
    if keyboard.events[bge.events.SPACEKEY] == KX_INPUT_JUST_ACTIVATED:
        me.jump()
        
main()



I assume the jump function works differently since the rest of the script like gravity works but I couldn’t find an example. Thanks for any help.

I wouldn’t use the character wrapper, rather use dynamic. Then to make a jump happen you want to detect a press from the spacebar (or any key) as well as the collision of the ground(which you will give a property to called ‘ground’) and apply an upwards linear velocity if both of those return positive:

to give you an idea:


import bge

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

#this is your keyboard sensor
keyboard = bge.logic.keyboard
space = keyboard.events[bge.events.SPACEKEY] 

#this is your collision sensor with no property set 
collision = cont.sensors['collision']

#this variable holds your jump force
jump_force = 5

#if you are pressing space and the object you are touching has a property called ground
if ('ground' in collision.hitObject) and (space == KX_INPUT_JUST_ACTIVATED):
    
    #sets the velocity of the z-axis
     own.setLinearVelocity([5.0, 0.0, 0.0], True)

#otherwise stop the force (stop the jump)
else:
    own.setLinearVelocity([5.0, 0.0, 0.0], False)