I can't use Quat to rotate a vector3?

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

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)
:bust_in_silhouette: Reply From: xdanic

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

:bust_in_silhouette: Reply From: Legorel

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

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


xdanic | 2021-05-02 01:43