Skip to content

Instantly share code, notes, and snippets.

@danielbierwirth
Created November 18, 2024 20:01
Show Gist options
  • Save danielbierwirth/581480b1837fb5571895053501cbe70b to your computer and use it in GitHub Desktop.
Save danielbierwirth/581480b1837fb5571895053501cbe70b to your computer and use it in GitHub Desktop.
This Unity C# script animates the camera's movement along a predefined path using transform waypoints while maintaining focus on a specific point of interest. Ideal for creating quick and efficient cinematic camera movements.
using UnityEngine;
// This code allows the camera to focus on a specific object while
// moving through predefined waypoints, enhancing the cinematic effect
//and dynamic camera movement in your game or application.
public class CinematicCameraPath : MonoBehaviour
{
[SerializeField] private Camera cameraToAnimate; // The camera that gets animated
[SerializeField] private Transform[] waypoints; // The points the camera will move between
[SerializeField] private float speed = 5.0f; // The speed of the camera movement
[SerializeField] private bool loop = false; // Whether the path should loop
[SerializeField] private Transform focusObject; // The object the camera focuses on
[SerializeField] private bool isAnimating = false; // Is the camera currently animating?
private int currentWaypointIndex = 0; // Index of the current waypoint
void Start()
{
if (waypoints.Length > 0)
{
InitializePath();
}
else
{
Debug.LogError("No waypoints defined for the camera path.");
}
}
void Update()
{
if (isAnimating && waypoints.Length > 0)
{
MoveCameraAlongPath();
}
}
private void InitializePath()
{
currentWaypointIndex = 0;
SetCameraToCurrentWaypoint();
isAnimating = true;
}
private void MoveCameraAlongPath()
{
Transform targetWaypoint = waypoints[currentWaypointIndex];
// Move the camera towards the target waypoint
cameraToAnimate.transform.position = Vector3.MoveTowards(cameraToAnimate.transform.position, targetWaypoint.position, speed * Time.deltaTime);
// Rotate the camera towards the focus object or towards the current waypoint's rotation
if (focusObject)
{
cameraToAnimate.transform.LookAt(focusObject);
}
else
{
cameraToAnimate.transform.rotation = Quaternion.RotateTowards(cameraToAnimate.transform.rotation, targetWaypoint.rotation, speed * Time.deltaTime);
}
// Check if the camera has reached the target waypoint
if (Vector3.Distance(cameraToAnimate.transform.position, targetWaypoint.position) < 0.1f)
{
currentWaypointIndex++;
// Loop or stop animation based on the loop setting
if (currentWaypointIndex >= waypoints.Length)
{
if (loop)
{
currentWaypointIndex = 0;
}
else
{
isAnimating = false;
}
}
}
}
private void SetCameraToCurrentWaypoint()
{
if (currentWaypointIndex >= 0 && currentWaypointIndex < waypoints.Length)
{
cameraToAnimate.transform.position = waypoints[currentWaypointIndex].position;
if (focusObject)
{
cameraToAnimate.transform.LookAt(focusObject);
}
else
{
cameraToAnimate.transform.rotation = waypoints[currentWaypointIndex].rotation;
}
}
}
// Public function to start animating the camera path
public void StartAnimating()
{
if (waypoints.Length > 0)
{
isAnimating = true;
}
else
{
Debug.LogError("No waypoints defined for the camera path.");
}
}
// Public function to stop animating the camera path
public void StopAnimating()
{
isAnimating = false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment