Enemy not moving correctly after initial scan

Hello! I am working on a FPS right now. The level, player and enemy are instantiated on start, before game start, the scene is empty. So, I set the enemy to follow me, and when I start the game, it only moves to the initial player position, and then it seems to want to follow me, but only like, wiggling in place in the tile its standing in. When I manually scan the grid graph again, while the game i running, it moves again, to the player position. But it shouldn’t be necessary to scan the grid more than once, if the level does not change, should it?
This is my code:

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

public class EnemyBehaviour : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{

}

// Update is called once per frame
void Update()
{
    SetDestination();
   /* var seeker = GetComponent<Seeker>();
    GameObject player = GameObject.Find("CorpumGuy(Clone)");
    seeker.StartPath(transform.position, player.transform.position, OnPathComplete);
   */
}
public void OnPathComplete(Path p)
{
    // We got our path back
    if (p.error)
    {
        // Nooo, a valid path couldn't be found
    }
    else
    {
        // Yay, now we can get a Vector3 representation of the path
        // from p.vectorPath
    }
}
private void SetDestination()
{
    GameObject player = GameObject.Find("CorpumGuy(Clone)");
    if (player != null)
    {
        AIDestinationSetter destinationSetter = GetComponent<AIDestinationSetter>();
        if (destinationSetter != null)
        {
            destinationSetter.target = player.transform;
            Debug.Log(destinationSetter.target.transform.ToString());
        }
        else
        {
            Debug.LogError("No AIDestinationSetter found on this enemy!");
        }
    }
    else
    {
        Debug.LogError("No Player object found in the scene!");
    }
}

}

Wether I use the seeker, or the AIdestinationSetter component, the result is exactly the same.

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

public class RoomGen : MonoBehaviour
{
public GameObject[,] Array1 = new GameObject[5,5];
public GameObject[] Room;
public GameObject spawnPoint;
public GameObject player;
public float spacing;
public GameObject ai;

// Start is called before the first frame update
void Start()
{
    for (int i = 0; i < Array1.GetLength(0); i++)
    {
        for (int j = 0; j < Array1.GetLength(1); j++)
        {
            if (i == Array1.GetLength(0) / 2 && j == Array1.GetLength(1) / 2)
            {
                Array1[i, j] = Instantiate(Room[0]);
                Array1[i, j].transform.parent = spawnPoint.transform;
                Array1[i, j].transform.localPosition = spawnPoint.transform.localPosition + new Vector3(i, 0, j) * spacing;
                Array1[i, j].transform.Translate(65, 0, 0);
                Array1[i, j].transform.rotation = Quaternion.identity;
                Array1[i, j].name = "SpawnRoom";
               // Array1[i, j].
                player.transform.position = Array1[i, j].transform.localPosition + new Vector3(-2.5f, 7, 52.5f);
                player.transform.rotation = Quaternion.identity;
                player.name = "CorpumGuy";
                Instantiate(player);
                ai.transform.position = Array1[i, j].transform.localPosition + new Vector3(-7.5f, 7, 47.5f);
                ai.transform.rotation = Quaternion.identity;
                ai.name = "AI";
                Instantiate(ai);
                
            }
            else
            {
                Array1[i, j] = Instantiate(Room[Random.Range(1, Room.Length)]);
                Array1[i, j].transform.parent = spawnPoint.transform;
                Array1[i, j].transform.localPosition = spawnPoint.transform.localPosition + new Vector3(i, 0, j) * spacing;
                Array1[i, j].transform.Translate(65, 0, 0);
                Array1[i, j].transform.rotation = Quaternion.identity;
                Array1[i, j].name = "Room_" + i + j;
            }
        }
    }
   AstarPath.active.Scan();
}

// Update is called once per frame
void Update()
{

}

}

The Astar prefab is part of the spawn room.

Does the graph look the same after you scan it the second time?

Also make sure the agent itself is excluded from the scan. If it was included in the scan, the graph could have generated a small navmesh piece on top of the agent, which it then think it is stuck inside.

Yeah, the graph looks exactly the same. Looks like the transform component from the player, that the AIDestinationSetter script has, does not get updated.
You are right, there are small navmesh pieces on top of the player and the enemy. I can switch the collider of the enemy off, so it doesn’t get a piece. But I am going to need the collider later. Can you tell me a better way to exclude it from the scan?
Now, the enemy moves to the navmesh tile above the player’s head, and then it stays there (the navmesh tile doesn’t move along with the player, of course).

I made a scan of the room that I spawn in in its prefab state. And enemy + player are already in the scene. But they have tiles above their heads anyways, so I assume, another scan is initiated, when the game starts?
So, I need to exclude the player and all enemies I’m going to spawn from the scan and all upcoming scans. But I do not know how to do that.

Hi

You can put the characters in a separate layer, and then exclude that layer in the graph settings.

Thank you very much, that did the trick :wink:

1 Like

Hello! I hav another issue. Now I spawned an actual enemy with mesh, rig and animations instead of a dummy. I noticed, that he is always turned sideways, and while following me, he always turns away. In the AIPath component the Orientation is set to Z-Axis forward. I tried rotating the enemy in the scene, but it didn’t do anything. Then, i tried rotating it in Blender, before exporting the .fbx, but it didn’t change anything either. I’m a little lost ;(

Hi

You’ll need to ensure that the +Z axis of the mesh is the forwards direction of the agent. You can either rotate a child object in unity, or change the mesh as it is exported from blender.

We made it work, i was just using the wrong animation file :wink: thank you for your effort, and sorry for asking such basic questions :sweat_smile:

1 Like