Godot 4.3 (stable)
The issue at hand…
When I go down a slope with my CharacterBody, it throws me off when I go down, so I basically “jump” off of it. I don’t walk down it. How can I fix this weird issue? I know slopes are annoying. I’m pretty amateur so I apologize if this question is dumb.
Here’s my code:
extends CharacterBody3D
@export var mouse_sensitivity: float = 0.005
@export var SPEED = 20
@export var JUMP_VELOCITY = 8
@onready var camera = $Camera3D
var xform : Transform3D
var yaw = 0.0
var pitch = 0.0
func _ready():
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED)
func _input(event):
if event is InputEventMouseMotion:
yaw -= event.relative.x * mouse_sensitivity
pitch -= event.relative.y * mouse_sensitivity
pitch = clamp(pitch, -PI/4, PI/4) # Restrict vertical look range
rotation.y = yaw
camera.rotation.x = pitch
func _physics_process(delta: float) -> void:
# Apply gravity
if not is_on_floor():
velocity.y -= 9.8 * delta # Default gravity in Godot is ~9.8
# Handle jump
if Input.is_action_just_pressed("ui_accept") and is_on_floor():
velocity.y = JUMP_VELOCITY
# Get movement input and apply it relative to camera
var input_dir = Input.get_vector("left", "right", "forward", "back")
var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
# Update the velocity and move the character
if direction:
velocity.x = direction.x * SPEED
velocity.z = direction.z * SPEED
else:
velocity.x = move_toward(velocity.x, 0, SPEED)
velocity.z = move_toward(velocity.z, 0, SPEED)
if Input.is_action_pressed("ui_cancel"):
get_tree().quit()
move_and_slide()