Search Unity

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

Discussion in 'Scripting' started by LeonhardtNils, Dec 29, 2021.

  1. LeonhardtNils

    LeonhardtNils

    Joined:
    Nov 2, 2021
    Posts:
    7
    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. :D

    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;


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

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    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.

    Code (CSharp):
    1. Vector3 right = transform.right;
    2.  
    3. for(var i = 0; i < troopCountPerSpawn; i++)
    4. {
    5.     float offset = i * 0.1f;
    6.     Vector3 position = transform.position + right * offset;
    7.     var troop = Instantiate(troopPrefab, position, transform.rotation);
    8. }
     
    LeonhardtNils likes this.
  3. LeonhardtNils

    LeonhardtNils

    Joined:
    Nov 2, 2021
    Posts:
    7
    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.
    Unity instantiate Prefab help.png
     
  4. LeonhardtNils

    LeonhardtNils

    Joined:
    Nov 2, 2021
    Posts:
    7
    I just changed the
    Vector3 right = transform.right;
    to
    Vector3 right = transform.up;, it seems to work now. :eek:
     
    SisusCo likes this.
  5. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,331
    Glad you figured it out :)
     
    LeonhardtNils likes this.