[SOLVED]Unity C# Instantiate multiple prefabs next to each other and move them from point1 to point2

I'm trying to instantiate 5 prefabs (centered, next to each other) at a time at GameObject1 and move them from there to a GameObject2 (for that I get the GameObjects coordinates with transform.position). I tried to rotate the first GameObject to the 2nd one and then I instantiated (transform.right) from that and the problem is, it only works when GameObject1 is horizontal aligned with the GameObject2/the destination, not when it's vertical.

So basically the code for the instantiating-positions of the prefabs doesn't use the right coordinate system, I think. :smile:

Can anyone help me with that?
- instantiating from GameObject and moving them from there (next to each other), no matter where the destination is.

here the 2 snippets I use right now (1st for moving the prefabs and 2nd for instantiating them next to each other at GO1)

transform.right = destination - transform.position;

for (var i = 0; i < troopCountPerSpawn; i++)
            {
                GameObject Troop = Instantiate(troopPrefab, transform.position + new Vector3(0, i * .1f, 0), transform.rotation) as GameObject;
                }

You can use transform.right * (i * .1f) instead of new Vector3(0, i * .1f, 0) to align the instantiated objects relative to the transform's rotation.

Vector3 right = transform.right;

for(var i = 0; i < troopCountPerSpawn; i++)
{
    float offset = i * 0.1f;
    Vector3 position = transform.position + right * offset;
    var troop = Instantiate(troopPrefab, position, transform.rotation);
}
1 Like

[quote=“SisusCo”, post:2, topic: 866630]
You can use transform.right * (i * .1f) instead of new Vector3(0, i * .1f, 0) to align the instantiated objects relative to the transform’s rotation.

Vector3 right = transform.right;

for(var i = 0; i < troopCountPerSpawn; i++)
{
    float offset = i * 0.1f;
    Vector3 position = transform.position + right * offset;
    var troop = Instantiate(troopPrefab, position, transform.rotation);
}

[/quote]

Well, this works perfect for sending out prefabs in a line after each other, but I need them to be instantiate at the same time next to each other (I’m sure this is because of missing information in my question)…

I’ll upload a picture with what I mean.

Red Boxes are GameObjects
The 5 dots are my prefabs and they need to be instantiated next to each other, not after another.
And it needs to work, no matter how my GameObject is rotated.
7767849--979086--Unity instantiate Prefab help.png

[quote=“SisusCo”, post:2, topic: 866630]
You can use transform.right * (i * .1f) instead of new Vector3(0, i * .1f, 0) to align the instantiated objects relative to the transform’s rotation.

Vector3 right = transform.right;

for(var i = 0; i < troopCountPerSpawn; i++)
{
    float offset = i * 0.1f;
    Vector3 position = transform.position + right * offset;
    var troop = Instantiate(troopPrefab, position, transform.rotation);
}

[/quote]

I just changed the
Vector3 right = transform.right;
to
Vector3 right = transform.up;, it seems to work now. :hushed:

1 Like

Glad you figured it out :)

1 Like