Player's "position.y" changes after every jump

Godot Version

Godot4

Question

I copied a basic player controller from a YouTube video and I added to the controller code (in the _physics_process function) the following lines:

if is_on_floor():
print(position.y)

so when Im running around it is constantly printing out a number around 1.5######, but every time I jump, the number is slightly higher or lower when I land. I do not know why this is, and I am having trouble with collisions as a result.

could just be floating point inaccuracies, does this discrepancy cause an issue in-game?

Yes. I have a “trampoline” which is just a flattened cylinder and when you enter its Area3D node (all of which is above the floor, but sitting on the floor), it sets the player’s velocity.y to a greater number. Right when I press play, I can walk into the Area3D and it launches the player as expected, but after landing, or if you jump, land, and then walk into it to try to get launched, it has no affect and the player is not launched, seemingly as a result of the strange change in the player’s position.y.

If it’s too thin it might not trigger for the same floating point inaccuracy reason. Make it pretty thick, have half of it inside the floor, half out. And/or change your trampolines physics material to be bouncy.

It (the Area3D’s CollisionShape3D) is currently 0.5m tall and 0.25m high on the y-axis, so its bottom is flush with the floor. Making half of it on top of the ground and the other half of it under the ground did not change its behavior, and raising it up so that it was floating did make it work more regularly, but I also have a “if _is_on_floor():” check in order for the player to be launched, and if you are launched and land back on the trampoline, it does not launch you again, even if you stand there after landing and wait. I am new to Godot so I do not know how to change its physics material, but I am more interested in figuring out why it is behaving this way and how to fix it than I am in actually getting this trampoline, specifically, to work.

Sounds like the area3d is a good size.

Are you launching the player in a _on_body_entered function? It will only check if they are on the floor they moment the enter the area, which if they jumped they will not be on the floor

Yes, it is an _on_body_entered() function- sounds like that could be (at least part of) the issue. What function should I use instead?

You could trampoline them regardless of being on the floor (remove if is_on_floor), the area3d will be close enough to the floor that players can’t tell anyways.

Removing the “if is_on_floor” does not change anything. I also have a ramp in the scene and the player can kind of just hover (no animations yet) across the bottom, maybe 0.5m, of the ramp, going sideways (across, not up or down (from floor to vertical edge of ramp, to ramp, down the other edge of ramp, back to floor on other side) without changing vertical position any. So I think the player’s position is somehow screwed up, except I can have a print(whatever) run every frame that the player is_on_floor for, and it will print the whole time, so I do not know what is wrong with it.

Could I see the trampoline script?

Could you share your scripts?

Also, make sure your CharacterBody2D has “Moving Platform->On Leave=do nothing”. By default it’s “Add Velocity”, this mostly works on horizontal platforms, your velocity.x will change on leaving the platform based on the platform’s velocity. However, if you have a trampoline (that’s probably working as an “up-down” platform) you might be having that issue every time you leave the trampoline but adding impulse to your velocity.y instead.

image

image

This is all of the code for the “trampoline.”

What we are referring to as the “trampoline” is not a moving platform. It stays still and launches the player upon entrance into a connected Area3D. Also, this project is in 3D, if that matters, not 2D.