[英]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:这可能发生的一些原因:
Wrap
function.Wrap
函数。 There's property where you can check if the simulated and actual position are in sync. 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.