How to implement first-person and third-person viewpoint in Unity

Cameras are strategically placed Depending on the game type (2D or 3D) and game logic. For example, in puzzle games or games with an orthographic perspective view, cameras are usually placed in a fixed position. In this type of game, the camera does not follow the player’s character or respond to the character’s movements. Instead, the camera remains stationary throughout the gameplay and provides a consistent view of the game environment from a predetermined angle. However, as game technology and player expectations have evolved, dynamic and player-controlled camera systems have become more prevalent in modern game development.

First-person and third-person games

The role of the camera in first-person and third-person games is crucial to shaping the player’s perspective, immersion, and overall gaming experience.

In first-person games, the player sees the game world as if they are the character. Examples include “Call of Duty,” “Half-Life,” “Doom,” and “Portal.” It adopts the viewpoint of the in-game character, and players see the game world through the character’s perspective. The camera’s role in first-person games is to create a sense of immersion and presence.

In third-person games, the player views the character and the environment from a distance. This allows players to see the character’s movements and actions, providing better spatial awareness. Examples include the “Legend of Zelda” series, “Assassin’s Creed,” “Tomb Raider,” and “Grand Theft Auto.”

Setting up the project

  1. First, create a new Unity 3D project or open an existing one. The default project includes a “Main Camera” and a “Directional Light” in the “Hierarchy” window.

  2. We’ll use a moving car on a plain surface to depict the concepts.

  3. Add a plane 3D GameObject by right-clicking in the “Hierarchy” window and selecting “plane” GameObject in the “3D object.”

  4. Next, import a 3D carhttps://assetstore.unity.com/packages/3d/vehicles/land/blue-sedan-195200 from the Unity Asset store into the scene and place it over the plane.

  5. Add a road blocker prophttps://assetstore.unity.com/packages/3d/props/exterior/road-props-low-poly-123340 from the asset store to make the scene more interesting.

  6. The scene will now look something like the following.

Camera view
Camera view

Implementation

The camera moves in the first person or third person, corresponding to the player’s movement. The camera position distinguishes the two. Let’s check out how we can implement both.

Camera for the third-person

Implementing a basic third-person camera in Unity 3D involves creating a script that positions the camera behind the player character and follows its movements.

  • Position the camera at a distance from the character in the scene. We can do this by adjusting the camera’s “Transform” component in the inspector. Set the appropriate position (X, Y, Z) and rotation (Rotation X, Y, Z) values.

  • Now, attach the PersonCameraController script to the camera.

  • Drag and drop the target GameObject in the “target” field of the script.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PersonCameraController : MonoBehaviour
{
public Transform target; // The target GameObject that the camera will follow (e.g., the player)
public Vector3 offset = new Vector3(0f, 2f, -5f); // The camera's position offset from the target
void LateUpdate()
{
// Calculate the new camera position based on the target's position and the offset
Vector3 newPosition = target.position + offset;
// Set the camera's position to the new calculated position
transform.position = newPosition;
// Make the camera look at the target
transform.LookAt(target);
}
}

The script above creates a simple third-person camera controller that follows and orbits around the target GameObject. The camera’s position is determined by the offset vector relative to the target’s position, and the camera always looks at the target to maintain a third-person perspective.

  • Line 10: The LateUpdate() method is a built-in Unity method that is called after all the other update methods have been executed. This guarantees that the camera’s position and rotation are updated after any potential player or object movement with confidence.

  • Line 19: The camera’s forward direction (the direction it is looking at) point towards the target. It ensures that the camera always faces the target GameObject.

Play the game

Note: To play the game, click the Run button below and wait a few seconds for the “Output” window to render. You can also play the game in a new tab by clicking the “Your app can be found at:” link below. Click outside the window to resume scrolling.

import React from 'react';
require('./style.css');

import ReactDOM from 'react-dom';
import App from './app.js';

ReactDOM.render(
  <App />, 
  document.getElementById('root')
);

Camera for the first-person

In creating first-person perspective, position the camera at character’s viewpoint. In case of the car scene, the position would be at the left seat in front of the steering wheel. The script will be the same of we used in the third-person PersonCameraController.

Play the game

Note: To play the game, click the Run button below and wait a few seconds for the “Output” window to render. You can also play the game in a new tab by clicking the “Your app can be found at:” link below.

import React from 'react';
require('./style.css');

import ReactDOM from 'react-dom';
import App from './app.js';

ReactDOM.render(
  <App />, 
  document.getElementById('root')
);
Copyright ©2024 Educative, Inc. All rights reserved