How to multiply a basis by a vector3?

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

Question:
This is possible in gdscript (godot 4.0beta3):

var exampleBasis = get_viewport().get_camera_3d().transform.basis
var expectedResult = (cameraBasis * Vector3(0.4, -0.3, -0.9))

But this won’t compile in C# (godot 3.5 mono):

var exampleBasis = GetViewport().GetCamera().Transform.basis;
var expectedResult = (exampleBasis * new Vector3(0.4f, -0.3f, -0.9f));

To better understand C#, could someone explain me why it is not possible?
Also, what could be a workaround to get a vector3 as an “expectedResult” from multiplying a basis and a vector3 in C#?

Intention:
The meaning of this code would be to get the basis of the camera to establish the forward direction of a moving character. An other vector2 variable would be getting input vectors and the x and y value resulting would then be used in the respective x and z fields of the “new Vector3” that would have been multiplied by the basis. Thus, returning a Vector3 that could be used to calculate the forward, left and right direction of a character relative to the camera.

Thank you for reading.

:bust_in_silhouette: Reply From: juppi

Hey Dostoi!

You have to use the Xform method on the Basis:

var cameraBasis = GetViewport().GetCamera().Transform.basis;
var expectedResult = cameraBasis.Xform(new Vector3(0.4f, -0.3f, -0.9f));

Oh! that’s it! Thank you very much, Juppl.

So, for future reference: Multiplying a basis directly by a Vector3 is indeed impossible in C#. According to the manual, the Xform method returns “a vector transformed (multiplied) by the basis matrix”, Thus, allowing a converted basis (to a Vector3) to be multiplied by an other Vector3.

Dostoi | 2022-12-31 11:52