定义
第一步
所需代码如下
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Timeline;
using UnityEngine.Playables;
public class Bezier : MonoBehaviour,ITimeControl
{
public Transform[] transforms;
public GameObject prefab;
GameObject obj;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public Vector3 Be(Vector3[] vs,float p)
{
while(vs.Length>1)
{
Vector3[] vs0 = new Vector3[vs.Length-1];
for (int i = 0; i < vs.Length-1; i++)
{
vs0[i] = Vector3.Lerp(vs[i],vs[i+1],p);
}
vs = vs0;
}
return vs[0];
}
public void SetTime(double time)
{
if (obj!=null)
{
List<Vector3> pos = new List<Vector3>();
for (int i = 0; i < transforms.Length; i++)
{
pos.Add(transforms[i].position);
}
obj.transform.position = Be(pos.ToArray(), (float)time / 5);
}
}
public void OnControlTimeStart()
{
if (obj==null)
{
obj = Instantiate(prefab);
}
obj.transform.position = transforms[0].position;
}
public void OnControlTimeStop()
{
if (obj!=null)
{
Destroy(obj);
obj = null;
}
}
}
创建一个空物体,挂上代码
同时创建属于它的Timeline
第二步
创建4个物体
将4个物体游戏对象拖入脚本中,并选择特效预制体
效果如下
特效小球就会沿着该轨迹运动形成一个弧度