首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >嘿,我在团结敌人的时候犯了个错误

嘿,我在团结敌人的时候犯了个错误
EN

Stack Overflow用户
提问于 2020-04-23 11:21:09
回答 1查看 510关注 0票数 1

在Unity3D中,我用一个简单的敌人ai脚本创建了一个僵尸字符,这样僵尸就会追上播放器,但是当我添加NavMeshAgent组件时,它声明了以下错误消息:

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

"SetDestination“。

我已经试过烘焙NavMesh,但它没有工作,请帮助我卡住了。这是我的密码:

代码语言:javascript
复制
 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);
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-26 11:18:14

问题解决了,我只需检查地面的静态和烘烤NavMeshAgent。希望它能帮上忙

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61386001

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档