如何解决“ SetDestination”问题只能在已放置在NavMesh上的活动代理上调用。错误

时间:2019-05-05 12:47:50

标签: c# unity3d

我遇到此错误:

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

我的敌人控制器看起来像这样:

using UnityEngine;
using UnityEngine.AI;

[RequireComponent(typeof(NavMeshAgent))]
public class EnemyController : MonoBehaviour
{
    public float lookRadius = 10f;

    GameObject obj;
    NavMeshAgent agent;
    Transform target;

    // Start is called before the first frame update
    void Start()
    {
        obj = GameObject.FindGameObjectWithTag("Player");
        target = obj.transform;
        agent = GetComponent<NavMeshAgent>();
    }

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

        if(distance <= lookRadius)
        {
            agent.SetDestination(target.position);
        }
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, lookRadius);
    }
}

我正在使用标准fpscontroller作为播放器。

有没有办法使这项工作成功?

亲切的问候。

/鲁迪

0 个答案:

没有答案