0

我已经尝试摆脱这个 NavMeshAgent 错误大约 2 周了,但似乎无法克服它,所以我转向 SO。

我有一个导航网格,我已经大量生成了放置在导航网格上的敌人,但是我不断收到这个错误,直到我的控制台充满了这个错误(999+)。错误是:

“SetDestination”只能在已放置在 NavMesh 上的活动代理上调用。UnityEngine.AI.NavMeshAgent:SetDestination(Vector3) ZombieAI:Update() (在 Assets/Scripts/ZombieAI.cs:137)

任何有关消除此错误的帮助将不胜感激,因为它会从我的程序中吸收所有性能到无法使用的地步。

脚本如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using FSG.MeshAnimator;


public class ZombieAI : MonoBehaviour
{
    /// <summary>
    /// Components
    /// </summary>
    MeshAnimator mesh;
    AudioSource audioSrc;
    SphereCollider sphereCollider;
    CapsuleCollider capsuleCollider;
    NavMeshAgent agent;

    /// <summary>
    /// Child Objects
    /// </summary>
    GameObject EnemyIcon;


    /// <summary>
    /// Animations
    /// </summary>
    public MeshAnimation[] deathAnimations = new MeshAnimation[5];
    string curDeathAnimations;
    public MeshAnimation[] runAnimations = new MeshAnimation[1];

    /// <summary>
    /// Sounds
    /// </summary>
    public AudioSource _as;
    public AudioClip[] infectedNoises;
    public int randomNoiseNum;


    /// <summary>
    /// Textures
    /// </summary>
    public Texture[] mantexture = new Texture[16];
    public Texture[] womantexture = new Texture[17];


    /// <summary>
    /// Health
    /// </summary>
    public float max_health = 5f;
    public float cur_health = 5f;
    public float distance;

    /// <summary>
    /// Awareness
    /// </summary>
    public bool female;
    public bool alive;
    public bool spotted = false;
    public bool attack = false;
    bool create = false;

    /// <summary>
    /// Targets
    /// </summary>
    public GameObject target;

    /// <summary>
    /// Veriables
    /// </summary>
    public float velocity;
    private Vector3 previous;
    [Range(0f, 1.35f)]
    public float attackRange;
    [Range(3f, 5f)]
    public float runAttackRange;
    [Range(1f, 100f)]
    public float detectionRange;
    [Range(1f, 10f)]
    public float runSpeed = 3.5f;
    float onMeshThreshold = 3;

    private void Start()
    {
        if (female == false)
        {
            gameObject.GetComponent<Renderer>().material.SetTexture("_MainTex", mantexture[Random.Range(0, mantexture.Length)]);
        }
        if (female == true)
        {
            GetComponent<Renderer>().material.SetTexture("_MainTex", womantexture[Random.Range(0, womantexture.Length)]);
        }


        curDeathAnimations = deathAnimations[Random.Range(0, deathAnimations.Length)].name;
        //EnemyIcon = transform.Find("Enemy-Icon").gameObject;
        mesh = GetComponent<MeshAnimator>();
        audioSrc = GetComponent<AudioSource>();
        _as = GetComponent<AudioSource>();
        sphereCollider = GetComponent<SphereCollider>();
        capsuleCollider = GetComponent<CapsuleCollider>();
        agent = GetComponent<NavMeshAgent>();
        target = GameObject.FindGameObjectWithTag("Player");

        alive = true;


        if (transform.position.y <= 0 && transform.position.y >= 1)
        {
            Destroy(gameObject);
        }


    }

    public void Update()
    {

        if (alive)
        {

            distance = Vector3.Distance(target.transform.position, transform.position);
            velocity = ((transform.position - previous).magnitude) / Time.deltaTime;
            previous = transform.position;



            if (transform.position.y <= 1 && transform.position.y >= -1)
            {
                create = true;
                if (create == false)
                {
                    Destroy(gameObject);
                }
                else if (create == true)
                {
                    agent.SetDestination(target.transform.position);
                }

            }


            if (velocity > 0 && !attack)
            {
                mesh.speed = velocity / 3;
                mesh.Play("Run1-0");
            }

            if (velocity > 0 && attack && spotted)
            {
                spotted = true;
                mesh.Play("Run Attack");
            }
            else if (velocity == 0 && attack && spotted)
            {
                mesh.Play("Attack1-0");
            }

            if (distance < detectionRange)
            {
                spotted = true;
            }

            if (spotted && distance < attackRange)
            {
                Vector3 targetPosition = new Vector3(target.transform.position.x, transform.position.y, target.transform.position.z);
                transform.LookAt(targetPosition);
            }

                        randomNoiseNum = Random.Range(0, 400);
            if (alive && randomNoiseNum == 20)
            {
                PlayRandomNoise();
            }

        }

       /* if (alive && distance > attackRange && velocity <= 0.05f && !attack)
        {
            mesh.Play("Idle1-0");
        }*/

            if (!alive)
            {
            //mesh.Play(curDeathAnimations);
            //ScoreManager.scoreValue += 1;
            Destroy(gameObject, 5);
            }

            if (cur_health <= 0)
            {
            mesh.speed = 1;
                alive = !alive;
                mesh.Play(curDeathAnimations);
                //Destroy(mesh,0);
                Destroy(EnemyIcon);
                Destroy(audioSrc, 0);
                Destroy(sphereCollider, 0);
                Destroy(capsuleCollider, 0);
                Destroy(agent, 0);

                alive = false;

            }


    }

    void OnTriggerStay(Collider col)
    {
        attack = true;
    }

    void OnTriggerExit(Collider col)
    {
        attack = false;
    }

    public void PlayRandomNoise()
    {
        if (female == true)
        {
            _as.volume = 2.0f;
            _as.clip = infectedNoises[Random.Range(0, infectedNoises.Length)];
            _as.pitch = Random.Range(1.10f, 1.35f);
            _as.PlayOneShot(_as.clip);
        }
        else
        {
            _as.volume = 2.0f;
            _as.pitch = Random.Range(0.75f, 1.05f);
            _as.clip = infectedNoises[Random.Range(0, infectedNoises.Length)];
            _as.PlayOneShot(_as.clip);
        }

    }


}
4

0 回答 0