What's the solution for this?

I’m making my new FPS game (This is just grey boxing) and I observe a strange behavior in nav mesh and nav mesh obstacle component.
In new versions we have option of carve in nav mesh obstacle, I enable it and hit play and enemy spawn at origin but then suddenly moved in front of my obstacle wall.
Then I disable carve it works perfectly.
Even while enabling carve, If I just move wall little down side it works perfectly.
Is it unity bug or just I’m fu***d up with this things!!
See full video you’ll understand better what I’m saying, and if anyone knows why this then please explain.

Here’s video of what’s happening.

Could it be because “EnemySpawner” object is inside the obstacle? Does moving the parent spawner object little up changes anything?

Parent spawner doesn’t moves! Also spawned capsules are not parent of any objects.

I tried few things and might find the solution.

I enable it and hit play and enemy spawn at origin but then suddenly moved in front of my obstacle wall.

What I understand from here is, you spawn enemies at the position of Enemy Spawner or directly at (0,0,0), then move them to Spawner Points. If this is correct, what happens in my opinion is that you spawn enemies outside the navmesh, then immediately try to “teleport” them by modifying their transform.position. The NavMeshAgent component tries to locate itself in the NavMesh surface and causing problems.

Here is how it looks on my screen

So I put a simple code here to the red object:

private void Update()
{
    if (Input.GetKeyDown(KeyCode.F))
    {
        transform.position += Vector3.forward * distance;
    }
}

as it can be seen in the video, this result in unexpected behaviour if the object is very close to the edge of the obstacle (I don’t know the exact reason though). On the other hand, if object is a little bit far away from the edge of the obstacle, the code works without problem. I think this is the reason of unintended behaviour of your spawner.

My solution is this:

[SerializeField] private float distance;
private NavMeshAgent agent;

private void Awake()
{
    agent = GetComponent<NavMeshAgent>();
}

private void Update()
{
    if (Input.GetKeyDown(KeyCode.F))
    {
        agent.gameObject.SetActive(false);
        transform.position += Vector3.forward * distance;
        agent.gameObject.SetActive(true);
    }
}

here, just before modifying transform.position, I disable NavMeshAgent component then enable it, thus, preventing my script and NavMeshAgent to modify transform of the object at the same time. I think this will solve your problem as well. Just disable NavMeshAgent on the newly spawned object, then enable when the object is in correct place

this is the end result

You can see, even though I “teleport” the object inside the obstacle, NavMeshAgent doesn’t try to change it’s position since its inactive. As soon as it is enabled, it moves the object to the closest position on the surface, probably using something similar to NavMesh.SamplePosition

I had the same issue. It happens because the NavMeshAgent is active while the object is being spawned or teleported near a carved obstacle, so the agent snaps itself to the nearest valid NavMesh point. Disabling the NavMeshAgent before setting the spawn position and enabling it afterward fixed it for me. After that the carving works as expected.

Thanku for your time, but I’m not controlling those capsule. They walk by agent.SetDestination(some valid position);