Why is characters sliding?

Godot Version

4.7.2

Question

Just making this basic platformer which only walks on X axis, and jump.

Currently there is two players( local co-op). But there is something with velocity which I’m not sure how to fix when I jump on other character it slide to side.

Video demonstration

Here is full code for character, for both is same apart from input_actions.

Inspector is just default CharacterBody3D

extends CharacterBody3D

const SPEED = 7.5
const JUMP_VELOCITY = 20
@onready var anim_tree: AnimationTree = $AnimationTree
@onready var anim_state: AnimationNodeStateMachinePlayback = anim_tree.get("parameters/playback" )

var state: String = "Idle"

func _ready() -> void:
	anim_state.travel(state)

func _physics_process(delta: float) -> void:
	# Add the gravity.
	if not is_on_floor():
		velocity.y += -90 * delta

	var input_dir := Input.get_action_strength("ui_right") - Input.get_action_strength("ui_left")
	velocity.x = input_dir * SPEED
	move_and_slide()
	
	if input_dir != 0:
		rotation.y = PI/2 if input_dir > 0 else -PI/2 


	# State Machine Logic
	var new_state = state
	if Input.is_action_just_pressed("ui_up") and is_on_floor():
		new_state = "Jump"
		velocity.y = JUMP_VELOCITY
	elif not is_on_floor():
		new_state = "Jump"
	elif is_on_floor():
		if state == "Jump":
			new_state = "Land"
		if abs(input_dir) > 0.1:
			new_state = "Walk"
		else:
			new_state = "Idle"
		
	if new_state != state:
		state = new_state
		anim_state.travel(state)

I see two separate issues here.

  1. Your players are not locked on the Z-axis, allowing them to shift forwards and backwards.
  2. Your players have Moving Platform set to Add Velocity, which is fine for most things but can cause odd physics interactions.

To fix the first one I recommend Gertkeno’s solution here: How do I restrict the movement to the horizontal axes?

Apply this to the Z axis instead of the Y axis, and it should work. I’d personally add position.z = 0 as well to make doubly sure they stay locked to 0 on the Z-axis.

To fix the second you have two options. Easiest would be to set Moving Platform to Do Nothing. This should stop the launching of players balanced on the heads of others, but would mean that players interacting with moving platforms and other moving objects won’t have physics applied when jumping or falling off said platforms.

The other option is to leave it on Add Velocity and instead remove all the Floor Layers, leaving just one for each player, making sure each player is on a different layer. This lets you choose which “moving platforms” are considered as such.

So I have added

	velocity.z = 0
	move_and_slide()

and changed

It happens less, but still not completely prevented.

Adding both, actually fixed it.

	velocity.z = 0
	position.z = 1.5