How do I make this 3D character turn

Godot Version

Godot 4

Question

How do I make this 3D character turn left if I press left and turn right if I press right?

extends CharacterBody3D


const SPEED = 5.0
const JUMP_VELOCITY = 4.5



var gravity = 9.8


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


	if Input.is_action_just_pressed("jump") and is_on_floor():
		velocity.y = JUMP_VELOCITY


	var input_dir = Input.get_vector("move_left", "move_right", "ui_up", "ui_down")
	var direction = (transform.basis * Vector3(input_dir.x, 0, input_dir.y)).normalized()
	if direction:
		velocity.x = direction.x * SPEED
	else:
		velocity.x = move_toward(velocity.x, 0, SPEED)

	move_and_slide()

For CharacterBody3D, you can directly modify the rotation like so:

func _physics_process(delta):
    # Your code...

    # Simple
    rotate_y(input_dir.x * delta)

    # Explicit calculation
    var rotationY = global_rotation.y
    rotationY = wrapf(rotationY + input_dir.x * delta, 0, TAU)
    global_rotation = Vector3(0, rotationY, 0)

Use either of the above to rotate your character. If you want to rotate faster, you just have to multiply the rotation by a value e.g. input_dir.x * delta * speed.

the world is now rotating instead of the character


its 2.5d

HAHA! Aaagh - made me laugh.

Umm… but why? You have the script on your CharacterBody3D, correct?
And you only use one of the approaches?

yeah and the camera is attached to it

Ah, well there’s your issue.

why did it make you laugh

how do i make the camera move with it then

at this moment its just rotating endless if i press left.

Your reply came up as a notification on Windows and it was the only thing I saw. Out of context it was slightly amusing.

There’s two ways to fix your issue.

Option #1: Rotate the mesh instead of your character

Get a reference to your MeshInstance for your player and rotate that instead of your CharacterBody3D

@export var mesh

func _physics_process(delta):
    # Simple
    mesh.rotate_y(input_dir.x * delta)

Option #2: Modify the camera setup

Mark the camera as being Top Level. This makes the camera unaffected by its parent’s transform i.e. it will no longer follow the player. You can then write a quick script for your camera that follows the player.
This will avoid the “world rotation” that you’re experiencing.
image

Yes. For your use-case you probably just want the character to either face left or right.

if input_dir.x is not 0:
    global_rotation = Vector3(0, PI * input_dir.x, 0)

Godot Docs GDScript style guide


can you fix this

replace is not with !=
and add indent after the if block (tab)

what do you mean by intent

To compare values, you use equals == or does not equal != operator. Operator is is for checking type (ie. some_object is Node3D). So what you are trying to do is:

if input_dir.x != 0:

And as said above, you need to indent (add tab) the following line to mark it as a new block of code.

As to the camera rotation problem, you can also use RemoteTransform3D and set it to update only position.

Pay attention to the error descriptions:
kuva
It says that input_dir isn’t declared in the current scope (that is, on the block of code). Where do you declare var input_dir? Make sure that it is 1) in the same function 2) not inside an if clause or similiar.