Advertisement
Staggart

Scale Child Colliders

Apr 1st, 2024
627
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.44 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Splines;
  3.  
  4. namespace sc.modeling.splines.runtime.auxiliary
  5. {
  6.     [ExecuteAlways]
  7.     public class ScaleChildColliders : MonoBehaviour
  8.     {
  9.         public SplineMesher splineMesher;
  10.         public int splineIndex = 0;
  11.  
  12.         public Vector3 baseScale = Vector3.one;
  13.        
  14.         private void Reset()
  15.         {
  16.             splineMesher = GetComponent<SplineMesher>();
  17.         }
  18.        
  19.         private void OnEnable()
  20.         {
  21.             //Subscribe
  22.             SplineMesher.onPostRebuildMesh += OnPostRebuild;
  23.         }
  24.  
  25.         private void OnPostRebuild(SplineMesher instance)
  26.         {
  27.             //Is the instance being rebuild the one we want to work with
  28.             if (instance == splineMesher)
  29.             {
  30.                 UnityEngine.Splines.Interpolators.LerpFloat3 float3Interpolator = new UnityEngine.Splines.Interpolators.LerpFloat3();
  31.                 BoxCollider[] colliders = gameObject.GetComponentsInChildren<BoxCollider>();
  32.                
  33.                 foreach (BoxCollider box in colliders)
  34.                 {
  35.                     //Position of box collider in spline's local-space
  36.                     Vector3 samplePos = splineMesher.splineContainer.transform.InverseTransformPoint(box.transform.position + box.center);
  37.                     //Find the position on the spline that's nearest to the box's center
  38.                     SplineUtility.GetNearestPoint(splineMesher.splineContainer.Splines[splineIndex], samplePos, out var nearestPoint, out float t, SplineUtility.PickResolutionMin, 2);
  39.  
  40.                     //Convert the normalized t-index to the distances on the spline
  41.                     t = splineMesher.splineContainer.Splines[splineIndex].ConvertIndexUnit(t, PathIndexUnit.Normalized, PathIndexUnit.Distance);
  42.                    
  43.                     //Sample the scale data on the spline at the calculated distance
  44.                     Vector3 outputScale = splineMesher.scaleData[splineIndex].Evaluate(splineMesher.splineContainer.Splines[splineIndex], t, splineMesher.scaleData[splineIndex].PathIndexUnit, float3Interpolator);
  45.                    
  46.                     //Apply scale
  47.                     box.transform.localScale = Vector3.Scale(baseScale, outputScale);
  48.                 }
  49.             }
  50.         }
  51.  
  52.         private void OnDisable()
  53.         {
  54.             //Unsubscribe
  55.             SplineMesher.onPostRebuildMesh -= OnPostRebuild;
  56.         }
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement