Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Curvy spline to 2D Edge collider
#1
Hi,
I got a question from a user about how to convert a spline to 2D Edge collider. I wrote a small script for him that does so. I post it here, maybe it will help someone else:

Code:
using FluffyUnderware.Curvy;
using UnityEngine;

[RequireComponent(typeof(CurvySpline))]
[RequireComponent(typeof(EdgeCollider2D))]
[ExecuteInEditMode]
public class CurvySplineToEdgeCollider : MonoBehaviour
{
   private EdgeCollider2D edgeCollider;
   private CurvySpline spline;

   void OnEnable()
   {
       edgeCollider = GetComponent<EdgeCollider2D>();
       spline = GetComponent<CurvySpline>();

       spline.OnRefresh.AddListenerOnce(OnRefresh);
       RebuildCollider();
   }

   void OnDisable()
   {
       spline.OnRefresh.RemoveListener(OnRefresh);
       edgeCollider = null;
       spline = null;
   }

   private void OnRefresh(CurvySplineEventArgs arg0)
   {
       RebuildCollider();
   }

   private void RebuildCollider()
   {
       if (spline.Interpolation != CurvyInterpolation.Linear)
           Debug.LogWarning("Spline's interpolation should be Linear. Current value is " + spline.Interpolation);
       if(spline.ControlPointCount < 2)
           Debug.LogWarning("Spline should have at least two points. Current count is " + spline.ControlPointCount);
       else
       {
           Vector2[] newPoints = new Vector2[spline.ControlPointCount];
           for (int i = 0; i < newPoints.Length; i++)
           {
               newPoints[i].x = spline.ControlPoints[i].transform.localPosition.x;
               newPoints[i].y = spline.ControlPoints[i].transform.localPosition.y;
           }

           edgeCollider.points = newPoints;
       }
   }
}
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply
#2
Another version that works with curved splines
Code:
using FluffyUnderware.Curvy;
using UnityEngine;

[RequireComponent(typeof(CurvySpline))]
[RequireComponent(typeof(EdgeCollider2D))]
[ExecuteInEditMode]
public class CurvySplineToEdgeCollider : MonoBehaviour
{
   [Range(2,100)]
   public int PointsCount = 20;

   private EdgeCollider2D edgeCollider;
   private CurvySpline spline;

   void OnEnable()
   {
       edgeCollider = GetComponent<EdgeCollider2D>();
       spline = GetComponent<CurvySpline>();

       spline.OnRefresh.AddListenerOnce(OnRefresh);
       RebuildCollider();
   }

   void OnDisable()
   {
       spline.OnRefresh.RemoveListener(OnRefresh);
       edgeCollider = null;
       spline = null;
   }

   private void OnRefresh(CurvySplineEventArgs arg0)
   {
       RebuildCollider();
   }

   private void RebuildCollider()
   {
       if(spline.Count == 0)
           Debug.LogWarning("Spline should have at least one segement.");
       else
       {
           Vector2[] newPoints = new Vector2[PointsCount];
           for (int i = 0; i < PointsCount; i++)
           {
               float tf = (float)i / (PointsCount - 1);
               Vector3 position = spline.Interpolate(tf);
               newPoints[i].x = position.x;
               newPoints[i].y = position.y;
           }
           edgeCollider.points = newPoints;
       }
   }

   private void OnValidate()
   {
       if(spline)
           RebuildCollider();
   }
}
Please consider leaving a review for Curvy. This will help a lot keeping Curvy relevant in the eyes of the Asset Store algorithm.
Reply


Possibly Related Threads...
Thread Author Replies Views Last Post
  How to spawn GameObjects on corners of the liinear spline? Pixel Flipper 7 86 09-18-2019, 07:03 PM
Last Post: _Aka_
  Best practice for mass-object instancing along 100km spline (railways) plus streaming bs@vectorvision.de 8 140 09-18-2019, 06:48 PM
Last Post: _Aka_
Information Curvy 6.0.1 is live _Aka_ 0 38 09-09-2019, 11:56 AM
Last Post: _Aka_
Information Curvy 6.0.0 is live _Aka_ 5 265 09-06-2019, 12:49 PM
Last Post: _Aka_

Forum Jump: