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.