0 votes

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

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

Godot version 3.2.3
in Engine by (48 points)

1 Answer

+1 vote
Best answer

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()
by (4,982 points)
selected by
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.