A fixed camera in Unity refers to a camera that remains anchored at a specific position in the game world. While it stays fixed at its location, it can still pan to follow a target GameObject or a specified direction. This allows game developers to maintain control over the camera’s position, while enabling smooth rotations to track characters, objects, or points of interest, creating an engaging and dynamic visual experience.
Note: You will be able to play the game at the end of this Answer.
In this Answer, we will create a scene that features a moving car on a flat surface.
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.
Add a plane 3D GameObject by right-clicking in the “Hierarchy” window and selecting the “plane” GameObject in the “3D object.”
For our scene, we’ll need a 3D car model and a road blocker prop. You can find these assets in the Asset Store. Once you’ve acquired these assets, you’re ready to place them in your scene.
Import a 3D car from the Unity Asset Store into the scene and place it over the plane.
To make the scene more interesting, add a road blocker prop from the Asset Store.
The scene will now look something like the following.
As you can see, the car and road blocker have been positioned on a simple plane surface. This sets the stage for us to create a fixed camera that can pan and follow the car’s movements. Now, let’s explore how we can achieve this in Unity.
In certain gaming contexts like puzzle games, a stationary camera proves effective. It maintains a fixed viewpoint and doesn’t automatically follow players or elements in the scene. To establish a stationary camera, take the following steps:
Camera positioning: In Unity’s Editor, identify the “Hierarchy” window and choose the camera you intend to anchor. In the “Inspector” window, locate the camera’s attributes. The “Transform” component contains position (X, Y, Z) and rotation (Rotation X, Y, Z) settings. Fine-tune these parameters to precisely dictate where the camera resides and its orientation within the scene.
Projection choice: Within the same camera properties, you’ll encounter the “Camera” component configurations. At this juncture, you’re presented with two projection types: Orthographic and Perspective.
Orthographic projection: This type of projection delivers a consistent, flat view, rendering objects uniformly, regardless of their distance from the camera. To create a classic video game feel, choose “Orthographic” projection. This will make the camera seem fixed, similar to older games.
Perspective projection: This provides a more realistic view, resembling how humans perceive depth in real life. As objects move away from the camera, they appear smaller. If you want a more engaging and immersive experience, select “Perspective” projection.
By understanding these options and adjusting the camera settings accordingly, you can customize the stationary camera setup to match your game’s desired style and objectives. In the scene shown below, the camera is fixed in place with the “Perspective” projection property enabled, without any panning motion alongside the car.
With the script preset below, the camera will remain anchored at its original position and smoothly pan to follow the target GameObject’s movements without translating (moving) along with it. This implementation aligns with the typical definition of a fixed camera in game development.
Position the camera GameObject in the scene where you want it to be fixed and attach the following FixedCameraController
script as a component to it.
using System.Collections;using System.Collections.Generic;using UnityEngine;public class FixedCameraController : MonoBehaviour{public Transform target; // The target GameObject that the camera should follow (e.g., the player).public float rotationSpeed = 3f; // The speed at which the camera rotates.void LateUpdate(){// Calculate the target's direction relative to the camera's position.Vector3 directionToTarget = target.position - transform.position;directionToTarget.y = 0f; // Ensure the camera does not tilt up or down.// Calculate the desired rotation based on the target's direction.Quaternion desiredRotation = Quaternion.LookRotation(directionToTarget);// Smoothly rotate the camera towards the desired rotation.transform.rotation = Quaternion.Slerp(transform.rotation, desiredRotation, rotationSpeed * Time.deltaTime);}}
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.
Lines 13–14: Calculating the direction from the camera to the target involves subtracting the camera’s position from the target’s position, creating a relative direction vector. We ensure a stable camera orientation by keeping the camera from tilting up.
Note: A Quaternion is a data structure that is used to represent rotations in three-dimensional space. It is a compact way to store rotational information and is often preferred over Euler angles (rotation around individual axes) due to its stability and avoidance of
. This information is essential for the proper execution of the program. gimbal lock issues gimbal lock is a challenge to smooth camera movement. It emerges when a three-axis rotation system—like Euler angles—loses a degree of freedom due to the alignment of two rotational axes. This can lead to unexpected rotations and undesired behavior.
Line 17: We calculate the desired rotation of the camera based on the direction to the target. The LookRotation
method creates a rotation that points from the camera’s position to the target’s position.
Line 20: We smoothly pan the camera toward the desired rotation using Quaternion.Slerp
. This process ensures that the camera smoothly follows the target, maintaining a fixed camera position.
After attaching the script, the scene will look like the following:
Now, it’s time to experience the work we have done.
Note: Play the game by clicking the “Run” button below. After a brief moment, the “Output” window will load. Interact with the scene by clicking on the output screen. You can also enjoy the game in a new tab by selecting the link labeled “Your app can be found at:” below.
import React from 'react'; require('./style.css'); import ReactDOM from 'react-dom'; import App from './app.js'; ReactDOM.render( <App />, document.getElementById('root') );
Free Resources