Having Troubles With sliding and crouching!!

Godot Version

godot 4.3

Question

I was tryig to create a general character controller but when it came down to implementing sliding (on ground), it didn’t work. no matter what I tried it always seems to be that the crouch is dominating the movement, and I would really apreciate it if you could help me with this one.
here is the controller code

	var Input_Dir = Input.get_vector("Left", "Right", "Forward", "Backward").normalized()
	
	if Input.is_action_pressed("Crouch") or is_sliding == true:
		if is_on_floor():
			Curent_Speed = Crouching_Speed
			head.position.y = lerp(head.position.y, Crouching_Head_Position.y, Lerp_Speed * delta)
			
			standing_collision.disabled = true
			crouching_collision.disabled = false
			
			if is_sprinting  == true and Input_Dir != Vector2.ZERO:
				is_sliding = true
				Sliding_Timer = Sliding_Timer_Max
				Sliding_Dir = Input_Dir
			
			is_crouching = true
			is_walking = false
			is_sprinting = false
		
	elif !crouching_collision_check.is_colliding():
		standing_collision.disabled = false
		crouching_collision.disabled = true
		
		head.position.y = lerp(head.position.y, Standing_Head_Position.y, Lerp_Speed * delta)
	
		if Input.is_action_pressed("Sprint"):
			Curent_Speed = Sprinting_Speed
			
			is_sprinting = true
			is_crouching = false
			is_walking = false
		else:
			Curent_Speed = Walking_Speed
			
			is_sprinting = false
			is_crouching = false
			is_walking = true
		
	if is_sliding == true:
		Sliding_Timer -= delta
		if Sliding_Timer <= 0:
			is_sliding = false
			print("sliding_finished")

	Motion_Dir = lerp(Motion_Dir, (transform.basis * Vector3(Input_Dir.x, 0, Input_Dir.y)).normalized(), Lerp_Speed * delta)
	if not is_on_floor():
		if Input_Dir != Vector2.ZERO:
			Motion_Dir = lerp(Motion_Dir, (transform.basis * Vector3(Input_Dir.x, 0, Input_Dir.y)).normalized(), delta * air_lerp_speed)
	if Motion_Dir:
		velocity.x = Motion_Dir.x * Curent_Speed
		velocity.z = Motion_Dir.z * Curent_Speed

	else:
		velocity.x = move_toward(velocity.x, 0, Curent_Speed)
		velocity.z = move_toward(velocity.z, 0, Curent_Speed)
		
	if is_sliding == true:
		Motion_Dir = (transform.basis * Vector3(Sliding_Dir.x, 0, Sliding_Dir.y)).normalized()
		is_crouching = false
	
	if is_sliding == true:
		velocity.x = Motion_Dir.x * (Sliding_Timer) * Sliding_Speed
		velocity.z = Motion_Dir.z * (Sliding_Timer) * Sliding_Speed
	if is_sliding == true:
		head.rotation.z = lerp(head.rotation.z, -deg_to_rad(7.0), delta * Lerp_Speed)
	else:
		head.rotation.z = lerp(head.rotation.z, -deg_to_rad(0), delta * Lerp_Speed)
	
	if Input.is_action_just_pressed("Jump") and is_on_floor():
		velocity.y = Jump_Velocity
		
	pass

is redundant. used like 4 times.

		is_crouching = true
		is_walking = false
		is_sprinting = false

You can use enum to have one variable.( imagine if you add crouch walking, sneaking etc. Your code will be hard to maintain)
You can make basic state machine and You can make label to show which state is active for easy debug.

1 Like