Get the direction of a Vector2

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

How can I get the direction in which a Vector2 goes if the x axis is 0° ?

For example if I have a Vector2(1,1) I want to get 45°.

Or if I have a Vector2(1,0) I want to get 0°.

Or if I have a Vector2(0,1) I want to get 90°.

I hope this question makes sense.

:bust_in_silhouette: Reply From: deaton64

Hi, not sure if this is what you mean:

	var direction = Vector2(1, 1)

func get_angle(dir: Vector2) -> int:
	match dir:
		Vector2(1, 0):
			return 0
		Vector2(0 ,1):
			return 90
		Vector2(1 ,1):
			return 45
	return -1 # Vector2(0, 0)
:bust_in_silhouette: Reply From: rakkarage

enter image description here

:bust_in_silhouette: Reply From: jgodfrey

You want the angle() method of the Vector2 class. For example:

func _ready():
	var v = Vector2(1, 1)
	print(v.angle())          # 0.7854
	print(rad2deg(v.angle())) # 45

That’ll return exactly what you requested, except that the value will be in radians. You can convert that to degrees via the rad2deg method. Both are shown above…