Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by krides · Feb 26, 2012 at 12:02 AM · movementtransformarraycoroutinetranslate

Moving multiple transforms from an array in a single script

Here’s the problem. I’ve got an array of cubes that are generically spread across five layers. I want the layers 2 3 4 5 to move forward every time they see that the front layer is gone. My problem is, I cannot use coroutines when every cube is being moved on its own, so I have to try and move them in a bulk. And I don’t know how to do that.

See, the number of cubes and their position in the array can be arbitrary, but they have to move every time they see that there is a cube missing in front of them. Here’s the script which I use to detect missing cubes:

 function Movement()
 {
     for (var fi = 0; fi <50; fi++)
     {
         
         for (var wi = 0; wi < 50; wi++)
         {
             for (var zi = 1; zi < 50; zi++)
             {
                 if (createBricks.globalArray[wi,fi,zi] != null)
                 {
                     if((takeTurns.machineTurn)&&(!takeTurns.playerTurn)&&(createBricks.globalArray[wi,fi,zi].gameObject.transform.position.z != 0)&&(createBricks.globalArray[createBricks.globalArray[wi,fi,zi].gameObject.transform.position.x,createBricks.globalArray[wi,fi,zi].gameObject.transform.position.y,0] == null))
                     {
                         currentMovement = createBricks.globalArray[wi,fi,zi];
                     }
                 }
             }
         }
     }
     yield takeTurns.ReturnTurns();
 }

Never mind the yield part, it just lets the game proceed once all the cubes have been processed. This function scans through the 3D array globalArray and detects missing array entries. Then we end up with a currentMovement (which is a transform) for every iteration cycle that actually detects something.

I want to move ALL the currentMovement transforms that this script detects (preferably all at once), and I’ve managed to move only one of them. I tried writing currentMovement to an array and then processing array enties, but you can’t really use a for loop here, because movement has to be in Update, but perhaps something went wrong.

For reference here’s the script that I use for actual movement routine. Maybe you will be able to point to the parts that can be modified for multiple movements, as I could not come up with anything.

 function Execute()
 {
         if (currentMovement != null)
         {
             if(currentMovement.transform == createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,1])
             {
                 currentMovement.gameObject.transform.Translate(0, 0, -3 * Time.deltaTime);
 //                transform.position = Vector3.Lerp (this.transform.position, Vector3(this.transform.position.x, this.transform.position.y, 0), Time.deltaTime * smooth);
             
                 if (currentMovement.transform.position.z <= 0.01)
                 {
                     currentMovement.transform.position.z = 0;
                     currentMovement.gameObject.AddComponent("SingleCube");
                     currentMovement.gameObject.AddComponent("Detector");
                     createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,0] = currentMovement.transform;
                     currentMovement = null;
                     previousMoved = true;
                 }
             }
             
             else if(currentMovement.transform == createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,2])
             {
                 currentMovement.gameObject.transform.Translate(0, 0, -3 * Time.deltaTime);
 //                transform.position = Vector3.Lerp (this.transform.position, Vector3(this.transform.position.x, this.transform.position.y, 1), Time.deltaTime * smooth);
 
                 if (currentMovement.transform.position.z <= 1.01)
                 {
                     currentMovement.transform.position.z = 1;
                     createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,1] = currentMovement.transform;
                     currentMovement = null;
                     previousMoved = true;
 
                 }            
             }
             
             else if(currentMovement.transform == createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,3])
             {
                 currentMovement.gameObject.transform.Translate(0, 0, -3 * Time.deltaTime);
 //                transform.position = Vector3.Lerp (this.transform.position, Vector3(this.transform.position.x, this.transform.position.y, 2), Time.deltaTime * smooth);
             
                 if (currentMovement.transform.position.z <= 2.01)
                 {
                     currentMovement.transform.position.z = 2;
                     createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,2] = currentMovement.transform;
                     currentMovement = null;
                     previousMoved = true;
 
                 }
             }
             
             else if(currentMovement.transform == createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,4])
             {
                 currentMovement.gameObject.transform.Translate(0, 0, -3 * Time.deltaTime);
 //                transform.position = Vector3.Lerp (this.transform.position, Vector3(this.transform.position.x, this.transform.position.y, 3), Time.deltaTime * smooth);
             
                 if (currentMovement.transform.position.z <= 3.01)
                 {
                     currentMovement.transform.position.z = 3;
                     createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,3] = currentMovement.transform;
                     currentMovement = null;
                     previousMoved = true;
 
                 }
             }
             else
             {    
             Debug.LogError("Something went wrong in cube movenent");
             print(createBricks.globalArray[currentMovement.transform.position.x,currentMovement.transform.position.y,currentMovement.transform.position.z]);
             }
         }
 }

By answering this question you would be saving my ass, so thank you very much if you have read it to this point. I’m completely stuck and don’t know what to do next.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to have a GameObject follow a path with c#? 3 Answers

how do i slowly translate a object to a other objects position 2 Answers

Object keeps orbiting target location in a Coroutine 0 Answers

Move player left or right after x seconds 1 Answer

rigidbody.position not working inside coroutine 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges