0 votes

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

in Engine by (59 points)

1 Answer

0 votes

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 "uileft", "uiright" 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)
by (482 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories