简体   繁体   English

Unity c# - 无法在导航网格上生成预制件

[英]Unity c# - unable to Spawn Prefabs on a NavMesh

I am trying to make a zombie wave game and current have a Prefab for my enemies.我正在尝试制作僵尸波游戏,并且当前为我的敌人准备了一个预制件。 If I have the prefab be in the scene when I hit run, they are attached to the NavMesh and track the player perfectly.如果我在运行时让预制件在场景中,它们会附加到 NavMesh 并完美地跟踪玩家。 I want to achieve this but with the enemy being spawned from an empty GameObject so I can get the waves spawning in. I have achieved them Spawning but they have the error,我想实现这一点,但是敌人是从一个空的游戏对象中产生的,所以我可以让波浪产生。我已经实现了它们的产生,但它们有错误,

"SetDestination" can only be called on an active agent that has been placed on a NavMesh.
UnityEngine.AI.NavMeshAgent:SetDestination(Vector3)
EnemyAI:Update() (at Assets/Scripts/EnemyAI.cs:25)

Here is my EnemyAI Script这是我的 EnemyAI 脚本

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

public class EnemyAI : MonoBehaviour
{
    public float lookRadius = 10f;

    Transform target;
    NavMeshAgent agent;
    public GameObject Player;
    
    void Start() 
    {
        agent = GetComponent<NavMeshAgent>();
    }
    // Update is called once per frame
    void Update()
    {
        float distance = Vector3.Distance(Player.transform.position, transform.position);
        
        if (distance <= lookRadius)
        {
            agent.SetDestination(Player.transform.position);
        }
    }
}

And my spawning script, which is attached to an empty game object,还有我的生成脚本,它附加到一个空的游戏对象上,

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Spawning : MonoBehaviour
{
    public GameObject prefab;
    public int CountofCubes;
    private IEnumerator coroutine;
    public float spawnRate;
    
    IEnumerator Start()
    {
        while (true)
        {
            for (int i = 0; i < CountofCubes; i++)
            {
                    Instantiate(prefab, new Vector3(Random.Range(-25.0f, 25.0f), 0.5f, Random.Range(-25.0f, 25.0f)), Quaternion.identity);
            }
            yield return new WaitForSeconds(spawnRate);
        }
    }   
} 

我正在生成的敌人预制件

Any help would be great thanks!任何帮助都会非常感谢!

I had the same issue and I don't have an explanation but only a workaround:我有同样的问题,我没有解释,只有一个解决方法:

In the Start fucntion, I added:在开始功能中,我添加了:

navAgent = GetComponent<NavMeshAgent>();
navAgent.enabled = false;

// Invoke is used as a workaround for enabling NavMeshAgent on NavMeshSurface
Invoke("EnableNavMeshAgent", 0.025f);

And the EnableNavMeshAgent function is just :而 EnableNavMeshAgent 功能只是:

private void EnableNavMeshAgent ()
{
    navAgent.enabled = true;
}

If I set the invoke delay to a value less than 0.025 second the error keep going but for 0.025 I only have it twice and the behaviour is the one I wanted after that.如果我将调用延迟设置为小于 0.025 秒的值,错误会继续存在,但对于 0.025,我只有两次,并且行为是我之后想要的。

Some reasons this might happen:这可能发生的一些原因:

  1. The agent is not on level with any navmesh ie can be too far above or below.代理不在任何导航网格的水平上,即可能高于或低于太多。 You want it to be "close to the NavMesh surface".您希望它“靠近 NavMesh 表面”。 Use raycasting on the floor to position the agent or add a rigidbody and drop it from above the floor.在地板上使用光线投射来定位代理或添加一个刚体并将其从地板上方放下。 After you do this you might need to disable and enable the agent.执行此操作后,您可能需要禁用和启用代理。
  2. Moving the transform yourself rather than using Wrap function.自己移动变换而不是使用Wrap函数。 There's property where you can check if the simulated and actual position are in sync.您可以通过属性检查模拟位置和实际位置是否同步。
  3. Corrupted navmesh so you might need to re-bake it.导航网格损坏,因此您可能需要重新烘焙它。

It is essentially trying to tell you your agent is not on the mesh so it cannot find a path.它本质上是试图告诉您您的代理不在网格上,因此无法找到路径。 Try playing with where you're placing the agent.尝试使用您放置代理的位置。

I kind of remember running into a similar problem a while back and problem was I forgot to bake the NavMesh in Window > AI > Navigation .我记得不久前遇到了类似的问题,问题是我忘记在Window > AI > Navigation烘焙 NavMesh。 Just in case.以防万一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM