- Home /
How do I spawn unique Waypoints for my AI?
Greetings,
I'm currently working on a prototype for a Petri dish-like simulator. In it, the player, a microbe, moves around and collides with other Microbes to spawn yet more Microbes. These are all of different colours, with the new Microbes spawning as a colour between the two "parents".
My question is, how do I have the newly Instantiated Microbes interact with their own Waypoints? I can individually plug each Microbe with a Waypoint in the Editor, but when I launch, and new Microbes spawn, they either go towards another Microbe's Waypoint, or they do not move (depending on the various things I have tried).
My code looks like this:
(MOVEMENT FOR THE MICROBES)
public GameObject Waypoint;
void Update ()
{
transform.LookAt(Waypoint.transform.position);
if(Vector3.Distance (transform.position, Waypoint.transform.position) >= 2)
{
transform.Translate (Vector3.forward * 2 * Time.smoothDeltaTime);
}
if(Vector3.Distance (transform.position, Waypoint.transform.position) <= 2)
{
Waypoint.transform.position = new Vector3(Random.Range(-10F, 10F), 0.75F, Random.Range(-10F, 10F));
}
}
And the Instantiate Script on the Player is (this is an example of a Blue player hitting a Yellow Microbe, and creating a Green Microbe):
void OnCollisionEnter(Collision Microbe)
{
if(Microbe.gameObject.tag == "Yellow")
{
Instantiate (MicrobeGreen, new Vector3(Random.Range(transform.position.x - 2.5F, transform.position.x + 2.5F), 0.75F, Random.Range(transform.position.z - 2.5F, transform.position.z + 2.5F)), Quaternion.identity);
}
}
Could you please help me out? I can't figure this out, and I've been working on it for a while now :/
Thanks!
Answer by fafase · Sep 05, 2012 at 09:06 AM
I think your issue can be fixed with function Start(). You would search and find the waypoint you want your object to go to. The Start function is called when the object is instantiated.
If you want your guy to go to waypoint2 and waypoint4 and another to go to waypoint3 only:
var waypoint:GameObject[]=new GameObject[2];
function Start(){
waypoint[0] = GameObject.Find("Waypoint2");
waypoint[1] = GameObject.Find("Waypoint4");
}
var waypoint:GameObject;;
function Start(){
waypoint = GameObject.Find("Waypoint3");
}
excellent answer, yes I believe conceptually what he is looking for is the "Start()" function, he could learn about Awake() as well
Your answer
![](https://koobas.hobune.stream/wayback/20220613080641im_/https://answers.unity.com/themes/thub/images/avi.jpg)