Unity3d NavMeshAgent 路径点寻路

using UnityEngine;

using System.Collections;

using System.Collections.Generic;


public class MotoCtrl : MonoBehaviour {

public NavMeshAgent moto;

public List<Transform> targets;

public int i=0;

public float speed =1.2f;

public Transform player;

public GameRef gr;

public GameObject[] spawnEnemy;

public GameObject thisNPC;

private int frameNum = 10;

void Start()

{

player = gr.gameCtrl.playerHolder.transform;

moto.SetDestination(targets[i].position);

moto.speed = speed;

}

void OnTriggerEnter(Collider other) {

if(other.name.Contains("way"))

{

StartCoroutine(CastRandom());

}

    }

void Update()

{

if(Time.frameCount%frameNum==0)

{

StartCoroutine(CheckDistance());

}

}

IEnumerator CastRandom()

{

int cur=i;

while(cur==i)

{

i=i+1;

if(i>targets.Count-1)

{

i=0;

}

yield return moto.SetDestination(targets[i].position);

yield return null;

}

}

IEnumerator CheckDistance()

{

float dot = Vector3.Dot(player.forward,transform.forward);

float dis = Vector3.Distance(player.position,transform.position);

if(dot<-0.9f&&dis<4)

{

moto.Stop();

yield return animation.Play("xiache");

animation.wrapMode = WrapMode.Once;

yield return new WaitForSeconds(1);

foreach(GameObject a in spawnEnemy)

{

GameObject b=(GameObject)Instantiate(a,transform.localPosition+new Vector3(Random.Range(0,0.2f),0,0),transform.rotation);

b.SetActive(true);

}

thisNPC.SetActive(false);

}

}

}


你可能感兴趣的:(Unity3d)