Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.Splines;
- namespace sc.modeling.splines.runtime.auxiliary
- {
- [ExecuteAlways]
- public class ScaleChildColliders : MonoBehaviour
- {
- public SplineMesher splineMesher;
- public int splineIndex = 0;
- public Vector3 baseScale = Vector3.one;
- private void Reset()
- {
- splineMesher = GetComponent<SplineMesher>();
- }
- private void OnEnable()
- {
- //Subscribe
- SplineMesher.onPostRebuildMesh += OnPostRebuild;
- }
- private void OnPostRebuild(SplineMesher instance)
- {
- //Is the instance being rebuild the one we want to work with
- if (instance == splineMesher)
- {
- UnityEngine.Splines.Interpolators.LerpFloat3 float3Interpolator = new UnityEngine.Splines.Interpolators.LerpFloat3();
- BoxCollider[] colliders = gameObject.GetComponentsInChildren<BoxCollider>();
- foreach (BoxCollider box in colliders)
- {
- //Position of box collider in spline's local-space
- Vector3 samplePos = splineMesher.splineContainer.transform.InverseTransformPoint(box.transform.position + box.center);
- //Find the position on the spline that's nearest to the box's center
- SplineUtility.GetNearestPoint(splineMesher.splineContainer.Splines[splineIndex], samplePos, out var nearestPoint, out float t, SplineUtility.PickResolutionMin, 2);
- //Convert the normalized t-index to the distances on the spline
- t = splineMesher.splineContainer.Splines[splineIndex].ConvertIndexUnit(t, PathIndexUnit.Normalized, PathIndexUnit.Distance);
- //Sample the scale data on the spline at the calculated distance
- Vector3 outputScale = splineMesher.scaleData[splineIndex].Evaluate(splineMesher.splineContainer.Splines[splineIndex], t, splineMesher.scaleData[splineIndex].PathIndexUnit, float3Interpolator);
- //Apply scale
- box.transform.localScale = Vector3.Scale(baseScale, outputScale);
- }
- }
- }
- private void OnDisable()
- {
- //Unsubscribe
- SplineMesher.onPostRebuildMesh -= OnPostRebuild;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement