Jumping only while on the ground

I have a dynamic character, and right now I can’t seem to get around being able to infinitely jump. I’ve only used logic bricks so far for the jump, and got the physics covered. I just can’t figure out this issue. I only want to be able to jump when my character is on the ground. Including a screen shot below.

Thanks guys, any help would be great.

your logic explicitly says:

when <space> -> force.z=350 + play sound

you never told your game what you really want.

I suggest you add a sensor that measures if you are on the ground. For example a ray sensor or a collision sensor.

You do not need to enable [tap]. It has no effect on your setup.

I suggest to use sensor names that tell the semantic of what they measure. For example “space” tells that the space key is measured. You can easily see that in the configuration. There is no need to repeat it in the name. Imagine you want to change the key … then you would need to rename the sensor. The semantic is why you want to measure the space key. In your case it would be “When the user wants to jump” or “When the user requests a jump”. So you could shorten it to something like “jump is requested” or “want to jump” or simply “jump”. This way you tell what you want, without detailing how it is measured. If it does not work as expected a reader can still guess what it should do. As additional benefit it is easier to differentiate between different requests that are triggered by the same input (imagine mouse click events, typically you have just one mouse button to do that).

“want to jump” --> “jump” + “play jumping sound” + “play jumping action”

“want to jump” + “is on ground” -> AND -> “jump” + “play jumping sound” + “play jumping action”

I hope it helps

For example a ray sensor or a collision sensor.

i would not use a ray to check if you are on a floor, a ray will fail, why?
a ray gets cast from the object origin(orange dot) the problem with this is, if you stand on an object and your on the edge of it (lets say you stand half on the ground) the ray will cast past the object and wont register anything and you aren’t able to move or jump anymore.

I would add a small cube below the player with a collision sensor click the cube, shift click player and link the collision sensor to your jump and or movement actions. now you can jump and move on/at every object even if you stand only half on it.

if you give your player a character physics type you can change simple motion to character motion in motion panel. there’s a jump button over there. i dont know if it detects if you are on the ground but at least it jumps.
sorry if this reply is useless

It depends what you want for your game. It is totally valid to prevent a character from jumping when it is hanging halfway into the air.

This can be treated as a third condition:

  • on ground
  • in air
  • at the edge

If you really care you need options to detect the edge condition.

In my “games” I handle the jumping with two rays (one pointed down and one up, to avoid jumping through the roof) and a check on z component of the local velocity. If there is something down, nothing up and z is ~0, then the character can jump.

To Monster, the “Tap” actually has a huge effect. Without it, I can hold jump and the dynamic character will float up forever. With Tap, I have to let go of space and press it again in order to jump a 2nd, 3rd or 4th time.

To everyone: I was hoping for an easy way to do this with just python- without rays or collision sensors. Maybe a string of code that says something along the lines of
“If ‘bounding sphere’ of ‘dynamic player obj’ is in collision with ground, jump = true
else if bounding sphere is not in collision with ground, jump = false.”

This would be awesome, I just don’t know how I would code it. I love using python much more than anything else in the BGE, contrary to the way many people think, to me it’s much easier and lines of code make more sense to me (easier to read, to).
Yeah, if anyone has an idea of how I could code that I would be deeply in debt to you! But thank you to everyone who replied.

Then look at rayCast if you purely want to use python.

“If ‘bounding sphere’ of ‘dynamic player obj’ is in collision with ground, jump = true
else if bounding sphere is not in collision with ground, jump = false.”

So you need a collision sensor. You can use a ray to check if something is below it, you cant check if it collides that’s why there are collision sensors.

Here is an example of my method:

You want to try it out, i got this setup in resource section of the forum:

Damn, that movement script was just what I was looking for! This script of yours is definitely going to teach me a lot on how it works. Thanks, cotax.

Edit: How do I get the [SOLVED] tag on my thread?

No problem

Edit: How do I get the [SOLVED] tag on my thread?

edit your first post, go advanced set prefix solved.

If you only check for collision with something, the player will also be able to jump if he hits a wall (what should not happen)…

I would make 2 rays at the edges of the player and one in the middle