Advertisement
Selzier

Bezier

Jul 31st, 2017
179
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.09 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Bezier : MonoBehaviour {
  6.     public LineRenderer lineRenderer;
  7.     public Transform point0, point1, point2;
  8.     private int numPoints = 50;
  9.     private Vector3[] positions = new Vector3[50];
  10.     private float timer;
  11.  
  12.     private void Start() {
  13.         lineRenderer.positionCount = numPoints;
  14.     }
  15.     private void Update() {
  16.         timer += Time.deltaTime;
  17.         DrawQuadraticCurve();
  18.     }
  19.  
  20.     private void DrawQuadraticCurve() {
  21.         for (int i = 1; i < numPoints + 1; i++) {
  22.             float t = i / (float)numPoints;
  23.             positions[i - 1] = CalcQuadraticBezierPoint(t * timer, point0.position, point1.position, point2.position);
  24.         }
  25.         lineRenderer.SetPositions(positions);
  26.     }
  27.  
  28.     private Vector3 CalcQuadraticBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2) {
  29.         float u = 1 - t;
  30.         float tt = t * t;
  31.         float uu = u * u;
  32.  
  33.         Vector3 p = uu * p0;
  34.         p += 2 * u * t * p1;
  35.         p += tt * p2;
  36.         return p;
  37.     }
  38. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement