Skip to content

Instantly share code, notes, and snippets.

@staggartcreations
Created January 5, 2020 08:51
Show Gist options
  • Save staggartcreations/d223b70e2af2b7b2947d9d2c3453cc01 to your computer and use it in GitHub Desktop.
Save staggartcreations/d223b70e2af2b7b2947d9d2c3453cc01 to your computer and use it in GitHub Desktop.
Unity script for creating a circular, arched or spiraling cinemachine path
using UnityEngine;
using Cinemachine;
using Waypoint = Cinemachine.CinemachineSmoothPath.Waypoint;
public class CinemachinePathShape : MonoBehaviour
{
public CinemachineSmoothPath path;
[Space]
[Range(2, 32)]
public int points = 16;
[Range(1f, 50f)]
public float radius = 5f;
[Range(0f, 360f)]
public float startAngle = 0;
[Range(15f, 360f)]
public float endAngle = 360f;
[Range(0f, 30f)]
public float elevation = 0f;
private Waypoint[] m_Waypoints = new Waypoint[0];
private void OnValidate()
{
if (!path) path = this.GetComponent<CinemachineSmoothPath>();
if (!path) return;
Apply();
}
public void Apply()
{
m_Waypoints = new Waypoint[points];
float angle = startAngle;
float length = endAngle - startAngle;
for (int i = 0; i < points; i++)
{
float t = (float)i / (float)points;
m_Waypoints[i].position = new Vector3(
Mathf.Sin(Mathf.Deg2Rad * angle) * (radius * 0.5f),
t * (elevation * 0.5f),
Mathf.Cos(Mathf.Deg2Rad * angle) * (radius * 0.5f));
angle += (length / (float)points);
}
path.m_Waypoints = m_Waypoints;
path.InvalidateDistanceCache();
}
}
@bunnybreaker
Copy link

This saved me a chunk of time. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment