To enable rotation for both first-person and third-person cameras in Godot 4, you need to handle the mouse input and appropriate camera rotation logic in your player script. Here’s a way to implement camera rotation for both camera modes:
Step 1: Enhancing the Player Script for Rotation
Modify your player script to handle mouse movement, which will allow you to look around when in first-person and rotate the third-person camera around the player.
Here’s an updated version of the previous script that implements rotation for both cameras:
extends Node3D
@onready var first_person_camera: Camera3D = $FirstPersonCamera
@onready var third_person_camera: Camera3D = $ThirdPersonCamera
@onready var player_body: Node3D = $PlayerBody # Assuming you have a node to represent the player's body (for rotation)
var is_first_person: bool = true
var mouse_sensitivity: float = 0.1 # Adjust for sensitivity
# You might want to constrain the Y rotation in first-person mode to avoid flipping
var y_rotation_limit: float = 89.0
var current_camera_rotation: Vector2 = Vector2.ZERO
func _ready():
# Ensure only the first-person camera is active at the start
set_camera_mode(is_first_person)
Input.set_mouse_mode(Input.MOUSE_MODE_CAPTURED) # Capture the mouse to prevent it from leaving the screen
func _process(delta):
handle_camera_switch()
if is_first_person:
handle_first_person_rotation(delta)
else:
handle_third_person_rotation(delta)
func handle_camera_switch():
# Switch camera with a key press (e.g., "ui_accept" which is the Enter key by default)
if Input.is_action_just_pressed("ui_accept"):
is_first_person = !is_first_person
set_camera_mode(is_first_person)
func set_camera_mode(first_person: bool):
first_person_camera.current = first_person
third_person_camera.current = not first_person
if not first_person:
# Ensure the third-person camera is positioned behind the player
update_third_person_camera_position()
func handle_first_person_rotation(delta):
var mouse_movement = Input.get_last_mouse_velocity()
current_camera_rotation.x -= mouse_movement.y * mouse_sensitivity
current_camera_rotation.y -= mouse_movement.x * mouse_sensitivity
current_camera_rotation.x = clamp(current_camera_rotation.x, -y_rotation_limit, y_rotation_limit)
first_person_camera.rotation_degrees.x = current_camera_rotation.x
player_body.rotation_degrees.y = current_camera_rotation.y # Rotate the player body for yaw
func handle_third_person_rotation(delta):
# Rotate the third-person camera around the player based on mouse movement
var mouse_movement = Input.get_last_mouse_velocity()
player_body.rotation_degrees.y -= mouse_movement.x * mouse_sensitivity
third_person_camera.rotation_degrees.x = clamp(third_person_camera.rotation_degrees.x - mouse_movement.y * mouse_sensitivity, -80, 80)
third_person_camera.position = player_body.position + Vector3(0, 1.7, -5).rotated(Vector3.UP, player_body.rotation_degrees.y)
func update_third_person_camera_position():
# Reposition the third-person camera whenever the mode is switched
third_person_camera.position = player_body.position + Vector3(0, 1.7, -5).rotated(Vector3.UP, player_body.rotation_degrees.y)
Explanation of the Script
-
Mouse Input Handling:
- The script captures mouse movement using
Input.get_last_mouse_velocity(). This gives the delta movement of the mouse, which is multiplied by a sensitivity factor to control how fast the camera rotates.
-
First-Person Rotation:
- For the first-person camera, the vertical rotation (pitch) is clamped to prevent flipping over. The horizontal rotation (yaw) is applied to the player's body to rotate left and right.
-
Third-Person Rotation:
- For the third-person camera, only the yaw is adjusted based on mouse movement, while a separate approach is taken to modify the pitch.
-
Repositioning the Cameras:
- The
update_third_person_camera_position() function ensures the third-person camera follows behind the player correctly upon switching.
Step 2: Add the Player Body Node
For the rotation to work correctly, ensure you have a dedicated Node3D node as the player body (named PlayerBody in the script). This body node should encompass the visual representation of the player (like a MeshInstance) and should be the one that rotates for bipedal characters in third-person mode.
Step 3: Test Your Game
Now, when you run your game:
- In first-person mode, use the mouse to look around.
- In third-person mode, use the mouse to rotate around the player as well.
Additional Notes
- Mouse Capture: The example captures the mouse for input, which is common in first-person shooters. For testing, you might want to set the mouse mode to
Input.MOUSE_MODE_VISIBLE when you want to exit the rotation easily.
- Field of View (FOV): You may want to tweak the FOV for both cameras for better visual results, particularly in the first-person perspective.
- Fine-Tuning Sensitivity: Adjust
mouse_sensitivity to suit your game's controls.
With these adjustments, you should have smooth rotation for both camera modes!