Creating a First-Person Camera Controller in Unity
Creating a First-Person Camera Controller in Unity
In many first-person games, precise control over the camera is essential for providing an immersive experience. In Unity, you can achieve this by implementing a first-person camera controller. In this blog post, we'll explore the SCfpscam
script, which enables you to control the camera view in a first-person perspective. Let's break down how this script works.
First-Person Camera Controller Script Overview
The SCfpscam
script is designed to be attached to the main camera in Unity. It provides functionality for looking around in a first-person view by capturing mouse input. Key components of this script include camera sensitivity, camera rotation, and locking the mouse cursor.
Required Imports
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
These using statements allow us to access necessary Unity functionalities.
Variables
public float _senstivity = 100;
Transform _cam, _playerBody;
float xRotation = 0f;
_senstivity
: A public variable to control the camera's sensitivity or responsiveness to mouse input._cam
: A reference to the camera's transform._playerBody
: A reference to the parent transform that represents the player's body or character.xRotation
: A variable to keep track of the camera's vertical rotation angle.
Initialization (Start Method)
void Start()
{
// Gets Camera's transform
_cam = this.transform;
// Check if the camera is null or not
if (_cam == null)
{
Debug.LogError("Camera is null");
}
// Gets the player's body's transform (typically a child of the camera)
_playerBody = this.transform.parent.transform;
// Check if the player's body transform is null
if (_playerBody == null)
{
Debug.LogError("Player is null");
}
// Locks the mouse cursor to the center of the screen
Cursor.lockState = CursorLockMode.Locked;
}
In this section, we initialize our variables, such as _cam
(camera transform) and _playerBody
(player's body transform). It's important to note that the camera's parent transform is often used to represent the player's body or character. We also lock the mouse cursor to ensure it stays within the game window for a better first-person experience.
Camera Control (FixedUpdate Method)
void FixedUpdate()
{
// Capture mouse input for horizontal and vertical rotation
float MouseX = Input.GetAxis("Mouse X") * _senstivity * Time.deltaTime;
float MouseY = Input.GetAxis("Mouse Y") * _senstivity * Time.deltaTime;
// Rotate the player's body horizontally
_playerBody.Rotate(Vector3.up * MouseX);
// Update the vertical camera rotation and clamp it within a range
xRotation -= MouseY;
xRotation = Mathf.Clamp(xRotation, -90f, 90f);
// Apply the vertical rotation to the camera's local rotation
_cam.localRotation = Quaternion.Euler(xRotation, 0f, 0f);
}
This part of the script captures mouse input for horizontal and vertical rotation. Horizontal rotation is applied to the player's body to simulate turning. Vertical rotation is handled by changing the camera's local rotation, allowing the player to look up and down. The Mathf.Clamp
function ensures that the vertical rotation stays within a specified range (-90 to 90 degrees) to prevent over-rotation.
With this script, you can create a responsive first-person camera controller for your Unity game, providing players with a natural and immersive view of their surroundings.
Important
- Create Player as an empty object and add Character controller on to it.
- Place Main Camera As Child Of Player Game Object
- Adjust the placement of cam in the place of head (DON'T PUT CAM ABOVE PLAYER ABOVE PLAYER OBJECT AS IT CAN LEAD TO CAM CLIPPING THROUGH THE WALLS)
Comments
Post a Comment