Skip to content

Instantly share code, notes, and snippets.

@SiarheiPilat
Created December 17, 2019 00:15
Show Gist options
  • Save SiarheiPilat/ed8f9d16fc4f25423ae7ff89e7dfa88a to your computer and use it in GitHub Desktop.
Save SiarheiPilat/ed8f9d16fc4f25423ae7ff89e7dfa88a to your computer and use it in GitHub Desktop.
Bezier Curve Line Renderer for Unity, credit goes to World of Zero: https://www.youtube.com/watch?v=tgCFzoG_BJM
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// Bezier Curve Line Renderer for Unity, credit goes to World of Zero: https://www.youtube.com/watch?v=tgCFzoG_BJM
/// </summary>
[ExecuteInEditMode]
public class BezierCurve3PointLineRenderer : MonoBehaviour
{
public Transform point1;
public Transform point2;
public Transform point3;
public LineRenderer lineRenderer;
public int vertexCount = 12;
void Update()
{
var pointList = new List<Vector3>();
for (float ratio = 0; ratio <= 1; ratio += 1.0f / vertexCount)
{
var tangentLineVertex1 = Vector3.Lerp(point1.position, point2.position, ratio);
var tangentLineVertex2 = Vector3.Lerp(point2.position, point3.position, ratio);
var bezierpoint = Vector3.Lerp(tangentLineVertex1, tangentLineVertex2, ratio);
pointList.Add(bezierpoint);
}
lineRenderer.positionCount = pointList.Count;
lineRenderer.SetPositions(pointList.ToArray());
}
private void OnDrawGizmos()
{
Gizmos.color = Color.green;
Gizmos.DrawLine(point1.position, point2.position);
Gizmos.color = Color.cyan;
Gizmos.DrawLine(point2.position, point3.position);
Gizmos.color = Color.red;
for (float ratio = 0.5f / vertexCount; ratio < 1; ratio += 1.0f / vertexCount)
{
Gizmos.DrawLine(Vector3.Lerp(point1.position, point2.position, ratio), Vector3.Lerp(point2.position, point3.position, ratio));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment