Creating a First-Person Controller in Unity
This tutorial will guide you through building a simple first-person controller in Unity. The first-person controller allows players to explore a 3D world with smooth movement and camera rotation. We will use basic C# scripting and Unity’s built-in CharacterController component.
1. Setting Up the Scene
Start by setting up the basic scene environment for your first-person controller:
- In Unity, create a new 3D scene.
- Go to GameObject > 3D Object > Plane to create a ground surface.
- Adjust the scale of the plane to make it large enough for the player to walk around.
- Go to GameObject > 3D Object > Cube to add obstacles or walls to interact with.
2. Adding the Player Object
To represent the player, we will use a capsule object:
- Go to GameObject > 3D Object > Capsule to add a capsule to the scene.
- Position the capsule slightly above the ground (e.g., Position = (0, 1, 0)).
- Rename the capsule to Player.
- Remove the Capsule Collider component since the CharacterController will handle collisions.
- Add a CharacterController component to the Player object by selecting the Player, then clicking Add Component in the Inspector and searching for CharacterController.
3. Writing the First-Person Controller Script
Now, let's create a script to handle the player movement and camera rotation:
- In the Project window, right-click and select Create > C# Script. Name it FirstPersonController.
- Attach the script to the Player object by dragging it from the Project window to the Player in the Hierarchy.
Open the script and replace the contents with the following code:
using UnityEngine;
public class FirstPersonController : MonoBehaviour
{
public float moveSpeed = 5f;
public float mouseSensitivity = 2f;
public float gravity = -9.81f;
private CharacterController controller;
private Vector3 velocity;
private Transform cameraTransform;
void Start()
{
controller = GetComponent();
cameraTransform = Camera.main.transform;
cameraTransform.position = new Vector3(transform.position.x, transform.position.y + 1.5f, transform.position.z);
cameraTransform.parent = transform; // Attach camera to player
}
void Update()
{
// Player movement
float moveX = Input.GetAxis("Horizontal") * moveSpeed;
float moveZ = Input.GetAxis("Vertical") * moveSpeed;
Vector3 move = transform.right * moveX + transform.forward * moveZ;
controller.Move(move * Time.deltaTime);
// Apply gravity
if (controller.isGrounded && velocity.y < 0)
{
velocity.y = -2f;
}
velocity.y += gravity * Time.deltaTime;
controller.Move(velocity * Time.deltaTime);
// Camera rotation
float mouseX = Input.GetAxis("Mouse X") * mouseSensitivity;
float mouseY = Input.GetAxis("Mouse Y") * mouseSensitivity;
transform.Rotate(Vector3.up * mouseX);
cameraTransform.Rotate(Vector3.left * mouseY);
}
}
4. Attaching the Camera
For the first-person view, the camera needs to be attached to the player:
- Select the Main Camera in the Hierarchy.
- Position it slightly above the Player object (e.g., Y = 1.5).
- Drag the Camera onto the Player object in the Hierarchy to make it a child of the Player. This will make it follow the player's movements and rotations.
5. Configuring Inputs
Unity’s Input Manager uses predefined axis names like “Horizontal” and “Vertical” for movement, as well as “Mouse X” and “Mouse Y” for mouse input. These are automatically configured, but you can adjust the sensitivity in the Edit > Project Settings > Input Manager.
6. Testing the Controller
Now that the first-person controller is ready, hit the play button to test it:
- Use W, A, S, D to move the player forward, backward, and sideways.
- Move the mouse to rotate the camera and look around.
- Ensure the gravity works by stepping off any raised objects to see if the player falls naturally.
7. Enhancing the Controller
You can enhance the first-person controller further by adding:
- Sprinting functionality by increasing the moveSpeed when holding the shift key.
- Jumping logic by detecting when the player presses the spacebar and applying an upward velocity.
- Climbing or crouching mechanics for more complex player interactions with the environment.
Conclusion
With this basic first-person controller, you now have a functional player setup for exploring 3D environments. By customizing and extending the script, you can add more features and interactions to suit your game’s needs.