+1 vote

I am implementing moving platforms in my 2D platformer.

The way it's implemented currently, when my player lands on a moving platform, he is reparented to it so that his position updates in line with the moving platform, which is just a KinematicBody2D with a constant velocity.

Because I use local position in all of my player's movement code, my player is still free to move left and right relative to the moving platform.

This works really well.

I notice, however, that when my player runs along the platform in the same direction as it's moving, there is a sense of moving fast. When he runs against the platform, there is a sense of sluggishness.

I know that relative to the platform my player is moving at the same velocity in both directions, but because he traverses more global distance than normal when moving with the platform, there is a sense of speed, and because he traverses less global distance than normal when moving against the platform, there is a sense of sluggishness.

So my question is: Are there any ways of mitigating these effects? How do classical platformers deal with this problem?

in Engine by (1,581 points)

4 Answers

0 votes

The solution would be to subtract the platforms speed from the players speed variable.

It will have to be subtracted from the speed variable because if you subtract it from the actual velocity then the player will freeze in space when not moving.

by (1,482 points)
0 votes

It looks like in Godot 3.5 and 4.0 there will be a new property

MovingPlatformApplyVelocityOnLeave

https://docs.godotengine.org/en/latest/classes/class_characterbody2d.html#class-characterbody2d-property-moving-platform-apply-velocity-on-leave

PLATFORMVELONLEAVEALWAYS = 0 --- Add the last platform velocity to the motion_velocity when you leave a moving platform.

PLATFORMVELONLEAVEUPWARDONLY = 1 --- Add the last platform velocity to the motionvelocity when you leave a moving platform, but any downward motion is ignored. It's useful to keep full jump height even when the platform is moving down.

PLATFORMVELONLEAVENEVER = 2 --- Do nothing when leaving a platform.

this should probably help with the velocity behaviour on moving KinematicBody2D platforms

by (80 points)

moving_platform_apply_velocity_on_leave is when you leave the platform, not when you are moving on the platform. So it won't help with the strange movement effects while on the platform. <(-_-)>

0 votes

You can subtract the moving platform's velocity from the player, enemy, npc, or any moving object's velocity. But you only want to do it if they are moving, and moving in the opposite direction of the moving platform. For a sideview platformer this is only in the x, either -1 (left) or 1 (right).

So your code might look like this, where dir.x is the input direction and getfloorvelocity().x is the platform's velocity.x.

   if dir.x and abs(get_floor_velocity().x) > 0:
        if sign(dir.x) != sign(get_floor_velocity().x):
            velocity.x -= get_floor_velocity().x

Breakdown of code by line:
1. if they input and the platform is moving
2. if they are inputing in the opposite direction of the platform
3. subtract the platforms velocity

by (18 points)
0 votes

What type of body is your player ? Is it a KinematicBody2D or a RigidBody2D ?

by (141 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.