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 Splines, this helps immensely. Thank you.
Reply


Messages In This Thread
Curvy spline to 2D Edge collider - by _Aka_ - 03-08-2018, 12:00 PM
RE: Curvy spline to 2D Edge collider - by _Aka_ - 03-09-2018, 07:23 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Spline Issue Leading to Unity Freeze Pankaj09 1 329 08-24-2026, 11:28 PM
Last Post: _Aka_
  Connecting Multiple Spline Nodes Issue Ritesh Kadam 1 286 08-22-2026, 11:24 PM
Last Post: _Aka_
  New User Looking for Advice on Choosing Curvy Splines ravigupta 1 301 08-21-2026, 09:45 PM
Last Post: _Aka_
  Spline Spots from 2 samples. BitBlit 8 1,353 06-30-2026, 11:49 AM
Last Post: _Aka_

Forum Jump: