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 BrunoTavares · Nov 17, 2014 at 09:20 AM · transformtransform.positioncubemoving

How to move several objects "y" units during a fixed time

I have a selection of transforms that I need to move a certain amount of units up during a fixed time. This is my corotine.

 float timer = 0f;
     while (timer < riseTime) {
         foreach (Transform trans in sel.list) {
             pos = trans.position;
             pos.y = pos.y + (sel.y *(cubeSize+cubeSpacing) * Time.deltaTime) / riseTime;
             trans.position = pos;
         }
         timer+= Time.deltaTime;
         yield return null;
     }

The distance between the last object and my visual reference is never the samealt text. I can't fix it and I would really appreciate any help.

I have done it beautifully with Vector3.Lerp but I need to store the initial positions for all the cubes in an array and that just doesn't sound good to me.

Thanks in advance.

question.png (12.0 kB)
Comment
Add comment · Show 7
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
avatar image bubzy · Nov 17, 2014 at 09:33 AM 0
Share

you will need to store them somewhere, it doesnt really matter if you store them in an array or in individual values on the objects, you will still have to collect and reference the data.

avatar image Codessaurus · Nov 17, 2014 at 09:56 AM 0
Share

Well yes, but if I could do it with first method the code would be much more efficient right?

avatar image bubzy · Nov 17, 2014 at 10:00 AM 0
Share

i think you are really "straining the gnat" here, this is not going to be a massive gain in efficiency unless you are moving thousands of objects.

are all the units being moved constantly or are you choosing when to move them?

avatar image Codessaurus · Nov 17, 2014 at 01:21 PM 0
Share

I chose when to move them... $$anonymous$$aybe you are right and I'm just complicating things. Anyway I spent so much time on the first method and Im so sure it's right. I would really like to know why it's not working.

avatar image bubzy · Nov 17, 2014 at 01:47 PM 0
Share

so the 4 cubes at the top move correctly and the two at the bottom do not?

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bubzy · Nov 17, 2014 at 01:59 PM

this works, not sure if its the kind of thing you are after though.

 using UnityEngine;
 using System.Collections;

 public class moveDir : MonoBehaviour {

 // Use this for initialization

 float timeToMove = 1f;
 float currentTime;
 GameObject[] cubeList;
 bool move = true;

 void Start () {
     cubeList = GameObject.FindGameObjectsWithTag("cube");
     currentTime = Time.time + timeToMove;
 }
 
 // Update is called once per frame
 void Update () {
     if(move)
     {
     moveCubes(timeToMove);
     }
     if(!move)
     {
         currentTime = Time.time + timeToMove;
     }
     if(Input.GetKeyDown(KeyCode.A))
     {
         move = !move;
     }
 }

 void moveCubes(float delayTime)
 {

     if(Time.time < currentTime)
     {
     

     foreach(GameObject cube in cubeList)
     {
         cube.transform.position += new Vector3(0,1,0)*Time.deltaTime;
     }
     }
 }


 }
Comment
Add comment · Share
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

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

transform.position not setting position OR animation setting position even though it shouldn't 0 Answers

[2D] Get the position of an object outside the scope 2 Answers

moving objects with transform position 2 Answers

Why can't I assign transform.position to a Vector3 object? 3 Answers

Why is rotation offset 1 Answer


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