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.
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)
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)