+3 votes

Hey, I'm still learning 3D...

So I rotate the Player object (which is a 3D KinematicBody) and then I want to move it forward to the direction it was rotated. So I tried

move_and_slide(Vector3(0, 0, 1))

but that doesn't work because it needs global coordinates instead of local coordinates, I guess. I read the tutorial on this several times and looked up some example codes but I don't really get the idea of it. How do I move my object forward to the direction it is facing? Is there an easy and understandable explanation for this?

Thank you!

in Engine by (50 points)

3 Answers

+2 votes

You can use the 3rd vector of the global transform of the object.

var facing = global_transform.basis.z
move_and_slide(facing)
by (3,871 points)
edited by
0 votes

You can use: transform.basis.xform(motion_direction)*motion_speed
That will transform the motion vector to the local transform (remember that forward is -z on your meshes), is what you put on the move_and_slide parameter.

The platformer and kinematic demos have more examples of motions related to the camera.

by (7,860 points)
+2 votes

I know this is kinda late, but since i was strugeling with the same problem and found no real answers online i thought i comment here:

The problem is that move _ and _ slide moves the kinematic body in the global transform, while the rotation is stored in its local transform.

Lets say your direction Vector is (through key presses for example):

var global_direction = Vector3(0,0,1)

and you donĀ“t want to move in the global direction, but in the local direction the kinematic body is facing. First, you rotate the vector on the axis you want (in my example i rotate around the y-axis ( its the Vector3(0,1,0) )):

var local_direction = global_direction.rotated(Vector3(0,1,0), rotation.y)

rotation is a Vector3 that stores the current rotation of the kinematic body in radians and since i want to rotate around the y-axis i use its y value

Finally you can call move _ and _ slide(), in my example however i want to apply speed to the direction first:

var velocity = local_direction * SPEED
move_and_slide(velocity)

I hope this helps people, that want to achieve some first or third person movement and use a kinematic body as a player and move with the move _ and _ slide() method.

by (18 points)

Oh boy. Thank you so much for this... I was tearing my hair out! I think I'll make a PR to update the docs to mention that moveandslide is not expecting local coordinates like Translate does...

Finally, after spending hours scratching my head and looking for help everywhere without getting any. Thenks.
I must say, I laughed at myself for not thinking about something like that.

I love you... A lot.

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.