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
Information Curvy 4.0.1 is live _Aka_ 2 35 09-17-2018, 11:09 AM
Last Post: _Aka_
Information Curvy 4.0.0 is live _Aka_ 0 39 09-07-2018, 02:07 PM
Last Post: _Aka_
Information Curvy 3.0.0 is live _Aka_ 3 114 09-05-2018, 04:58 PM
Last Post: _Aka_
  Freewalk mode, then return to spline mythstified 6 152 08-30-2018, 10:29 AM
Last Post: Jacki

Forum Jump: