Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 alcamedes · Oct 03, 2018 at 11:18 PM · arraylistlerploopmoving

No Movement when Attempting to Lerp through an array

Hello Again, Thought I could work through this one on my own but I guess not.

I have a collection of game objects that when triggered should lerp to a new position. Each target position is an empty game object stored as a child of the object that will be moving for easy access.

 void Update(){
        // global event is triggered
        if (explode)
         {
             // get the blocks that should move
             GameObject[] blocks = GameObject.FindGameObjectsWithTag("b1");
 
              // get the starting position of each block
             foreach (GameObject block in blocks)
             {
                 Transform t = block.transform;
                 // add starting transform to List<Transform>  origins
                 origins.Add(t);
 
                 //get ending position if any from gameobject nested under this
                 if (block.GetComponentInChildren<Transform>() != null)
                 {
                     Transform t2 = block.GetComponentInChildren<Transform>();
 
                     //add ending transform to List<Transform> targets
                     targets.Add(t2);
                 }
             }
 
              //loop through blocks[] and move each from its origin location to its target location
             for (int i = 0; i < blocks.Length; i++)
             {
                 blocks[i].transform.position = Vector3.Lerp(origins[i].position, targets[i].position, Time.deltaTime);
             }
 
             //Prevent from future loops
             explode = false;
         }
 }


Whats happening is that it loops through the discovery phase each frame finding blocks, origins, and targets, but doesnt move anything and doesnt stop looping.

been at the for hours and don't know where i'm stuck :(

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by JVene · Oct 04, 2018 at 03:11 AM

My opening observation is not directly about your question, but I assume you know that both arrays "origins" and "targets" could end up out of alignment if the return from getting the transform from a child returns null. If you have a guarantee, you'd not actually need to test for null (and I'm not saying you shouldn't test for null, you should). Maybe you should 'sense' if this alignment is invalid and abandon the operation and log an error.


That said, after you get through debugging and this works, you may do well to consider retaining origins and targets, because forming them anew every time is probably not necessary (though I don't know your construction, maybe that does change during runtime).


That's not the problem. Your puzzle is solved simply, but first I must assume the two arrays are in alignment, and that for any 'i', targets and origins are not the same vector3 values.


The problem is the use of Time.deltaTime. If Update fires at the refresh rate of the device, and assuming that's 60 fps and is fairly steady, then Time.deltaTime will always be .0166667 seconds, at every update. Time.deltaTime would differ only if the frame rate isn't steady, but even in that case it shouldn't vary by much (unless something is wrong or you're debugging). If, say, the rate slowed to 30 fps occasionally, Time.deltaTime might reach 0.3333, but then if the next frame returns to the rate of 60 fps, Time.deltaTime will return to .0166667.


For a slerp or lerp (and several similar functions), the third parameter is expected to transit between 0 and 1.0. When 0 is provided, the result will be the first vector provided, when 1.0 the result will be the second vector provided, and all values between are interpolations of a vector (point) between the first and second, as a ratio expressed in the third parameter. Since that value in your code, as posted, is probably never changing, nothing will ever change the result (except for what might look like some small jitter if the frame rate isn't constant).


The solution is simple. Establish a float that counts elapsed time. Start it at zero. Increment this float by Time.deltaTime just before using that float as the third parameter to a lerp or slerp, so it can accumulate over time, and move from 0 to 1.0.


You should clamp the value to 1.0 for the parameter to lerp (there isn't much of a meaning to a lerp where this parameter exceeds 1.0). When it reaches 1.0, all of the transitions from 'origins' to 'targets' is completed, and this should stop the motion (or reset it to proceed to some next step).

Comment
Add comment · Show 2 · 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
avatar image alcamedes · Oct 04, 2018 at 06:41 PM 0
Share

Hello JVene, Thank you for such a detailed response.

yes, there is the possibility of misalignment in this script but i've mitigated that in the scene so no issues there.

while I appreciate your detailed explanation of the Time.deltaTime function I don't think thats the issue. I just created a version of this function using the exact same language but ins$$anonymous$$d of getting the target position from a transform that is a child of the moving object i manually entered in the location and it works perfectly.

That leads me to believe there is a logic loop within the parent child relationship of the scene structure. it seems like it should work since the values of origin and target are defined and stored in a List prior to any movement but that's what i'm able to figure out.

going to begin exploring other ways of storing and accessing target vectors rather than nesting it as a child of the moving object unless anyone has any other ideas.

thanks!

avatar image JVene alcamedes · Oct 04, 2018 at 10:12 PM 0
Share

Your point doesn't quite explain the reason for a lerp in the first place. I submit there is no reason for a lerp, since the value given to lerp is always the same (under stable frame rate conditions), and is so low a value as to be nearly always the origins[i].position. targets[i].position has almost no influence in that code. This means there must be something happening to origins[i].position in order for anything to move, and that's the motion you're getting from the lerp (almost without regard to targets[i].position). Put another way, you should hardly see a difference in results if you replaced the lerp with something like:

  blocks[i].transform.position = origins[i].position;

Or something effectively similar.

avatar image
0

Answer by alcamedes · Oct 05, 2018 at 01:22 AM

OK solved it. IT's what I though. By getting the target location from a child it was creating a logic loop where nothing was happening.

Moved all the target transforms to another GO parent and it worked.

Comment
Add comment · Show 1 · 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
avatar image JVene · Oct 05, 2018 at 02:43 AM 0
Share

That was something only you could see, nothing about that structure issue was visible out here. Glad it worked out.

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

110 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Create a button from an int, remove button when clicked, and never load it again 1 Answer

A node in a childnode? 1 Answer

Loop trough list of textures and chek mouse position over 0 Answers

What's the best way to find the smallest Vector3.Distance of an array of enemies? 2 Answers

what are Lists? 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