How to mitigate speed effects on moving platforms?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Diet Estus

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?

:bust_in_silhouette: Reply From: MysteryGM

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.

:bust_in_silhouette: Reply From: Pavel Kotlyar

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

MovingPlatformApplyVelocityOnLeave

PLATFORM_VEL_ON_LEAVE_ALWAYS = 0 — Add the last platform velocity to the motion_velocity when you leave a moving platform.

PLATFORM_VEL_ON_LEAVE_UPWARD_ONLY = 1 — Add the last platform velocity to the motion_velocity 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.

PLATFORM_VEL_ON_LEAVE_NEVER = 2 — Do nothing when leaving a platform.

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

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. <(-_-)>

roboeast | 2022-08-15 00:21

:bust_in_silhouette: Reply From: roboeast

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 get_floor_velocity().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
:bust_in_silhouette: Reply From: zenbobilly

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