0 votes

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?

in Engine by (249 points)

2 Answers

–2 votes

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.

by (27,240 points)
+1 vote

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
by (16 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.

Categories