using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class beiseer : MonoBehaviour {
public GameObject p0;
public GameObject p1;
public GameObject p2;
public GameObject p3;
public GameObject pp;
// public GameObject[] pg;
Vector3 pp0;
Vector3 pp1;
Vector3 pp2;
Vector3 pp3;
Vector3 vp;
Vector3[] vshu ;
List<Vector3> lv = new List<Vector3>() ;
float t=0;
float tim = 0;
// Use this for initialization
void Start () {
//自动添加到 list 里
// for (int i = 0; i < pg.Length; i++)
// {
// lv.Add(pg[i].transform.position);
// }
pp0 = p0.transform.position;
pp1 = p1.transform.position;
pp2 = p2.transform.position;
pp3 = p3.transform.position;
vshu = new Vector3[] { pp0, pp1, pp2, pp3 };
lv.AddRange(vshu);
tim += Time.time + 0.1f;
}
// Update is called once per frame
void Update () {
pp.transform.position = vp;
Bezier( t,lv);
if (t>=1f)
{
Destroy( p0.GetComponent<beiseer>());
}
if (tim<Time.time)
{
t += 0.01f;
tim += 0.1f;
}
}
// 线性:俩点之间运动
Vector3 Bezier(Vector3 p0, Vector3 p1, float t)
{
print((1 - t) * p0 + t * p1);
vp = (1 - t) * p0 + t * p1;
return (1 - t) * p0 + t * p1;
}
// 二阶曲线:俩点之间相互插值 p0和p1,p1和p2,再求这俩个插值之间的插值
Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 p2, float t)
{
Vector3 p0p1 = (1 - t) * p0 + t * p1;
Vector3 p1p2 = (1 - t) * p1 + t * p2;
Vector3 result = (1 - t) * p0p1 + t * p1p2;
print(result);
vp = result;
return result;
}
// 三阶曲线:这个和前面一样,只是多一个点就多一层计算插值
Vector3 Bezier(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t)
{
Vector3 result;
Vector3 p0p1 = (1 - t) * p0 + t * p1;
Vector3 p1p2 = (1 - t) * p1 + t * p2;
Vector3 p2p3 = (1 - t) * p2 + t * p3;
Vector3 p0p1p2 = (1 - t) * p0p1 + t * p1p2;
Vector3 p1p2p3 = (1 - t) * p1p2 + t * p2p3;
result = (1 - t) * p0p1p2 + t * p1p2p3;
vp = result;
return result;
}
// n阶曲线:这个就是和前面一样,就是不停的计算插值,都是有规律的,用递归实现
public Vector3 Bezier(float t, List<Vector3> p)
{
if (p.Count < 2)
return p[0];
List<Vector3> newp = new List<Vector3>();
for (int i = 0; i < p.Count - 1; i++)
{
//划线连接
Debug.DrawLine(p[i], p[i + 1]);
Vector3 p0p1 = (1 - t) * p[i] + t * p[i + 1];
newp.Add(p0p1);
}
vp = Bezier(t, newp);
return Bezier(t, newp);
}
}