可以做全自动的动画,只要设置好路径点就可以了,
private Transform[] ways1;//路径点的位置
public Transform tagret;//移动的物体
private int index1;
private void Start()
{
ways1 = JieLine.instance.LuJing;
index1 = 0;
tagret.transform.gameObject.SetActive(false);
}
public void MoveToway1()
{
if (index1 > ways1.Length - 1) { return; }
tagret.localPosition = Vector3.MoveTowards(tagret.localPosition, ways1[index1].localPosition, speed * Time.deltaTime);
if (Vector3.Distance(ways1[index1].localPosition, tagret.localPosition) < 0.01f)
{
index1++;
if (index1 == ways1.Length)
{
tagret.localPosition = ways1[index1 - 1].localPosition;
}
}
}
//多个位置移动的方法
public void MoveToway1(Transform obj,Transform []way,ref int index1,float speed)
{
if (index1 > ways1.Length - 1) { return; }
obj.localPosition = Vector3.MoveTowards(obj.localPosition, ways1[index1].localPosition, speed * Time.deltaTime);
if (Vector3.Distance(ways1[index1].localPosition, obj.localPosition) < 0.00001f)
{
index1++;
if (index1 == ways1.Length)
{
obj.localPosition = ways1[index1 - 1].localPosition;
}
}
}
//单个位置移动的方法
public void MoveToway1(Transform obj,Transform way,float speed)
{
if (Vector3.Distance(way.localPosition, obj.localPosition) > 0.0001f)
{
obj.localPosition = Vector3.MoveTowards(obj.localPosition, way.localPosition, speed * Time.deltaTime);
}
}
//物体平滑旋转的 方法
public void MoveToway1(GameObject obj,Quaternion vector3,float speed)
{
obj.transform.localRotation = Quaternion.Slerp(huizhuangang.transform.localRotation,
vector3, speed * Time.deltaTime);
}