Мне нужно реализовать ручной сценарий кривой Безье для камеры в моей сцене. Я НЕ МОГУ использовать любые методы, предоставленные единицей, для реализации кривой Безье. Я правильно понял формулу, но я не знаю, как заставить камеру следовать кривой, не говоря уже о 4. Я не мог создать массив и просто применить скрипт к каждой камере, если это возможно. У меня есть скрипт lineRenderer, который показывает кривую:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Bezier : MonoBehaviour
{
public LineRenderer lineRenderer;
public Transform point0, point1, point2, point3;
private int cPoints = 50;
private Vector3[] positions = new Vector3[50];
// Start is called before the first frame update
void Start()
{
lineRenderer.positionCount = cPoints;
DrawCubicCurve();
}
// Update is called once per frame
void Update()
{
DrawCubicCurve();
}
private void DrawCubicCurve()
{
for (int i = 1; i < cPoints - 1; i++)
{
float t = i / (float)cPoints;
positions[i - 1] = CalculateCubicBezierCurve(t, point0.position, point1.position, point2.position, point3.position);
}
lineRenderer.SetPositions(positions);
}
private Vector3 CalculateCubicBezierCurve(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
float u = 1 - t;
float t2 = t * t;
float uu = u * u;
float uuu = uu * u;
float t3 = t2 * t;
Vector3 p = uuu * p0;
p += 3 * uu * t * p1;
p += 3 * u * t2 * p2;
p += t3 * p3;
return p;
}
}
Затем у меня есть скрипт, который я пытаюсь заставить эту камеру двигаться по такой кривой:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CamBezier : MonoBehaviour
{
public GameObject[] cam;
public Transform point0, point1, point2, point3;
private float cPoints = 50.0f;
private Vector3[] positions = new Vector3[50];
// Start is called before the first frame update
void Start()
{
for (int i = 1; i < cam.Length - 1; i++)
{
float x = cam[i].transform.position.x;
float y = cam[i].transform.position.y;
float z = cam[i].transform.position.z;
}
DrawCubicCurve();
}
// Update is called once per frame
void Update()
{
DrawCubicCurve();
}
private void DrawCubicCurve()
{
for (int i = 1; i < cam.Length - 1; i++)
{
float t = i / (float)cPoints;
positions[i - 1] = CalculateCubicBezierCurve(t, point0.position, point1.position, point2.position, point3.position);
cam.transform.position = positions;
}
}
private Vector3 CalculateCubicBezierCurve(float t, Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3)
{
float u = 1 - t;
float t2 = t * t;
float uu = u * u;
float uuu = uu * u;
float t3 = t2 * t;
Vector3 p = uuu * p0;
p += 3 * uu * t * p1;
p += 3 * u * t2 * p2;
p += t3 * p3;
return p;
}
}
Опять не могу использовать любые методы, предоставленные единицей для реализации кривой Безье. Цель состоит в том, чтобы заставить камеру двигаться по кривой Безье. Создание массива из них является дополнительным.