Easy math: position in front of 3d character

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

Yesterday I was trying to figure out how to calculate a position in fron of my character. I knew how to do it when the node rotation was expressed as Quaternion (in Unity). How can I calculate a position in front (or behind, or to a side, whatever) of a 3d node?

:bust_in_silhouette: Reply From: Zylann

Godot uses the OpenGL convention for its axes. So the forward vector is negative Z. (if you are not sure, you can see the axes in the editor, in local mode)
You can obtain this vector in this way:

var forward = -spatial_node.global_transform.basis.z

So a position in front of the character would be:

var pos = spatial_node.global_transform.origin + forward * distance

Note: spatial_node would be the 3D node you want to get the position in front. If that code is already in a script attached to that node, you don’t need to use such variable.

1 Like