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
  ScanConnections() causes heavy load in play mode, even with all Curvy objects removed bennett 5 17 Yesterday, 03:32 PM
Last Post: bennett
  player stuck on spline Dextex 4 63 07-17-2021, 04:52 PM
Last Post: _Aka_
  Locking the UV's of a closed spline John_MLS 1 105 07-09-2021, 03:54 PM
Last Post: _Aka_
  Accessing generator's input spline in script, changing it at runtime smackledorf 3 277 06-17-2021, 01:59 PM
Last Post: _Aka_

Forum Jump: