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 erikiene · Nov 25, 2016 at 07:56 AM · transformmove an objecttimescale

Way to Move/Rotate object through List of points that would work with changing timescale

Hi all,

I've been trying to think of a way to do this for too many hours now.. I simply want a way to smoothly move an object through a list of positions (this I sort of have already) and for this to be affected when timescale and fixedDeltaTime are changed.

I currently have a simple coroutine which when activated by a keydown iterates through the list of positions and sets them appropriately:

 for (int i = targetPos.Count - 1; i > -1; i--)
         {
             transform.position = targetPos[i];
             transform.rotation = targetRot[i];
         }

This obviously will not be affected by slowing down time. So I tried replacing it with lerping or movetowards'ing but that either didn't work at all or gave some crazy non-results. My last attempt to do this looks like this (excuse my probably obvious missunderstanding of how coroutines work, but by now I've been trying all sorts of craziness):

 ...(inside coroutine)...
 for (int i = targetPos.Count - 1; i > -1; i--)
          {
              StartCoroutine(MovePos(targetPos[i], targetRot[i]));
          }

 ...(nested coroutine)...
     IEnumerator MovePos (Vector3 endP, Quaternion endR)
     {
 
         while (transform.position != endP || transform.rotation != endR)
         {
 
             transform.position = Vector3.MoveTowards(transform.position, endP, Time.deltaTime);
             transform.rotation = Quaternion.RotateTowards(transform.rotation, endR, Time.deltaTime);
 
         } 
 
         if(transform.position == endP && transform.rotation == endR)
         {
             yield break;
         }
     }

This looks exactly the same as the simple method when played.

Any ideas?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by slavo · Nov 25, 2016 at 09:47 AM

Hello,

in your Coroutine you are Lerping correctly, but everything in While loop happening in one frame. You need to wait for another frame in each iteration.

 while (transform.position != endP || transform.rotation != endR)
          {
  
              transform.position = Vector3.MoveTowards(transform.position, endP, Time.deltaTime);
              transform.rotation = Quaternion.RotateTowards(transform.rotation, endR, Time.deltaTime);
 
            //WAIT FOR ANOTHER FRAME
             yield return null;
  
          }


Comment
Add comment · Show 4 · 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 erikiene · Nov 25, 2016 at 12:13 PM 0
Share

Right, stupid of me to miss that, I had a similar setup when I wasn`t nesting coroutines.. But now the whole thing churns out *some crazy results* as mentioned in the original post. From what it looks like maybe the for loop iterates down the list once the nested coroutine is yielded and so the object doesn't get to move the whole way through to the target position before the new target is assigned? The object definitely does not reach its final position.

Because of all that I can't even begin to test whether scaling down timescale works.

No idea what to do

avatar image slavo erikiene · Nov 25, 2016 at 12:29 PM 1
Share

When you want your coroutine to wait for nested coroutine you need to yield retrun start coroutine. This way you start coroutine and you continue for loop. so you have 'targetPos.Count - 1' corutines running at same time and messing with you transform.

  ...(inside coroutine)...
  for (int i = targetPos.Count - 1; i > -1; i--)
           {
               yield return StartCoroutine($$anonymous$$ovePos(targetPos[i], targetRot[i]));
           }
avatar image erikiene slavo · Nov 25, 2016 at 12:38 PM 0
Share

alright alright alright! It's working, thank You for that, I'm marking it as an answer, but the movement I'm getting is jittery and unfortunately I've no idea why..

It's less noticeable when slowed down so I could potentially hide it as i'm only using this when time's slow, but that wouldn't really meet the "polish" level I want. Any ideas? If not, thank You anyway for sorting the basics out!

Show more comments

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Moving object to object (with Bezier Curve) doesn't land on it 0 Answers

Object movement with different time scale 1 Answer

Object rotating around object at two levels 1 Answer

Moving child and parent 1 Answer

Move relative to objects rotation (C#) 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