Move the 3D player relative to camera in Godot 4?

Hello! This is my first day on this site. I am trying to make 3D platformer game with Godot 4, I just need to make the player move relative to the camera when I rotate camera horizontally and vertically. I’m a very begginer at coding, so I did this:

velocity = cameraBase.basis * movement
movement.x = inputDir.x
movement.z = inputDir.y

It seems to have worked when moving the player with the camera rotated horizontally. However, when I rotate the camera vertically, the player’s movement pushes with the camera rotating vertically. It’s just wrong.

There is a helpful guide here that implements a simple method for moving relative to the camera.

Somewhere in your code could be something like this:

var relativeDir = Vector3(inputDir.x, 0.0, inputDir.y).rotated(Vector3.UP, cameraBase.rotation.y)

The rotated(axis: Vector3, angle: float) function returns a vector that’s been rotated on the given axis by the amount of angle in radians.

This means your input vector, after being converted to a vector3, is now rotating along with your camera, but only on the y axis. Vertically rotating the camera will no longer affect your movement. Hope this helps!

1 Like

This seems to work with this way. You have my thanks!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.