Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

How to transform.translate array of GameObjects at the same time and speed?

Discussion in 'Scripting' started by Shoopen, Apr 30, 2015.

  1. Shoopen

    Shoopen

    Joined:
    Mar 14, 2015
    Posts:
    7
    I can't do a loop in update() function because it won't looks like I want. Here is the video:


    And here is the code:
    Code (CSharp):
    1. {
    2.      public GameObject[] Skiny = new GameObject[15];
    3.      public float PozycjaPoczatkowa = -1.1f;
    4.      public GameObject[] WylosowaneSkiny = new GameObject[40];
    5.      void Start ()
    6.      {
    7.          UstawPozycjePoczatkowe ();
    8.          for(int i = 0; i < Skiny.Length; i++)
    9.          {
    10.              Destroy(Skiny[i].gameObject);
    11.          }
    12. //IT DOESN'T WORK!!!
    13.          for (int i = 0; i < 39; i++) {
    14.              WylosowaneSkiny [i].transform.Translate (Vector2.right * Time.deltaTime * -i, Space.Self);
    15.          }
    16.      }
    17.      void Update ()
    18.      {
    19.      }
    20.      void UstawPozycjePoczatkowe()
    21.      {
    22.          int licznik = 0;
    23.          do {
    24.              Vector2 pozycja = new Vector2 (PozycjaPoczatkowa, 0);
    25.              WylosowaneSkiny[licznik] = Instantiate(Skiny[Random.Range(0,Skiny.Length)], pozycja , Quaternion.identity) as GameObject;
    26.              PozycjaPoczatkowa += 1.1f;
    27.              licznik++;
    28.          } while(licznik <=39);
    29.      }
    30. }
     
  2. Munchy2007

    Munchy2007

    Joined:
    Jun 16, 2013
    Posts:
    1,732
    Have you tried parenting your objects to a single game object and then moving the parent object instead?
     
    comando likes this.
  3. Shoopen

    Shoopen

    Joined:
    Mar 14, 2015
    Posts:
    7
    I found solution after long searching in google.
    Code (CSharp):
    1.     void przesun(GameObject[] objects)
    2.     {
    3.         foreach(var obj in objects)
    4.         {
    5.             obj.transform.Translate(Vector2.right * Time.deltaTime * predkosc *-1, Space.Self);
    6.         }
    7.     }
    Code (CSharp):
    1.     void Update ()
    2.     {
    3.         przesun(WylosowaneSkiny);
    4.     }
    But I don't know what foreach means. However It's working!
    Now I want to set the speed to slow down and fully stop. How can I do that?
     
  4. comando

    comando

    Joined:
    Nov 5, 2018
    Posts:
    2
    This save me a lot of Time.Thanks a lot
     
    Munchy2007 likes this.