Godot Version
4.6.1.stable
Question
Hey guys! I’m making a game where the character can fly. When the character goes on flying mode, I want it’s model’s forward vector to be the same as camera’s forward vector (Visualise how Superman flies). When I try to do it, I get weird camera angles that I don’t like. How could I fix it so that the character rotates properly while preventing the rolling rotation?
Code:
func rotate_camera(event: InputEvent) -> void:
#non-flying rotation
if not is_flying or is_on_floor():
if transform.basis.y != Vector3.UP:
reset_rotation()
transform.basis *= Basis(Vector3.UP, deg_to_rad(-event.relative.x * SENSITIVITY))
head.transform.basis *= Basis(Vector3.RIGHT, deg_to_rad(-event.relative.y * SENSITIVITY))
head.rotation.x = clamp(head.rotation.x, deg_to_rad(-90), deg_to_rad(90))
#flying rotation
else:
transform.basis *= Basis(transform.basis.x, deg_to_rad(-event.relative.y * SENSITIVITY))
head.transform.basis *= Basis(head.transform.basis.y, deg_to_rad(-event.relative.x * SENSITIVITY))
I found a way to get around the math required to calculate and change the rotation. I have the Camera3D in a 3D node called “Head”. I put the player mesh inside the Head node and then did this:
func rotate_camera(event: InputEvent) -> void:
# When player is not flying
if not is_flying or is_on_floor():
if transform.basis.y != Vector3.UP:
reset_rotation()
transform.basis *= Basis(Vector3.UP, deg_to_rad(-event.relative.x * SENSITIVITY))
camera_3d.transform.basis *= Basis(Vector3.RIGHT, deg_to_rad(-event.relative.y * SENSITIVITY))
camera_3d.rotation.x = clamp(camera_3d.rotation.x, deg_to_rad(-90), deg_to_rad(90))
# When player is flying
else:
transform.basis *= Basis(Vector3.UP, deg_to_rad(-event.relative.x * SENSITIVITY))
head.transform.basis *= Basis(Vector3.RIGHT, deg_to_rad(-event.relative.y * SENSITIVITY))
The Head node is positioned in a way that it acts as a pivot point for the model’s rotation. When the player is walking, you only rotate the camera. But when you want the model to rotate along with it, you rotate the Head node. This prevents any weird rotations. It will be good enough for now, but I might need to change things if I want to add more to the flying system.
Head node’s placement: