0 votes

Hi, I was trying to convert some code I did in unity into GDScript and it seemed quite straightforward, until I tried to do this, and apparently, Quat doesn't return a Vector 3, but I don't know how to convert it into one...

-This is what the C# code in unity looks like for reference-

public class ThirdPersonCamera : MonoBehaviour
    public Vector3 offset;
    private Transform target;
    [Range (0, 1)] public float lerpValue;
    public float sensibilidad;

void Start()
    target = GameObject.Find("Player").transform;

void LateUpdate()
    transform.position = Vector3.Lerp(transform.position, target.position + offset, lerpValue);
    offset = Quaternion.AngleAxis(Input.GetAxis("Mouse Y") * sensibilidad, -Vector3.right) * offset;
    offset = Quaternion.AngleAxis(Input.GetAxis("Mouse X") * sensibilidad, Vector3.up) * offset;


And this is what I'm trying to do on godot

extends Camera

var target
export var offset = Vector3(0, 3, -8)
export var lerp_val = 0.2
export var sens = 5
# Called when the node enters the scene tree for the first time.
func _ready():
    target = get_node("../Player")

# Called every frame. 'delta' is the elapsed time since the previous frame.

func _process(delta):
    global_transform.origin = Vector3(lerp(global_transform.origin, target.global_transform.origin + offset, lerp_val))
    offset = Quat.IDENTITY.set_axis_angle(Vector3.UP, Input.get_action_strength("mousey"))
    offset = Quat.IDENTITY.set_axis_angle(-Vector3.RIGHT, Input.get_action_strength("mousex"))
    look_at(target.transform.origin, Vector3.UP)
Godot version 3.3
in Engine by (19 points)

2 Answers

+1 vote
Best answer

I've never used quaternions but i found something that might work :

var quat = Quat.IDENTITY.set_axis_angle(Vector3.UP, Input.get_action_strength("mousey"))
offset = quat.get_euler()
quat =  Quat.IDENTITY.set_axis_angle(Vector3.UP, Input.get_action_strength("mousey"))
offset = quat.get_euler()

from the documentation :

Vector3 get_euler()

Returns Euler angles (in the YXZ convention: when decomposing, first Z, then X, and Y last) corresponding to the rotation represented by the unit quaternion. Returned vector contains the rotation angles in the format (X angle, Y angle, Z angle).

by (250 points)
selected by

I forgot in my unity code I'm multipling by offset, your code still throws me an error:

Invalid call. Nonexistent function 'get_euler' in base 'Nil', I also realized be both missed in unity i was multipling by offset (that would go at the end of the first line in your example)

While I found a better method, having a 1:1 code would probably be useful for some people, I think this should work, althought it probably is more complicated than it should? I gotta figure how to translate it to my code


0 votes

While my question is very specific, the thing 3rd person controller I was trying to implement is possible with this tutorial


by (19 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.