嘿,我在使敌人成为一体时出错了

问题描述 投票:-1回答:1

[在Unity3D中,我用简单的敌人ai脚本创建了一个僵尸角色,以便僵尸追赶玩家,但是当我添加NavMeshAgent组件时,它声明了以下错误消息:

“ SetDestination”只能在已放置在NavMesh.UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)]上的活动代理上调用

我已经尝试过烘焙NavMesh,但是它没有用,请帮助我。这是我的代码:

 using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UIElements;

public class EnemyAI : MonoBehaviour
{
    public float lookRadius = 20f;
    Transform target;
    NavMeshAgent agent;

    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        target = PlayerManager.instance.player.transform;

    }

    // Update is called once per frame
    void Update()
    {
        float distance = Vector3.Distance(target.position, transform.position);

        if (distance <= lookRadius)
        {
            agent.SetDestination(target.position);
        }
        if (distance <= agent.stoppingDistance)
        {
            FaceTarget();
        }
        void FaceTarget()
        {
            Vector3 direction = (target.position - transform.position).normalized;
            Quaternion lookRotation = Quaternion.LookRotation(new Vector3(direction.x, 0, direction.z));
            transform.rotation = Quaternion.Slerp(transform.rotation, lookRotation, Time.deltaTime * 5f);
        }
    }
    public void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, lookRadius);
    }
}

[在Unity3D中,我用简单的敌人ai脚本创建了一个僵尸角色,因此僵尸将追赶玩家,但是当我添加NavMeshAgent组件时,它声明了以下错误消息:“ ...

c# unity3d
1个回答
0
投票
问题解决了,我只需要检查地面是否静止并烘烤NavMeshAgent。希望对您有所帮助
© www.soinside.com 2019 - 2024. All rights reserved.