Godot 3d get Forward Vector

I was wondering if there was a way to get the forward vector of a spatial node in godot 3d.

In unity this would simply be transform.forward.

Godot gives me a rotational vector but im not sure how to convert this to a directional vector.

What is godot's version of transform.forward?

8 Answers

Forward is what you decide it should be. I usually do this:

var aim = $Player.get_global_transform().basis var forward = -aim.z var backward = aim.z var left = -aim.x var right = aim.x 

Of course it helps if you have a convention for this when designing your assets and scenes. I always use -Z as forward but you can choose differently if you so wish.

An example in c#

Vector3 forward = GlobalTransform.basis.z; 

forward example

0

i solved my problem using get_global_transform().basis.z

3

SanzioSan was correct in using the basis.z vector, but he did not explain it well enough. In essence, it is a pretty simple problem.

If you are trying to drag an object in 3d or have an object and want it follow your screen, you can use code similar to this:

var transform_hold_obj = node_to_hold_obj.get_global_transform() transform_hold_obj.origin = transform_hold_obj.origin - transform_hold_obj.basis.z * some_distance_between_you_and_object obj_being_dragged.set_transform(transform_hold_obj) 

The transform_hold_obj in my case is from my camera node. I want some object I select to follow my camera. This code is inside some script connected to the object being dragged. The basis.z vector is my forward vector or the direction my camera is pointing.

I know it is kinda late but i came with the solution of building my own forward and left Vector. It is in c# but should easily converted into GDScript. Hope that it helps someone.

Vector3 forward = new Vector3(-(float)Math.Sin(Rotation.y), 0, -(float)Math.Cos(Rotation.y)); Vector3 left = new Vector3((float)Math.Sin(Rotation.y - Math.PI/2), 0, (float)Math.Cos(Rotation.y - Math.PI/2)); 

From a godot contributor:

The purpose of Vector3.FORWARD is such that moving by this vector will move a character forward. For games where the camera angle does not change you always want Vector3(0, 0, -1) as the forward vector. For first person games, you need this in local space, so it's better to use -transform.basis.z, but you can also use basis.xform(Vector3.FORWARD).

So you can use use global_transform.basis.xform(Vector3.FORWARD) to get the forward vector using something named "forward". It should also work for the UP and RIGHT constants.

does it have anything to do with Vector3.FORWARD?

enter image description here

 transform.position += Vector3.forward * Time.deltaTime; 

use transform.basis.z because of: https://docs.godotengine.org/en/stable/tutorials/3d/using_transforms.html#obtaining-information

ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoa3FsY21%2FeH%2BOoKadp6RigKV5xp6rZp6fp8SivsNmrZ6bpKS%2F