Creating an Interactive Camera with Cinemachine and C#

Alberto Garcia
3 min readJul 15, 2023

--

In game development, an interactive camera can greatly enhance the player’s experience by providing dynamic and engaging perspectives. By utilizing Cinemachine, a powerful virtual camera system in Unity, in combination with C# scripting, developers can create interactive camera functionalities. In this article, we will explore how to make an interactive camera using Cinemachine and C#, showcasing its benefits and usefulness in the industry.

I’ve written the following code to exemplify some basic functionalities for this case:

using Cinemachine;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CameraController : MonoBehaviour
{
[SerializeField]
private CinemachineVirtualCamera VCam1, VCam2, VCam3;
[SerializeField]
private GameObject _cube;
[SerializeField]
private GameObject _capsule;

private bool _firstTarget;
private bool _camsOff;

void Start()
{
VCam1.gameObject.SetActive(true);
VCam2.gameObject.SetActive(true);
VCam3.gameObject.SetActive(true);

VCam1.m_Lens.FieldOfView = 80;
VCam3.m_Lens.FieldOfView = 60;
_camsOff = false;
}

void Update()
{
if(Input.GetKeyDown(KeyCode.E))
{
if (!_camsOff)
{
VCam1.gameObject.SetActive(false);
VCam2.gameObject.SetActive(false);
_camsOff = true;
}
else
{
VCam1.gameObject.SetActive(true);
VCam2.gameObject.SetActive(true);
_camsOff = false;
}
}
if(Input.GetKeyDown(KeyCode.R))
{
SwitchTarget();
}
if(Input.GetKeyDown(KeyCode.Space))
{
float FOV = VCam3.m_Lens.FieldOfView -= 20;
if (FOV < 19)
{
VCam3.m_Lens.FieldOfView = 60;
}
}
}

private void SwitchTarget()
{
if(_firstTarget == true)
{
VCam3.LookAt = _cube.transform;
_firstTarget = false;
}
else if (_firstTarget == false)
{
VCam3.LookAt = _capsule.transform;
_firstTarget = true;
}
}
}

Let’s examine its benefits and usefulness!

Multiple Camera Views: The code includes multiple virtual cameras, namely VCam1, VCam2, and VCam3. By activating and deactivating these cameras, developers can easily switch between different camera perspectives, enhancing the visual variety and allowing for versatile gameplay experiences.

Field of View Control: The code demonstrates how to dynamically adjust the field of view (FOV) of VCam3 using the space bar. By altering the FOV, developers can create various visual effects and control the level of immersion and focus for different scenes or gameplay situations.

Target Switching: The SwitchTarget() method showcased in the code allows developers to switch the camera’s focus between two objects, a cube and a capsule. This feature can be utilized to follow different characters or objects in the game world, enabling more interactive and dynamic camera behavior.

Camera Activation and Deactivation: By pressing the ‘E’ key, the code enables or disables the virtual cameras VCam1 and VCam2. This functionality provides developers with the ability to toggle the visibility of specific cameras, which can be useful for implementing cinematic sequences, cutscenes, or transitioning between different gameplay states.

Easy Customization and Extension: The provided code serves as a foundation for creating an interactive camera, but it can be easily customized and extended to suit the specific needs of different games. Developers can modify camera settings, add more cameras, or incorporate additional functionality based on their game’s requirements and desired player experience.

Here we can see three times camera switch, the FOV change as well as LookAt change between cube and capsule.

testing the script

The integration of Cinemachine and C# scripting allows developers to create interactive camera systems with ease, empowering them to craft immersive and engaging gameplay experiences in little time. By leveraging the code’s functionalities, including multiple camera views, field of view control, target switching, and camera activation/deactivation, developers can enhance the visual storytelling, player engagement, and overall polish of their games.

The benefits and usefulness of an interactive camera using Cinemachine and C# are vast, offering flexibility, dynamic camera behavior, and the ability to adapt camera perspectives to suit different gameplay scenarios. With the provided code as a starting point, developers can explore and expand upon these functionalities, creating unique and captivating camera experiences in their Unity projects.

--

--

Alberto Garcia
Alberto Garcia

No responses yet