SanzioSan
SanzioSan

Reputation: 254

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?

Upvotes: 14

Views: 44579

Answers (8)

idbrii
idbrii

Reputation: 11946

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.

Upvotes: 2

Barrrettt
Barrrettt

Reputation: 819

An example in c#

Vector3 forward = GlobalTransform.basis.z;

forward example

Upvotes: 3

Tanmorik
Tanmorik

Reputation: 21

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

Upvotes: 2

Rakka Rage
Rakka Rage

Reputation: 19599

does it have anything to do with Vector3.FORWARD?

enter image description here

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

Upvotes: 0

airvine
airvine

Reputation: 731

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.

Upvotes: 3

Daniklad
Daniklad

Reputation: 1079

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.

Upvotes: 15

SanzioSan
SanzioSan

Reputation: 254

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

Upvotes: 1

Related Questions