I’ve tried multiple times to make a camera with relative movement, but I can’t make anything work, I just wanted to know if anyone has any suggestions/ tutorials, I’m willing to change versions if it helps
If you share code, please wrap it inside three backticks or replace the code in the next block:
to the camera, I want to turn a 3rd person camera and still move forward so if I turn the camera completely around, I want the player to turn around and move in the new forward direction
You want player movement to be relative to camera’s viewing direction? Project the input directions to x and z axes and then project the result onto horizontal plane.
So, to allow the player to face the model toward the camera direction this snippets could help:
# Get the input direction and handle the movement/deceleration.
var direction = Vector3(input_dir.x, 0, input_dir.y).rotated(Vector3.UP, _camera_mount.rotation.y)
If I read properly without executing the code I will say you are moving based on the original direction to front and sides. Not aligned with the camera. Only based on the original setup of the CharacterBody3D of the Godot template code.
Here’s some boilerplate code that can handle moving a CharacterBody3D with respect to the direction that the camera is facing.
func handle_move() -> void:
var move: Vector2 = Input.get_vector("move_left", "move_right", "move_forward", "move_back")
var direction: Vector3 = Vector3.ZERO
if move != Vector2.ZERO:
direction = Camera.basis * Vector3(move.x, 0, move.y)
direction = Vector3(direction.x, 0, direction.z).normalized();
velocity.x = direction.x * speed
velocity.z = direction.z * speed
move_and_slide()
Here’s what’s happening, at a high level. We first have an input source, gathered from our actions moving in the four directions forward, back, left, and right. Together, that’s a 2D vector that is “flat” along the X-Z plane. That vector defines the direction that we want to move in.
Next we need to apply the camera’s basis to the direction that we have, so we multiply the basis by the desired direction. This gives us the vector relative to the Camera’s rotation, which is what we want here. Next, we zero out the y value (because we don’t want to move up or down) and normalize the vector.
Then we can adjust the velocity of the body with respect to the desired speed, and then call move_and_slide()
I added this under the movement code and it has an issue, It says “Invalid operands “Vector2” and “Vector3” for “!=” operator.” what does that mean? don’t boilerplate code repeats existing code?
I accidentally compared a Vector2 with a Vector3, which is invalid. I have edited my original reply to fix this. “Boilerplate code” is referring to code that intends to lay the foundations for future application.
This was the invalid comparison: move != Vector3.ZERO
move in this case is a Vector2 (contains two numbers) while Vector3.ZERO is referring to a Vector3 (contains three numbers). The not equals operator != compares the values of two things, but those things must be the same type of thing.
the player character is now floating and irresponsive to my inputs even after I rewrote the inputs in the input map to correspond with the rewritten script inputs, did the vector 3 in the boilerplate mess with the _physics_prosess function?
It might be worth going back to the basics and understanding how things work in Godot. What made it fall before, and what changed to make it now floating? Tutorials and forum posts often tell you what to do, but understanding why you do it that way is important.
I’d checking out the example project below. It has everything you need to know about setting up basic movement, and then come back to visit that code example and see how you can modify your setup with the camera in mind.