using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class NewBehaviourScript : MonoBehaviour {
public Transform[] points;
private int destPoint = 0;
private NavMeshAgent nav;
static Animator anim;//動畫面板
void Start()
{
anim = GetComponent<Animator>();
nav = GetComponent<NavMeshAgent>();
//禁用自動制動允許點之間的連續移動(即代理程序不會因為進入目標點而減慢)。
// nav.autoBraking = false; //當啟用時,代理將在到達目的地時減慢速度。
GotoNextPoint();
}
void GotoNextPoint()
{
//如果沒有設置點(長度),則返回
if (points.Length == 0)
return; //返回
{
anim.SetBool("isWalk", true);
//設置代理進入當前選擇的目的地。
nav.destination = points[destPoint].position;
//agent.SetDestination(points[destPoint].transform.position);
//選擇陣列中的下一個點作為目的地,如有必要,循環到開始。
destPoint = (destPoint + 1) % points.Length;
}
}
void Update()
{
//當代理程序接近當前目標點時,選擇下一個目標點。
if (nav.remainingDistance < 0.5f)
GotoNextPoint();
}
}