Simple 3D camera rotation with keyboard

Godot Version

4.2.2

Question

Hello!

I’m trying to rotate a 3d first person camera with the keyboard (like in old 3d games).
I just wanna do look left/right and up/down with the arrow keys.

I’ve tried a million things (rotating the character, rotationg the camera…) and always it kinda works, but if i press the keys quickly and randomly the camera tilts and ends up upside down or similar.

This is expected as the Godot Docs say:

It happens exactly as the docs say, but i can’t make the solution to work, and i can’t use internet solutions because all of them are for mouse, and i need to move the camera with the keyboard.

Can someone tell me how to do it?

Thank you very much!

extends CharacterBody3D

var camera_speed = 0.1

func _process(delta):
	if Input.is_action_pressed("ui_left"):
		rotate_y(camera_speed)
	if Input.is_action_pressed("ui_right"):
		rotate_y(-camera_speed)
	if Input.is_action_pressed("ui_up"):
		rotate_x(camera_speed)
	if Input.is_action_pressed("ui_down"):
		rotate_x(-camera_speed)

Check the “Axis order” section of the article you linked

You could rotate your player around the y axis and your camera around the x axis

I’ve read that part of the doc but still can’t figure out how to make that simple code: rotate up/down, left/right with the keyboard.

Nobody knows how to do it? :S

this also doesn’t work:

func _process(delta):
	if Input.is_action_pressed("ui_up"):
		transform.basis = transform.basis.rotated(Vector3.RIGHT, camera_speed)
	if Input.is_action_pressed("ui_down"):
		transform.basis = transform.basis.rotated(Vector3.RIGHT, -camera_speed)
	if Input.is_action_pressed("ui_left"):
		transform.basis = transform.basis.rotated(Vector3.UP, camera_speed)
	if Input.is_action_pressed("ui_right"):
		transform.basis = transform.basis.rotated(Vector3.UP, -camera_speed)

neither does this:

func _process(delta):
	if Input.is_action_pressed("ui_up"):
		rotate_x(camera_speed)
	if Input.is_action_pressed("ui_down"):
		rotate_x(-camera_speed)
	if Input.is_action_pressed("ui_left"):
		rotate_y(camera_speed)
	if Input.is_action_pressed("ui_right"):
		rotate_y(-camera_speed)

With both this two, the same thing happens. looks like it works, and then the camera starts acting weird.

extends CharacterBody3D

@onready var camera = $Camera3D
var camera_speed = 0.1

func _process(delta):
	if Input.is_action_pressed("ui_left"):
		rotate_y(camera_speed)
	if Input.is_action_pressed("ui_right"):
		rotate_y(-camera_speed)
	if Input.is_action_pressed("ui_up"):
		camera.rotate_x(camera_speed)
	if Input.is_action_pressed("ui_down"):
		camera.rotate_x(-camera_speed)

Try this, it does what I said earlier

OMG yes! it works!

If I’m reading it right, the up/down movement needs to be with the player and the left/right with the camera.

I don’t understand why but anyway thank you very much!!!

Lmao no that’s the opposite left and right is the player

It didn’t work earlier because rotating the camera around an axis modifies the camera’s local basis which rotations are based on

oh yes yes, left/right is the player.
ahhh ok, so cameras should only tilt up/down and the rest with the player.

thank you!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.