Пробую писать на Unity.
Построил кривую Безье по 4 точкам
про помощи таких двух скриптов
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public static class Bezier
{
public static Vector3 GetPoint(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) {
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
oneMinusT * oneMinusT * oneMinusT * p0 +
3f * oneMinusT * oneMinusT * t * p1 +
3f * oneMinusT * t * t * p2 +
t * t * t * p3;
}
public static Vector3 GetFirstDerivative(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) {
t = Mathf.Clamp01(t);
float oneMinusT = 1f - t;
return
3f * oneMinusT * oneMinusT * (p1 - p0) +
6f * oneMinusT * t * (p2 - p1) +
3f * t * t * (p3 - p2);
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
[ExecuteAlways]
public class BezierTest : MonoBehaviour
{
public Transform P0;
public Transform P1;
public Transform P2;
public Transform P3;
[Range(0,1)]
public float t;
void Update()
{
transform.position = Bezier.GetPoint(P0.position, P1.position, P2.position, P3.position, t);
transform.rotation = Quaternion.LookRotation(Bezier.GetFirstDerivative(P0.position, P1.position, P2.position, P3.position, t));
}
private void OnDrawGizmos() {
int sigmentsNumber = 20;
Vector3 preveousePoint = P0.position;
for (int i = 0; i < sigmentsNumber + 1; i++) {
float paremeter = (float)i / sigmentsNumber;
Vector3 point = Bezier.GetPoint(P0.position, P1.position, P2.position, P3.position, paremeter);
Gizmos.DrawLine(preveousePoint, point);
preveousePoint = point;
}
}
}
Как задать движение по этой траектории при старте проекта?