Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class Bezier : MonoBehaviour {
- public LineRenderer lineRenderer;
- public Transform point0, point1, point2;
- private int numPoints = 50;
- private Vector3[] positions = new Vector3[50];
- private float timer;
- private void Start() {
- lineRenderer.positionCount = numPoints;
- }
- private void Update() {
- timer += Time.deltaTime;
- DrawQuadraticCurve();
- }
- private void DrawQuadraticCurve() {
- for (int i = 1; i < numPoints + 1; i++) {
- float t = i / (float)numPoints;
- positions[i - 1] = CalcQuadraticBezierPoint(t * timer, point0.position, point1.position, point2.position);
- }
- lineRenderer.SetPositions(positions);
- }
- private Vector3 CalcQuadraticBezierPoint(float t, Vector3 p0, Vector3 p1, Vector3 p2) {
- float u = 1 - t;
- float tt = t * t;
- float uu = u * u;
- Vector3 p = uu * p0;
- p += 2 * u * t * p1;
- p += tt * p2;
- return p;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement