Created
January 21, 2026 20:56
-
-
Save tombasche/110344618017bfdd5e53b722a66020aa to your computer and use it in GitHub Desktop.
Custom spline animation component for Unity (replacement for Spline Animate)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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