Jump

Hi,
I am making the jumping. But it is not working

own.bPress = 0
own.jtimer = 0
if Jump.isPositive() and own.bPress == 0: 
    own.jstate = 1
    own.bPress = 1
    
if own.jstate == 1:
    jspeed = 6
    jumping.setLinearVelocity(0.0,0.0,jspeed,1)
    cont.activate(jumping)
    own.jtimer = own.jtimer + 1

if own.jtimer >= 5:
    jspeed = 0
    own.jtimer = own.jtimer - 1
    jumping.setLinearVelocity(0.0,0.0,jspeed,0)
    cont.deactivate(jumping)
    
if own.jtimer <= 0:
    own.jtimer = 0
    own.jstate = 0
        
if Jump.isPositive() == 0:
    own.bPress = 0

it keeps moving on z axis.
own.jtimer is not working. it is integer. When I press space it only increases with 1

or How do I make the jumping. Any other other way?

Thanks in advance
Regards

You have it so currently you set jtimer to 0 at the beginning of every frame, so of course it only gets as high as 1; because then the next frame comes around and you reset it again. Try putting “own.jtimer = 0” inside the first if statement. However, once the timer reaches 5 it’ll hold there, because you have 2 if statements that will both be true, one of which will add 1 to the timer and the other which will subtract 1. If you get that flaw worked out you’ll have a rather unnatural looking jump, where the character moves up at a constant speed and then suddenly is moving down at a constant speed. What I recommend is to let the physics engine deal with the character in air; all you need is to test if the character is currently on the ground, and if the player has pressed the jump key. Then apply a lot of upward force, or set the Z velocity (but only for one frame!) and let gravity do the rest. You’ll have a much simpler and better looking result.

here’s a simpler way of achieving a jump. it’s Captain Oblivions suggestion translated to code.

import bge
controller = bge.logic.getCurrentController
player_obj = controller().owner
keyboard = bge.logic.keyboard
keyjustactive = bge.logic.KX_INPUT_JUST_ACTIVATED

# create a collision logic brick called Floorcollision 
# which detects when the character is touching the floor
# an always sensor is required as well  
floor = controller().sensors["Floorcollision"]

# change the z value(500.0) depending on how high you want the jump 
if keyboard.events[bge.events.SPACEKEY] == keyjustactive and floor.positive:
        player_obj.applyForce([ 0.0, 0.0, 500.0], True)

please note this is a 2.55 script though it shouldn’t be difficult to change to 2.49

After I posted this thread, I removed my jumping python code (1st post). Then I made the same thing (like both of you suggested and coded), using only logic brick. It is working fine. But I never thought to code it in python.
I will try to convert it :yes:.

Thank you for the reply
Regards

EDIT: Just made it. Result is same like logic brick :yes:.

I made yesterday a blend file with your script (text). And one simple script (text.001). But yesterday the forum was so slow that I wasn’t able to upload the file.
Jump.blend (141 KB)

mine is also same like yours :yes:. Just I added own.bPress so that it won’t jump continuously, while pressing the space key.

Thank for the blend file.
Regards