Hi - I have 3 groups of buildings, and each group contains several buildings as separate gameObjects. Each building has a child object called Nav Meshes which I am using to contain and organize objects for npc character navigation, which includes an empty gameObject called navTarget_entry which is a target for nav agents. I want to instantiate and place a “worker” prefab in front of each building at the navTarget_entry gameObject, but cannot get them to relocate to the correct position and suspect it has something to do with changing the global position or the level of the child object. A few of the workers relocate correctly, some are right next to the spot where they should be, and others are half-way across the map. Ugh. Any idea what’s on?
// 1. instantiate a worker prefab ---------------------------
GameObject worker = Instantiate (workerPrefab) as GameObject;
// 2. assign the worker prefab to a building -----------
foreach (Transform t in building.gameObject.transform)
{
if (t.name == "Workers")
{
worker.transform.SetParent (t.transform);
}
}
// 3. relocate the worker prefab ---------------------------
Transform mapPosition = building.navTarget_entry;
workerData.gameObject.transform.position = new Vector3 (mapPosition.position.x, mapPosition.position.y, mapPosition.position.z);
Or you can Destroy the object from point A, and instantiate the same object at point B with the exact same attributes, except its transform position. But if you want the worker to actually move there you need to use a function called Translate.
SOLVED - the issue was caused by “Teleporting Nav Mesh Agents”, which might be the coolest sounding error I’ve encountered so far. Each of my worker prefabs has a NavMeshAgent component. Relocating the worker prefab after it was instantiated was causing the agent to “teleport” to a position on the map that was different from the position specified in the code. The intended location itself was on the very edge of the nav mesh (ie: not set sufficiently within the nav mesh to account for the agent’s radius), which may also have contributed to the issue.
.
Solution - after instantiating the worker prefab, I turned off the NavMeshAgent, then relocated the worker prefab, then reactivated the NavMeshAgent. I also relocated the navTarget_entry gameObjects (the new location for the worker prefab) to make sure it was well within an existing nav mesh.
// 1. instantiate a worker prefab ---------------------------
GameObject worker = Instantiate (workerPrefab) as GameObject;
// 2. assign the worker prefab to a building -----------
foreach (Transform t in building.gameObject.transform)
{
if (t.name == "Workers")
{
worker.transform.SetParent (t.transform);
}
}
// 3. relocate the worker prefab ---------------------------
NavMeshAgent navAgent = worker.GetComponent<NavMeshAgent>();
if (navAgent != null) navAgent.enabled = false;
Transform mapPosition = building.navTarget_entry;
workerData.gameObject.transform.position = new Vector3 (mapPosition.position.x, mapPosition.position.y, mapPosition.position.z);
navAgent.enabled = true;
// note: make sure new location is well within an existing nav mesh area