Instanitiate PreFabs and let them move to different locations

HI,

and another Problem.

Right now i am trying to make an “Skill” for my warrior. When i hit a button, i want to create an Arrow Pre fab für each enemy that is on my world.

This works for me with

 foreach (GameObject go in GameManager.spawnEnemys)
        {

            GameObject disc = Instantiate(discPreFab);
            disc.transform.position = new Vector3(transform.position.x+10,transform.position.y+10,transform.position.z+10);

now comes the problem, how can i say my prefabs where they should move to? I want one Disc for each enemy.

Tryed

  void Update()
    {
        SetTargets();

        //while (i < targets.Length) // while seems not to work here 
        //{
        //    transform.position = Vector3.MoveTowards(transform.position, targets*.transform.position, 50);*

// i++;
//}

if(GameManager.spawnEnemys.Count > 0)
{
foreach (GameObject go in targets)
{
transform.position = Vector3.MoveTowards(transform.position, go.transform.position, 50);
//GameManager.spawnEnemys.Remove(go);
}
}

}

public GameObject[] targets;

public void SetTargets()
{

targets = GameManager.spawnEnemys.ToArray();

}
combination but like expected doenst work for me. Have many trouble with target setting, if anyone knows a good tutorial especialy for multitarget it would be great. But any Answer which i can test and try to understand would be great :slight_smile:
Greetz Smokki

I’m not sure whether that second script is attached to your character or to each disc object, but the easiest way to do this would be to assign each disc a target when you spawn it, and then let the disc travel towards its own target.
**
So you could make a script and attach it to your disc prefab. Also, 50 seems like a large distance to use in Vector3.MoveTowards, and is not frame independent. Did you mean 50 * Time.deltaTime?

public class discFlightBehavior : Monobehavior {
    public GameObject targetEnemy;

    void Update(){
        transform.position = Vector3.MoveTowards(transform.position, targetEnemy.transform.position, 50 * Time.deltaTime);
    }
}

And then when you spawn each disc:

foreach (GameObject go in GameManager.spawnEnemys) {
    GameObject disc = Instantiate (discPreFab);
    disc.GetComponent<discFlightBehavior>().targetEnemy = go;
    disc.transform.position = new Vector3 (transform.position.x + 10, transform.position.y + 10, transform.position.z + 10);
}