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.
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.”
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.
We’ll use a moving car on a plain surface to depict the concepts.
Add a plane 3D GameObject by right-clicking in the “Hierarchy” window and selecting “plane” GameObject in the “3D object.”
Next, import a
Add a
The scene will now look something like the following.
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.
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 targetvoid LateUpdate(){// Calculate the new camera position based on the target's position and the offsetVector3 newPosition = target.position + offset;// Set the camera's position to the new calculated positiontransform.position = newPosition;// Make the camera look at the targettransform.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.
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') );
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
.
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') );