How to get the angle between two vectors.

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By vonflyhighace2
:warning: Old Version Published before Godot 3 was released.

I’m trying to calculate the angle between my characters Z axis and the cameras Z axis. How would I go about doing this, and does Godot have any build in methods to make it any easier?

EDIT: For Vector2 is angle_to, but z axis?

If is for Vector3, just checked and Vector3.angle_to is on Godot 2.1.1

It appears on internal editor’s docs (with no details) but are not on the website yet.

eons | 2016-12-01 22:47

Reminder: in Godot, the Camera’s Z axis is backwards due to OpenGL convention.
I would go for eons solution once you have the two vectors.
And indeed… the function exists
But it’s not in the docs :expressionless:

Zylann | 2016-12-02 16:41

On Godot 4 the object Vector2 provide the method angle_to_point(Vector2). Objects based in Node2D provide transform properties like position property, for situate the Node2D, wich is a Vector2 and you can get angle respect to another position (vector2). The result angle (in radians) is necesary to normalize so that fit with the expected, for example adding 180 grades:

self.rotation = self.position.angle_to_point(forwardPosition);
self.rotation_degrees = self.rotation_degrees + 180; #optional, for fit

RufusWein | 2023-04-06 09:11