Joystick controlling camera (3D)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By James122333

Hi
How do you code so that a joystick rotates a 3D camera?

:bust_in_silhouette: Reply From: Wakatta

To rotate around which axis?
To rotate around itself or another node?
To rotate from itself or from another node?
To rotate 360º or FPS style?

Since OP did not specify answers will be limited

You can use “ui_left”, “ui_right” or
Setup Input Map to use C stick

Rotate around own axis

extends Camera

const SPEED = 0.5

func _process(delta):
	angle+= SPEED * delta * delta 
	angle = wrapf(angle, 0, 360)
	
	if Input.is_action_pressed("ui_look_right"):
		rotate_y(SPEED * delta)
	elif Input.is_action_pressed("ui_look_left"):
		#rotate_y(-SPEED * delta)
	elif Input.is_action_pressed("ui_look_up"):
		rotate_x(SPEED * delta)
	elif Input.is_action_pressed("ui_look_down"):
		rotate_x(-SPEED * delta)

Rotate around reference Node

extends Camera

const SPEED = 0.5
var angle = 0
var rot = Vector3()
onready var referencePoint = get_parent().global_transform.origin

func _process(delta):
	angle+= SPEED * delta * delta 
	angle = wrapf(angle, 0, 360)
	
	if Input.is_action_pressed("ui_look_right"):
		rot = (global_transform.origin - referencePoint).rotated(Vector3.UP, angle)
	elif Input.is_action_pressed("ui_look_left"):
		rot = (global_transform.origin - referencePoint).rotated(Vector3.UP, -angle)
	else:
		rot = Vector3()
	if rot:
		global_transform.origin = referencePoint + rot
		look_at(referencePoint, Vector3.UP)