0
$\begingroup$

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] == bge.logic.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.

$\endgroup$
  • $\begingroup$ I think cont.jump_speed should be me.jump_speed, but as I did not find jump_speed in the documentation I suggest you visit this video that contains a motion script and works perfectly. $\endgroup$ – Strapicarus Mar 22 '17 at 5:15
1
$\begingroup$

Use this:

import bge

def main():
    cont = bge.logic.getCurrentController()
    own = cont.owner
    keyboard = bge.logic.keyboard.events

    if keyboard[bge.events.SPACEKEY]==1:
        bge.types.KX_CharacterWrapper.jump(bge.constraints.getCharacter(own)) 
        # make sure the object has "Character"  as Physics Type      

main()

I recently discover that piece of code and it works just great. Hopefully works for you as well

$\endgroup$
  • $\begingroup$ Thanks for that, it was what I was looking for, and it worked very well. $\endgroup$ – Elektro Javorski Jun 29 '18 at 15:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.