Jumping Mechanics in Python

So I’ve added walking animations for all directions and an idle state. Another feature is being added!

Which means more problems for me…

So the player does the jumping animation but it keeps ascending everytime I hold the spacebar. How do I make this stop?


from bge import logic, events


scene = logic.getCurrentScene()
owner = logic.getCurrentController().owner
armature = scene.objects['PlayerRig']


move = 0
moveSideways = 0
<b>Jump = 0</b>
delay = 10
cool = 10


key_none = logic.KX_INPUT_NONE
key_tap = logic.KX_INPUT_JUST_ACTIVATED
key_active = logic.KX_INPUT_ACTIVE
## Movement Keys ##
UPARROWKEY = logic.keyboard.events[events.UPARROWKEY]
DOWNARROWKEY = logic.keyboard.events[events.DOWNARROWKEY]
RIGHTARROWKEY = logic.keyboard.events[events.RIGHTARROWKEY]
LEFTARROWKEY = logic.keyboard.events[events.LEFTARROWKEY]


## Misc Keys ##
SPACEKEY = logic.keyboard.events[events.SPACEKEY]


### Double Tap to run ###
## Tap Counter ##
if UPARROWKEY == key_tap:
    owner["TAP"] += 1
    if owner["TAP"] &gt;= 2:
        owner["TAP"] = 2
        owner["TIMER"] = 0
        owner["RUN"] = True


## Double Tap Timer ##
if owner["TAP"] == 1:
    owner["TIMER"] += 1
    if owner["TIMER"] &gt; delay:
        owner["TIMER"] = 0
        owner["TAP"] = 0
##Factors that will change speed##
SPEED = 1.25


if owner["Armed_Primary"] == False:
    move *= 1.25
if owner["Wounded"] == True:
    move *= 0.75
    SPEED *= 0.75


AimingTarget = scene.objects['PlayerCam']


if AimingTarget["Aiming"] == True:
    move *= 0.50
    SPEED *= 0.50
    if owner["RUN"] == True:
        AimingTarget["Aiming"] == False


## Move ## (NAME, START, END, LAYER, PRIORITY, BLENDIN, PLAY, LYRWGHT, IPO_FLG, SPEED, BLEND)
BLENDTYPE = logic.KX_ACTION_BLEND_BLEND
PLAYTYPE = logic.KX_ACTION_MODE_PLAY
LYRWGHT = 0
IPO_FLG = 0
BLEND = 5


if UPARROWKEY == key_active:
    if owner["RUN"] == True:
        armature.playAction("PlayerRunning", 1, 9, 0, 0, 5, 1, 1, IPO_FLG, SPEED, 0)
        move = -0.12
    else:
        armature.playAction("PlayerWalking", 1, 9, 0, 0, 5, 1, 1, IPO_FLG, SPEED, 0)
        move = -0.08
if DOWNARROWKEY == key_active:
    Striaght = 0.06
    armature.playAction("PlayerWalking", 9, 1, 0, 0, 5, 1, 0, 0, SPEED, 0)
    if Striaght &gt; 0:
        owner.applyMovement((0, Striaght, 0), True)
        
if LEFTARROWKEY == key_active:
    Sideways = 0.08
    armature.playAction("PlayerWalking", 11, 19, 0, 1, 5, 1, 0, 0, SPEED, 0)
    if Sideways != 0:
        owner.applyMovement((Sideways, 0, 0), True)
        
if RIGHTARROWKEY == key_active:
    Sideways = -0.08
    armature.playAction("PlayerWalking", 19, 11, 0, 1, 5, 1, 0, 0, SPEED, 0)
    if Sideways != 0:
        owner.applyMovement((Sideways, 0, 0), True)
## Reset Walk Cool Down ##
elif UPARROWKEY == key_none:
    armature.playAction("PlayerWalking", 0, 0, 0, 2, BLEND, PLAYTYPE, LYRWGHT, IPO_FLG, SPEED, BLENDTYPE)


    if owner["TAP"] == 2:
        owner["TIMER"] -= 1


        if owner["TIMER"] &lt; -cool:
            owner["TAP"] = 0
            owner["TIMER"] = 0
            owner["RUN"] = False


##Player does walking animation with arms if weapon if not armed##


if owner["Armed_Primary"] == False:
    if UPARROWKEY or DOWNARROWKEY or LEFTARROWKEY or RIGHTARROWKEY == key_active:
        armature.playAction("PlayerWalkingArms", 1, 9, 1, 1, 5, 0, 0, 0, SPEED, 0)
    
    else:
        armature.playAction("PlayerWalkingArms", 0, 0, 1, 1, 5, 0, 0, 0, 0, 0)




owner.applyMovement((0, move, 0), True)
<b>##Jumping##</b>

<b>if SPACEKEY == key_active:</b>
<b>     Jump = 1</b>
<b>if Jump != 0:</b>
<b>    armature.playAction("PlayerJump", 0, 10, 0, 0, 0, 0, 0, 0, SPEED, 0)</b>
<b>    owner.applyMovement((0, 0, Jump), True)</b>
<b>if onwer["Armed_Primary"] == False:</b>
<b>    armature.playAction("PlayerWalkingArms", 10, 20, 1, 1, 5, 0, 0, 0, SPEED, 0)</b>



raycast down, if ray hits, then you are on the ground.

add a test to see if he is on the ground…only when he is on the ground and key space is pressed can he jump. I would use a keyboard sensor ‘space’ set to tap…this way it only triggers once.

gangsta = bge.constraints.getCharacter(player)
if gangsta onGround:
now he can jump
else:
he is falling or already jumping…so now he cannot jump

I’m trying my best to refrain of connecting sensors or actuators on the python controller…

Any other alternatives?

Am I suppose to replicate this in Python or logic bricks?

The way I did it was to set the value low enough that it won’t overcome gravity. I found 0.15 worked, so in your code it would look like:

<b>if SPACEKEY == key_active:
</b><b>     Jump = 0.15</b>

That way the longer you hold down the spacebar, the longer you float, which gives you some degree of jump height control.
I forget if I changed the default gravity value, but you can examine the source code: https://blenderartists.org/forum/showthread.php?438242-My-first-BGE-game-Mycellium
In any case, try reducing the jump value until the character avatar eventually falls back down, even when holding down the spacebar.

jumpforce = 6
ground = False
height = 1  #Distance from feet to hitbox origin, plus a little
rayto = (owner.worldPosition[0], owner.worldPosition[1], owner.worldPosition[2]-height)

rayobj = owner.rayCastTo(rayto, 0, "")

if rayobj != None:
    ground = True

if SPACEKEY == key_active and ground == True:
    owner.worldLinearVelocity[2] = jumpforce

Excuse the late reply, but it doesn’t seem to work…

Is there another simpler alternative?

that code is a snippet, things are missing and/or assumed. check the console and work through the errors.

Okay, so I got something figured out. Now the actual problem is transitions. The jumping movement lacks transition, but the animation doesn’t…

I’m also made it into a different script. How do I make the player jump if a ray sensor is positive?

Sorry about this, I’m having some major issues with this segment.