创建一个名为Path的C#脚本
<span style="font-size:24px;"><span style="font-size:24px;">using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class Enemy : MonoBehaviour {public float MoveSpeed = 3.0f; //行走速度private Vector3 Target; //目标位置private float rotateSpeed = 5.0f; //旋转的速度public Path path; //Path脚本private List<Vector3> waypoints; //列表存储waypointsvoid Awake(){waypoints = new List<Vector3>(); //定义一个waypointfor (int i = 0; i < path.WayPotins.Length; i++) //将Path路径的WayPoints全部数组元素存储到waypoints的列表里{waypoints.Add(path.WayPotins[i].position);}GetNextPoint();}void GetNextPoint() //更新下一个waypoint{if (waypoints.Count > 0) //如果当前waypoints里的个数大于0的情况下{Target = waypoints[0]; //Target就为waypoints的第一个元素waypoints.RemoveAt(0); //到达目的地后就把当前waypoint列表的第一个元素给删除掉//以达到更新Target位置的目的print("路点为:"+waypoints.Count);}}//Use this for initializationvoid Start() {}//Update is called one per framevoid Update() {bool reached = MoveToPoint(Target);if (reached){GetNextPoint();}}bool MoveToPoint(Vector3 point) //定义一个布尔类型的移动到目标点的函数{float distance = Vector3.Distance(this.transform.position,point);if (distance < 0.15f) //如果当前位置与目标位子小于0.15的话,就返回true{return true;}//设置当前目标的旋转为目标减去当前位置的方向Quaternion wantedRot = Quaternion.LookRotation(point-this.transform.position);//this.transform.rotation = wantedRot;//控制旋转的插值运算this.transform.rotation = Quaternion.Slerp(this.transform.rotation, wantedRot, rotateSpeed * Time.deltaTime);//移动方向为单位向量Vector3 dir = (point - this.transform.position).normalized;this.transform.Translate(dir * MoveSpeed * Time.deltaTime, Space.World);return false;//不然就默认返回false}
}</span></span>