Skip to content

Instantly share code, notes, and snippets.

@tombasche
Created January 21, 2026 20:56
Show Gist options
  • Select an option

  • Save tombasche/110344618017bfdd5e53b722a66020aa to your computer and use it in GitHub Desktop.

Select an option

Save tombasche/110344618017bfdd5e53b722a66020aa to your computer and use it in GitHub Desktop.
Custom spline animation component for Unity (replacement for Spline Animate)
using UnityEngine;
using UnityEngine.Splines;
public class MoveAlongSpline : MonoBehaviour
{
public SplineContainer spline;
float moveSpeed = 0f;
[SerializeField]
private float rotationLerpSpeed = 20f;
private float currentDistance = 0f;
private bool playing = false;
public void Play()
{
playing = true;
}
public float Speed() => moveSpeed;
public void SetSpeed(float value)
{
moveSpeed = value;
}
void Update()
{
if (!playing) { return; }
Vector3 targetPosition = spline.EvaluatePosition(currentDistance);
transform.position = Vector3.MoveTowards(transform.position, targetPosition, moveSpeed * Time.deltaTime);
Vector3 targetDirection = spline.EvaluateTangent(currentDistance);
if (targetDirection != Vector3.zero)
{
Quaternion targetRotation = Quaternion.LookRotation(targetDirection, transform.up);
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, rotationLerpSpeed * Time.deltaTime);
}
if (currentDistance >= 1f)
{
currentDistance = 1f;
playing = false;
}
else
{
float splineLength = spline.CalculateLength();
float movement = moveSpeed * Time.deltaTime / splineLength;
currentDistance += movement;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment