transform character to camera space

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By vonflyhighace2
:warning: Old Version Published before Godot 3 was released.

I’m trying to get my character to move in the direction the camera is facing. In unity you would use something like TransformDirection. Any equivalent way to do the same in GD Script?

:bust_in_silhouette: Reply From: Zylann

You can get the world-space forward direction of a Camera node by getting its Z axis:

# Don't forget the minus sign. It's because Godot follows OpenGL's convention
var forward_vector = -camera_node.get_global_transform().basis.z

Then you can use this vector to move your character’s root node.

Note: you can also get the X axis if you want to strafe.

:bust_in_silhouette: Reply From: LettucePie

Post is Super Old, however I was facing similar issue and had this tab opened mocking me with it’s inefficiency. I have since solved my issue and figured I should share. You were most likely looking for: transform.xform(variant). Here is the link to the documentation https://docs.godotengine.org/en/stable/classes/class_transform.html#class-transform-method-xform

Here is how I utilized it:

Scene Tree

Spatial (Spatial.gd)
/Player
//CamPivot
///Camera

Script (Spatial.gd)

func _input(event):
if event is InputEventMouseButton:
	if event.button_index == BUTTON_LEFT:
		pressed = event.pressed
if event is InputEventMouseMotion and pressed:
	var pos = $Player.transform.origin
	var inputDirection = Vector3(
		event.relative.x,
		0,
		event.relative.y)
	inputDirection = $Player/CamPivot.transform.xform(inputDirection)
	pos.x += (inputDirection.x * 0.05)
	pos.z += (inputDirection.z * 0.05)
	$Player.transform.origin = pos