How to find out if an object points down

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

Hey there!
I would like to find out if an object in my scene points down. In my case this object a gun, that needs to be pointed at the floor to reload (it’s a VR game).

I tried the following piece of code:

var angle = get_global_transform().basis.get_euler().angle_to(Vector3.DOWN)
if(angle < 20):
	reload()

My idea was t get the object’s global orientation vector and compare it to the world’s global DOWN vector. If the angle between both vectors iy smaller than 20 degrees, the gun must be pointing at the floor.

But actually this code does not what I thought it would do. It just returns a constant value, now matter how I rotate my object. Also I might have mixed up degrees and radians, bt that is a different problem :wink:

Can anyone give me ahint on how to get the global orientation vector of a node?

:bust_in_silhouette: Reply From: Wakatta

You’re absolutely correct about the radians
Honestly this is a problem probably better solved with a Raycast or dot_product but because we like self torture allow me to make some assumptions

You are using Godot’s default -Z as your default facing direction so that would make its angle to Vector3.DOWN 90deg

var facing = -get_global_transform().basis.z
var angle = facing.angle_to(Vector3.DOWN)
if(rad2deg(angle) < 20):
    reload()

You are using +Z as your facing direction

var angle = get_global_transform().basis.z.angle_to(Vector3.DOWN)
if(rad2deg(angle) < 20):
    reload()