Movment script problem

Godot Version

4.4.5

Question

ok so im makeing a game where ya surf down hills (its dumb but who cares) and i made this player movement script here it is,

‘’’ extends CharacterBody3D

var max_speed: float = 10.0
var acceleration: float = 10.0
var rotation_speed: float = 3.0

var jump_speed: float = 12.0
var gravity: float = -24.8

var coyote_time: float = 0.2
var coyote_timer: float = 0.0

var jump_buffer_time: float = 0.2
var jump_buffer_timer: float = 0.0

var current_speed: float = 0.0
var started: bool = false

@onready var camera_pivot = $CameraPivot
var mouse_sensitivity: float = 0.15
var rotation_x: float = 0.0
var rotation_y: float = 0.0
var mouse_delta: Vector2 = Vector2.ZERO

@onready var rock_particles = $RockParticles

func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)

func _input(event: InputEvent) → void:
if event is InputEventMouseButton and event.pressed:
if event.button_index == MOUSE_BUTTON_LEFT:
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
elif event is InputEventKey and event.pressed:
if event.keycode == KEY_ESCAPE:
Input.set_mouse_mode(Input.MOUSE_MODE_VISIBLE)
elif event is InputEventMouseMotion:
mouse_delta += event.relative

func _physics_process(delta: float) → void:
# Camera freelook
if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and camera_pivot:
rotation_y -= deg_to_rad(mouse_delta.x * mouse_sensitivity)
rotation_x -= deg_to_rad(mouse_delta.y * mouse_sensitivity)
rotation_x = clamp(rotation_x, deg_to_rad(-80), deg_to_rad(80))
camera_pivot.rotation = Vector3(rotation_x, rotation_y, 0)
mouse_delta = Vector2.ZERO

var turn_dir: float = 0.0
if Input.is_action_pressed("ui_left"):
	turn_dir = -1.0
elif Input.is_action_pressed("ui_right"):
	turn_dir = 1.0
rotation.y += turn_dir * rotation_speed * delta

current_speed = lerp(current_speed, max_speed, acceleration * delta)
var forward_dir: Vector3 = -transform.basis.z.normalized()
velocity.x = forward_dir.x * current_speed
velocity.z = forward_dir.z * current_speed

velocity.y += gravity * delta

if is_on_floor():
	coyote_timer = coyote_time
else:
	coyote_timer -= delta

if Input.is_action_just_pressed("ui_accept"):
	jump_buffer_timer = jump_buffer_time
else:
	jump_buffer_timer -= delta

if coyote_timer > 0 and jump_buffer_timer > 0:
	velocity.y = jump_speed
	coyote_timer = 0
	jump_buffer_timer = 0

if is_on_floor():
	var floor_normal = get_floor_normal()
	var slide_dir = Vector3(floor_normal.x, 0, floor_normal.z)
	if slide_dir.length() > 0:
		slide_dir = slide_dir.normalized()
		var floor_angle = rad_to_deg(acos(floor_normal.dot(Vector3.UP)))
		var slide_strength = floor_angle / 90.0 + 0.1
		velocity += slide_dir * abs(gravity) * delta * slide_strength
	else:
		velocity.y -= 0.1

move_and_slide()

if rock_particles:
	var floor_angle = rad_to_deg(acos(get_floor_normal().dot(Vector3.UP)))
	rock_particles.emitting = is_on_floor() and floor_angle > 0 '''

but when i open the game it moves you forward when you hit space to start like it should but i cant steer and it wont go up slopes also it broke the buttons somehow

It’s pretty hard to read. It would easier to help, if you could format it properly and may boil down the problem you have.

Camera freelook

if Input.get_mouse_mode() == Input.MOUSE_MODE_CAPTURED and camera_pivot:
rotation_y -= deg_to_rad(mouse_delta.x * mouse_sensitivity)
rotation_x -= deg_to_rad(mouse_delta.y * mouse_sensitivity)
rotation_x = clamp(rotation_x, deg_to_rad(-80), deg_to_rad(80))
camera_pivot.rotation = Vector3(rotation_x, rotation_y, 0)
mouse_delta = Vector2.ZERO

var turn_dir: float = 0.0
if Input.is_action_pressed(“ui_left”):
turn_dir = -1.0
elif Input.is_action_pressed(“ui_right”):
turn_dir = 1.0
rotation.y += turn_dir * rotation_speed * delta

current_speed = lerp(current_speed, max_speed, acceleration * delta)
var forward_dir: Vector3 = -transform.basis.z.normalized()
velocity.x = forward_dir.x * current_speed
velocity.z = forward_dir.z * current_speed

velocity.y += gravity * delta

if is_on_floor():
coyote_timer = coyote_time
else:
coyote_timer -= delta

if Input.is_action_just_pressed(“ui_accept”):
jump_buffer_timer = jump_buffer_time
else:
jump_buffer_timer -= delta

if coyote_timer > 0 and jump_buffer_timer > 0:
velocity.y = jump_speed
coyote_timer = 0
jump_buffer_timer = 0

if is_on_floor():
var floor_normal = get_floor_normal()
var slide_dir = Vector3(floor_normal.x, 0, floor_normal.z)
if slide_dir.length() > 0:
slide_dir = slide_dir.normalized()
var floor_angle = rad_to_deg(acos(floor_normal.dot(Vector3.UP)))
var slide_strength = floor_angle / 90.0 + 0.1
velocity += slide_dir * abs(gravity) * delta * slide_strength
else:
velocity.y -= 0.1
this is the part that deals with turning

You say you can’t steer, do you mean these few lines that should make your player rotate?

var turn_dir: float = 0.0
if Input.is_action_pressed("ui_left"):
	turn_dir = -1.0
elif Input.is_action_pressed("ui_right"):
	turn_dir = 1.0
rotation.y += turn_dir * rotation_speed * delta

Were you able to go up slopes before? if so what code changed?


You can format by pasting your code between three back ticks ```

yes those are for rotation