Bootstrap

Unity中物体在固定路线移动

using UnityEngine;
using System.Collections;
using System;

public class NavigationFixedPath : MonoBehaviour {
1. //定义物体移动位置 的数组
GameObject[] pathPoints;
//记录下一个路点
int nextPathPointIndex = 1;
// Use this for initialization
2. void Start () {
pathPoints = GameObject.FindGameObjectsWithTag(“Path01”);
//Array.Reverse(pathPoints); 用该方法或者下面的方法对路线排序
Array.Sort(pathPoints, (x, y) => { return x.gameObject.name.CompareTo(y.gameObject.name); });
transform.position = pathPoints[0].transform.position;
transform.forward = pathPoints[nextPathPointIndex].transform.position - transform.position;
}
// Update is called once per frame
3. void Update () {
if (Vector3.Distance(pathPoints[nextPathPointIndex].transform.position, transform.position) < 0.1f)
{
if (nextPathPointIndex != pathPoints.Length - 1)
{
nextPathPointInd

;