Rotate CharacterBody3Ds direction

Godot Version

Godot 4.2

Hi!
I have a CharacterBody3D Node which is moved by player inputs. When the mouse ist moved, the rotation.x property of the character. So far, it works. But when I now move the character the direction stayed the same. What am I doing wrong? Thanks for any help!
Greetings

1 Like

Can you post the script for your character? It will help us help you debug your code.

Heres my code:


extends CharacterBody3D

@export var speed = 6
@export var gravity = 32
@export var jump_impulse = 10

var target_velocity = Vector3.ZERO
var mid :Vector2
var mouse_position :Vector2
var turn_speed :Vector2
var new_mouse_position :Vector2
var rot :float

func _ready

Input.set_mouse_mode(Input.MOUSE_MODE_HIDDEN)
	mid.y = get_viewport().size.y / 2
	mid.x = get_viewport().size.x / 2
	turn_speed.x = (TAU / 2) / get_viewport().size.x
	turn_speed.y = (TAU) / get_viewport().size.y
	Input.warp_mouse(nullpunkt)
	mouse_position = nullpunkt

func _input(_ev):
	var mouse_motion = -get_viewport().get_mouse_position() + nullpunkt
	mouse_motion /= 100
	
	rotation.y += mouse_motion.x
	if $Camera3D.rotation.x < TAU/4 and mouse_motion.y > 0:
		$Camera3D.rotation.x += mouse_motion.y
	if $Camera3D.rotation.x > -TAU/4 and mouse_motion.y < 0:
		$Camera3D.rotation.x += mouse_motion.y
	rot = rotation.y
	
	Input.warp_mouse(mid)


func _physics_process(delta):
	var direction = Vector3.ZERO
	transform.rotated(Vector3(0, 1, 0), rot)
	if Input.is_action_pressed("move_forward"):
		direction.z -= 1
	if Input.is_action_pressed("move_back"):
		direction.z += 1
	if Input.is_action_pressed("move_right"):
		direction.x += 1
	if Input.is_action_pressed("move_left"):
		direction.x -= 1
	
	
	if direction != Vector3.ZERO:
		direction = direction.normalized()
	
	if Input.is_action_pressed("run") and is_on_floor():
		direction = direction * 2
	
	target_velocity.x = direction.x * speed
	target_velocity.z = direction.z * speed
	
	if not is_on_floor():
		target_velocity.y = target_velocity.y - (gravity * delta)
		direction = direction * 0.8
	
	if Input.is_action_pressed("jump") and is_on_floor():
		target_velocity.y = jump_impulse
	
	velocity = target_velocity
	move_and_slide()
	

​​