原因我也不知道。运行时,主角转圈,取消脚本,就可以了。
所以,我使用两个脚本,一个主脚本,一个判断主脚本活死的脚本。
主脚本代码:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class lsscript : MonoBehaviour
{
public GameObject enemy;
public float speed;
private NavMeshAgent b;
public GameObject object1;//chu xing shi
public GameObject object2;//tong guan fang
public GameObject object3;//empty object
public int c=1;
public bool c2;
public AudioClip ls1;
public AudioClip ls2;
public bool jc;
// Start is called before the first frame update
// Update is called once per frame
private void Start()
{
enemy = object1;
b = GetComponent<NavMeshAgent>();
b.SetDestination(enemy.transform.position);
//var a = (enemy.transform.position - transform.position).normalized;
//transform.Translate(a * speed * Time.deltaTime)
GetComponent<ai>().enabled = true;
}
void Update()
{
//b.updateRotation = false;
b.SetDestination(enemy.transform.position);
//var a = (enemy.transform.position - transform.position).normalized;
//transform.Translate(a * speed * Time.deltaTime)
if (c == 2)
{
enemy=object2;
}
else if (c == 1)
{
enemy = object1;
}else if (c == 3)
{
enemy = object3;
}
if (!c2)
{
if (!GetComponent<AudioSource>().isPlaying)
{
GetComponent<AudioSource>().loop = true;
GetComponent<AudioSource>().clip = ls1;
GetComponent<AudioSource>().Play();
}
}
else if (c2)
{
if (!GetComponent<AudioSource>().isPlaying)
{
GetComponent<AudioSource>().loop = false;
GetComponent<AudioSource>().clip = ls2;
GetComponent<AudioSource>().Play();
}
}
}
private void OnTriggerEnter(Collider other)
{
if (other.name == "fhf") {
object3 = other.gameObject;
c = 3;
c2 = true;
b.speed = 6;
StartCoroutine(six());
jc = true;
StartCoroutine(sixa());
}//object1
else if (other.name == "tgf")//tong guan fang
{
c = 1;
jc = true;
StartCoroutine(sixa());
}
else if (other.name == "cxs")
{
c = 2;
jc = true;
StartCoroutine(sixa());
}
}
private void OnTriggerExit(Collider other)
{
if (other.name == "fhf")
{
c = 1;
jc = false;
}//object1
else if (other.name == "tgf")//tong guan fang
{
c = 1;
jc = false;
}
else if (other.name == "cxs")
{
c = 1;
jc = false;
}
}
IEnumerator six()
{
yield return new WaitForSeconds(16);
c2 = false;
speed = 3;
}
IEnumerator sixa()
{
yield return new WaitForSeconds(0.1f);
jc = false;
}
}
第二个脚本:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class ai : MonoBehaviour
{
private void Update()
{
if (GetComponent<lsscript>().jc)
{
GetComponent<lsscript>().enabled = true;
}
else
{
GetComponent<lsscript>().enabled = false;
}
}
}
代码很简单,我就不多介绍了,这样就可以了。