Godot 3d get Forward Vector
Asked Answered
F

8

13

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?

Foggia answered 25/1, 2018 at 8:10 Comment(0)
D
13

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.

Drivein answered 9/10, 2019 at 20:23 Comment(0)
C
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.

Calorific answered 19/4, 2020 at 20:50 Comment(0)
R
3

An example in c#

Vector3 forward = GlobalTransform.basis.z;

forward example

Riccio answered 21/12, 2022 at 21:29 Comment(0)
T
2

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));
Tribromoethanol answered 20/3, 2021 at 18:51 Comment(0)
S
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.

Stoller answered 27/12, 2022 at 21:40 Comment(0)
F
1

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

Foggia answered 25/1, 2018 at 14:51 Comment(3)
Please explain a bit more on how this helpedAndrosphinx
my goal was to make a game object move the way it is facing. I needed the direction vector for thisFoggia
It's good to hear that you found the cause of your problem. You could make your answer more helpful for future readers by explaining how you diagnosed it, and in particular, which evidence will identify this problem when it's seen elsewhere.Kelwunn
V
0

does it have anything to do with Vector3.FORWARD?

enter image description here

    transform.position += Vector3.forward * Time.deltaTime;
Ventricular answered 6/5, 2020 at 2:35 Comment(0)
B
-1

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

Branle answered 23/4, 2021 at 6:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.