可以做全自动的动画,只要设置好路径点就可以了,
1private Transform[] ways1;//路径点的位置 2 public Transform tagret;//移动的物体 3 private int index1; 4 5 private void Start() 6 { 7 ways1 = JieLine.instance.LuJing; 8 index1 = 0; 9 tagret.transform.gameObject.SetActive(false); 10 } 11 public void MoveToway1() 12 { 13 if (index1 > ways1.Length - 1) { return; } 14 tagret.localPosition = Vector3.MoveTowards(tagret.localPosition, ways1[index1].localPosition, speed * Time.deltaTime); 15 if (Vector3.Distance(ways1[index1].localPosition, tagret.localPosition) < 0.01f) 16 { 17 index1++; 18 19 if (index1 == ways1.Length) 20 { 21 tagret.localPosition = ways1[index1 - 1].localPosition; 22 23 } 24 } 25 } 26 27//多个位置移动的方法 28 public void MoveToway1(Transform obj,Transform []way,ref int index1,float speed) 29 { 30 if (index1 > ways1.Length - 1) { return; } 31 obj.localPosition = Vector3.MoveTowards(obj.localPosition, ways1[index1].localPosition, speed * Time.deltaTime); 32 if (Vector3.Distance(ways1[index1].localPosition, obj.localPosition) < 0.00001f) 33 { 34 index1++; 35 36 if (index1 == ways1.Length) 37 { 38 obj.localPosition = ways1[index1 - 1].localPosition; 39 40 } 41 } 42 } 43 //单个位置移动的方法 44 public void MoveToway1(Transform obj,Transform way,float speed) 45 { 46 if (Vector3.Distance(way.localPosition, obj.localPosition) > 0.0001f) 47 { 48 obj.localPosition = Vector3.MoveTowards(obj.localPosition, way.localPosition, speed * Time.deltaTime); 49 } 50 } 51 //物体平滑旋转的 方法 52 public void MoveToway1(GameObject obj,Quaternion vector3,float speed) 53 { 54 obj.transform.localRotation = Quaternion.Slerp(huizhuangang.transform.localRotation, 55 vector3, speed * Time.deltaTime); 56 }