How do I make an Actor Jump only when he is on the ground.

I'm currently making a game and I trying to make my actor jump by a button (Jump Button) but for some reason he can jump when he is still in the air and I only want the actor is on the ground.
My setup is:

Jump Button:
Rule
When touch is pressed
Do
Change attribute game.jump to 1
Else
Change attribute game.jump to 0

Main Character:
Rule
If game.jump = 1
Do
Timer for 0.4 seconds
Move in direction 90 degrees at the speed of 250
Else
Move in direction 270 at the speed of 0

Gravity:
Accelerate in direction 270 with acceleration 300

Sorry it is so long, hope you can help.

Comments

  • camerondamancamerondaman Member, PRO Posts: 13

    you have a boolean, lets call it "jump", where it turns true when your actor collides with the ground. Then for your requirement to jump you have that the jump attribute needs to be true. Lastly in the same rule that enables your character to jump, change the "jump" attribute to false.

  • paul848paul848 Member Posts: 2

    Thank you So much camerondaman
    This really helped a lot

  • RossmanBrothersGamesRossmanBrothersGames Member Posts: 659

    @camerondaman I would add a little bit to that, as you could still run into some problems with that method, such as you may collide with the side of the ground while falling or something similar.

    For jumping I make an attribute called "grounded" and have it true IF:
    actor overlaps or collides with ground
    and
    if actor's vertical velocity = 0,
    Otherwise: grounded=false
    This way I know the actor is standing on the ground and not just touch it in some way.

    It is also helpful to have this attribute if there are other actions or animations you want to perform when the character is on the ground

    also your jumping code can be simplified

    the timer should not be needed
    if you always have gravity accelerating down you only need to change the vertical velocity and the gravity will gradually change that force from an upward speed to a downward speed just like real physics. When you jump there is one initial force that pushes you upward, gravity does the rest of the work in changing your vertical velocity.

    if "grounded"=true and Jump is pressed
    change vertical velocity to 500 (may need to be higher or lower just play with it)

    the gravity will take care of the rest no timer needed this will help performance.

Sign In or Register to comment.