public void ShrinkToLength(float targetLength) { // Base mesh { // How many rings to remove float headPatchLength = GetPatchLength(headPatch); int targetRings = (int)((targetLength - headPatchLength) / interval) + 1; // Leave at least one ring targetRings = Mathf.Max(targetRings, 1); while (snakeMesh.Count > targetRings) snakeMesh.PopFromStart(); } // Tail position { float tailPatchLength = targetLength - GetPatchLength(headPatch) - baseLength; float factor = tailPatchLength / interval; ValueTransform tailRing = ValueTransform.lerp(snakeMesh.Kernel.Path[0], lastPoppedRing, factor); tailRing.SetTransform(tail); UpdateTailPatch(tailRing); // UV offset float distanceTraveled = baseDistanceTraveled - baseLength - tailPatchLength; baseTailOffset = tailMaterial.GetUnscaledTextureOffset(); baseTailOffset.y = distanceTraveled / snakeMesh.RingLength; tailMaterial.SetUnscaledTextureOffset(baseTailOffset); } }