Can someone pls help me and fix this code! I tried using AI but they messed up my movement, and it also would not work to change my mouse mode while I press ESC
here is my script:
extends CharacterBody3D
@export var playerSpeeed = 5.0 @export var jumpForce = 10.0 @export var gravity = 8.0 @export var mouseSensivity = 0.5
@onready var head = $Head @onready var camera = $Head/Camera3D
var direction = Vector3.ZERO
var camera_x_angel = 0.0
I want to have fps movement, and that works. but I have an camera on the node “Head” but it doesnt move around with the cursor. and i want that the camera rotates with the cursor so i can look around.
The input function seems good, other than the mis-capitalized name, you may notice the function is missing it’s blue arrow by the line number; this is the correct signature:
func _input(event: InputEvent) -> void:
Your move_and_slide should only happen during physics, so change _process to _physics_process.
The direction could be massively simplified with Input.get_vector and using basis which I assume you were trying to do but the forward/right vectors don’t rotate like the basis does on it’s own.
func _physics_process(delta: float) -> void:
var input_direction: Vector2 = Input.get_vector("left", "right", "forward", "backward")
var direction: Vector3 = head.basis * Vector3(input_direction.x, 0, input_direction.y)
# you should use a seperate horizontal/vertical velocity so you can preserve gravity
var vertical_velocity := velocity.project(up_direction)
var horizontal_velocity := velocity - vertical_velocity
# assign player movement on a horizontal plane, not overwriting gravity
horizontal_velocity = direction * playerSpeeed
# gravity and jumping, using vertical_velocity
if Input.is_action_just_pressed("jump") and is_on_floor():
vertical_velocity.y += jumpForce
else:
vertical_velocity.y -= gravity * delta
# re-combine horizontal and vertical velocity
velocity = vertical_velocity + horizontal_velocity
move_and_slide()
Make sure to paste code with proper formatting between three ticks ```
# you should use a seperate horizontal/vertical velocity so you can preserve gravity
var vertical_velocity := velocity.project(up_direction)
var horizontal_velocity := velocity - vertical_velocity
# assign player movement on a horizontal plane, not overwriting gravity
horizontal_velocity = direction * playerSpeeed
# gravity and jumping, using vertical_velocity
if Input.is_action_just_pressed("jump") and is_on_floor():
vertical_velocity.y += jumpForce
else:
vertical_velocity.y -= gravity * delta
# re-combine horizontal and vertical velocity
velocity = vertical_velocity + horizontal_velocity
move_and_slide()
you should only have one func _input, the capitalized version is not an override so it does nothing, unlike the lower case _input which is correctly named.
Thank you so much! your a life saver! but I have another problem in the same project now… I’m not forcing you, but on my other topic is the new problem (Realy thank you fot fixing the camera!)