physics
[Splines] Prevent Jumps During Seamless Path Transitions
Solution
Unity 2022.1.x - Unity 6.3.x
Published 9 days ago
Abrupt snapping occurs when a Transform moves between two SplineContainer instances at high velocities. This is usually observed when distanceTraveled is reset upon reaching the end of a segment, causing a position mismatch or a one-frame teleportation to the origin of the next path.
Quick-Fix
To maintain continuity between paths, do not reset your distance value. Instead, update your current spline and subtract the previous spline length from distanceTraveled to preserve the object's relative momentum.
using UnityEngine;
using UnityEngine.Splines;
using Unity.Mathematics;
public class SplineFollower : MonoBehaviour
{
public SplineContainer currentSpline;
public SplineContainer nextSpline;
public float speed = 5f;
private float distanceTraveled;
void Update()
{
if (currentSpline == null) return;
float splineLength = currentSpline.CalculateLength();
distanceTraveled += speed * Time.deltaTime;
if (distanceTraveled >= splineLength && nextSpline != null)
{
// Transition logic: Carry over the distance to the next spline
currentSpline = nextSpline;
distanceTraveled -= splineLength;
nextSpline = null;
}
float3 position = currentSpline.EvaluatePosition(distanceTraveled / currentSpline.CalculateLength());
transform.position = position;
}
}
Related Posts Haven't quite found a solution to your problem? We think these posts might help you.
[2.5D] Fix Sprite Jitter and Snapping During Movement[URP] Fix Incorrect Prefab Rotation and Forward Axis During Instantiation[NavMesh] Fix AI Agent Teleporting to Origin on Start
Content inspired by a Unity discussion post.