Problems in implementing reduced control while jumping or sliding

Godot Version



I am trying to add reduced control when the player is jumping or sliding. However I can not seem to find a way to properly implement it. I have tried change the players input by dividing it but that does nothing. If I modify the velocity the player slows down when I don’t what them to. Here is the code I am using for my player controller.

extends CharacterBody3D

var SPEED = 8
var SENSITIVITY = 0.004
var crouching = 0
var sliding = 0
var reduced_control = 1
var bonus_speed_applied = false

@onready var head = $Head
@onready var camera = $Head/Camera3D

# Get the gravity from the project settings to be synced with RigidBody nodes.
var gravity = ProjectSettings.get_setting("physics/3d/default_gravity")

func _ready():
func _input(event):
	if event is InputEventMouseMotion:
		head.rotate_y(-event.relative.x * SENSITIVITY * reduced_control)
		camera.rotate_x(-event.relative.y * SENSITIVITY * reduced_control)
		camera.rotation.x = clamp(camera.rotation.x, deg_to_rad(-60), deg_to_rad(90))

func _physics_process(delta):

	# Add the gravity.
	if not is_on_floor():
		velocity.y -= gravity * delta

	# Handle jumping
	if Input.is_action_just_pressed("space") and is_on_floor() and Input.is_action_pressed("crouch") == false:
		velocity.y = JUMP_VELOCITY
		velocity.x *= 1.5
		velocity.z *= 1.5

	#Handle Crouching
	if Input.is_action_just_pressed("crouch"):
		$CollisionShape3D.scale = Vector3(1,0.5,1)
		crouching = 1

	#Plays the uncrouch animation and changes the collsion shape size
	elif crouching == 1 and Input.is_action_pressed("crouch") == false:
		$CollisionShape3D.scale = Vector3(1,1,1)
		crouching = 0

	#Adds the crouching speed boost and and the reduced control and turns on the sliding varible
	if crouching == 1 and velocity.length() > 5 and is_on_floor():
		sliding = 1
		if bonus_speed_applied == false:
			velocity.x *= 1.5
			velocity.z *= 1.5
			bonus_speed_applied = true
			reduced_control = .1
	#Keeps normal control when not sliding and turns off the sliding varible
		sliding = 0
		reduced_control = 1
		bonus_speed_applied = false
	# Gets the input direction and handle the movement/deceleration.
	var input_dir = Input.get_vector("d", "a", "s", "w")
	#Applys the control constrants
	input_dir.x *= reduced_control
	input_dir.y *= reduced_control

	#Changes the direction
	var direction = -(head.transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	#Makes the velocity
	if direction:
		velocity.x = direction.x * SPEED
		velocity.z = direction.z * SPEED
	#Applys friction when not crouching and on the floor
	if is_on_floor() and crouching == 0:
		velocity.x = lerp(velocity.x, 0.0, 0.1)
		velocity.z = lerp(velocity.z, 0.0, 0.1)
	#Applys friction when crouching
	elif is_on_floor() and crouching == 1:
		velocity.x = lerp(velocity.x, 0.0, 0.01)
		velocity.z = lerp(velocity.z, 0.0, 0.01)
	#Applys the velocity to the player

I think your problem is here, you are overwriting your velocity modifiers for sliding.

After you apply the bonus speed, and the player is still holding a direction it will remove your bonus speed.

The boost from sliding is fine but I can’t figure out how to add reduced control when they are sliding.

I think you missed my point.

That code will remove all velocity modifications.

Let’s say a slide started, you would typically let friction slow you down. This code would make you slide indefinitely as long as you hold a direction. But fortunately the math for reduced control is .1 * SPEED, OR POINT 0.8 which will turn off sliding on the first frame.

If you want, to do control but maintain momentum. You need to add the speed to the velocity.

Oh okay thank you.