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
  Curvy Line Renderer for UI Spline? gekido 3 6 04-04-2024, 12:56 PM
Last Post: _Aka_
  8.8.0 is live, and it improves Curvy Generator greatly _Aka_ 1 10 04-03-2024, 03:16 PM
Last Post: _Aka_
  snap to the curve created by the curvy splines segment points ShiroeYamamoto 3 11 04-02-2024, 02:24 PM
Last Post: _Aka_
  Get position of all control points for a spline gekido 1 6 03-28-2024, 10:08 PM
Last Post: _Aka_

Forum Jump: