- Home /
How to Create a Unique Waypoint List for Each Prefab Unit ??
I wrote this C# script that creates a List of Vector3's for movement...My NavMeshAgent goes to the first location then the second and then the third and so on. As my agent gets there, it erases each waypoint (no longer needed) and when it gets to the last point, there is nothing left of the list...a simple dynamic list of vector3s. I can add more any time...as many as I wish.
It works fine. HOWEVER...
I was thinking that once I added this script to each of my prefabs, I would be able to provide each prefab with it's own unique list of waypoints so each of my prefab/navmeshagents could be controlled separately.
But, that's not how it works. ALL my nav mesh agents go to the same exact waypoints. I've been pulling my hair out trying to figure what to add to allow me to give separate waypoints to each prefab unit.
Any help to get my prefabs moving independently would be appreciated.
Thanks.
public class TestNav1 : MonoBehaviour
{
Collider sc;
private bool isSelected = false;
public NavMeshAgent agent;
private int wayPoint = 0;
public List<Vector3> moveToHere = new List <Vector3> ();
void Update ()
{
////////////////////////////////////////////////// Activates Unit ///////////////////////////////////////////////////////////////////////////////////////////
if (Input.GetMouseButtonDown (0))
{
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast (ray, out hit))
{
Collider sc = hit.collider;
if (sc.gameObject.tag == "Ally" || sc.gameObject.tag == "Supply")
{
GameObject[] tempGo; ////////////////////////////////////////////////////// Next lines through the foreach statement changes color of nonselected items back to blue.
tempGo = GameObject.FindGameObjectsWithTag ("Ally");
foreach (GameObject Ally in tempGo)
{
gameObject.GetComponent<Renderer> ().material.color = Color.blue;
isSelected = false;
}
sc.gameObject.GetComponent<Renderer> ().material.color = Color.cyan;
agent = sc.GetComponent<NavMeshAgent> ();
isSelected = true;
}
}
} ////////// End Unit Selection/activation Logic ////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////// Begin Area where you "Click" to add way points. ////////////////////////////////////////////////////////
if (Input.GetKey(KeyCode.LeftShift) && Input.GetMouseButtonDown(1) && isSelected == true )
{
RaycastHit hit;
if (Physics.Raycast (Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Mathf.Infinity))
{
moveToHere.Add(hit.point);
}
}//////////////////////////////////////////////////////////////////// End Click to add waypoint area //////////////////////////////////////////////////////////////////
if(moveToHere.Count>0) agent.destination = moveToHere [0];
if (moveToHere.Count>0 && !agent.pathPending && agent.remainingDistance < 0.5f) moveToHere.RemoveAt (0);
}
}
I'm still working on this...hoping to find a way to create a unique list for each prefab.
I haven't figured out how to do this. I can't do much until tomorrow but I'll be back on then and try to research some more Internet information.
Answer by Topthink · Sep 14, 2017 at 10:32 PM
I think I finally figured this out through trial and error...I had been doing a few things wrong and I think I finally got it straightened out.
Answer by hexagonius · Aug 27, 2017 at 06:33 AM
It depends on how you place them in the world. If the prefabs are created during edit mode you can just edit each prefab individually and that's it (just don't apply the changes).
If you instantiate them, the instantiator needs to set the waypoints.
Thank you for your help.
The prefabs are spawned (instantiated) occasionally over a period of time. I don't want the waypoints to be assigned until AFTER they are created. Once the prefabs are created, I want to see what is going on in the world (on the terrain map) and then manually send them to different places...I might want to send them to a half dozen different places (thus the dynamic list).
But, I want to send each prefab to a different set of waypoints...and that is the problem. Right now, they all want to go to the same places.
Right now, I'm trying to fully interpret your response and I'll continue to look for a resolution. I do appreciate the time you took to help me.
I've been looking at equipment lists (and so forth) that some people use for RPG games. I suppose you can make a comparison to something like that because each "character" needs a different set of equipment.
Anyway, I'm looking at different models that designers use to see if there is something there that I can use.
Again, thanks.
Your answer
Follow this Question
Related Questions
A node in a childnode? 1 Answer
Increase size of obstacle - navmesh 1 Answer
NavMeshPath.corners.length is always 0 1 Answer
Removing 100ms Delay in NavMeshAgent moving to destination? 1 Answer
How to access/print parts of a list 2 Answers