Player doesn't stick to moving walls

Godot Version

4.2

Question

Right now the player has a state that will attach them to a wall. This mechanic works perfectly for static platforms. However when an animatable body moves horizontally or vertically, the player won’t move with it.

What would be that best way to make the player attach to a wall?

Is your player a CharacterBody2D?

If yes, do you keep calling move_and_slide() every frame even when the character is attached to a moving platform?

Yes to both. I have the wall attachment mechanic as a separate state then moving around. The player moves with moving platforms while in other states, it’s just when the player is on the wall that I have this problem.

Looks like moving platform data is set only for floor and wall collisions, if the wall collision is not with CharacterBody2D: godot/scene/2d/physics_body_2d.cpp at a9f444bbc307004c022e85e1b31fcfbd2e73566d · godotengine/godot · GitHub

void CharacterBody2D::_set_collision_direction(const PhysicsServer2D::MotionResult &p_result) {
	if (motion_mode == MOTION_MODE_GROUNDED && p_result.get_angle(up_direction) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { //floor
		on_floor = true;
		floor_normal = p_result.collision_normal;
		_set_platform_data(p_result);
	} else if (motion_mode == MOTION_MODE_GROUNDED && p_result.get_angle(-up_direction) <= floor_max_angle + FLOOR_ANGLE_THRESHOLD) { //ceiling
		on_ceiling = true;
	} else {
		on_wall = true;
		wall_normal = p_result.collision_normal;
		// Don't apply wall velocity when the collider is a CharacterBody2D.
		if (Object::cast_to<CharacterBody2D>(ObjectDB::get_instance(p_result.collider_id)) == nullptr) {
			_set_platform_data(p_result);
		}
	}
}

That’s wierd.

Thanks for the help, but I ended up using the get_platform_velocity() function and then just set the velocity equal to that.

Interesting that it works, because if platform data was not saved, platform velocity should not be updated as well. Meaning that it returns whatever value from the last successful collision.

Have you set platform_wall_layers property by any chance?

I’m not sure.

It’s a property of CharacterBody2D. You can find it in the inspector.

I have the first wall layer set to true, and the moving wall uses the second collision layer. Oddly enough, when I go to change the wall layer to the second collision (the one that the moving wall has), the player will go way faster than the walls speed. So I don’t exactly know what’s going on.

Isn’t it because the player is moved twice? First time by the engine (as the wall platform layer is now set correctly and correctly detected), the second time by your code as you added get_platform_velocity() to the velocity a couple of messages earlier in this thread.

1 Like

ohhhhhhhhh. that makes sense. Thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.