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 RG_1azybones · Nov 11, 2019 at 10:37 PM · whilewhile-loopwhile loopquaternion.slerp

Quaternion.Slerp issue

I'm trying to rotate my camera using Quaternion.Slerp. I'm getting the desired rotation angle, but it's snapping to the rotation. I declared a float called "elapsed" and attempted to increment it during a while loop within the coroutine. The issue is that on key press my "elapsed" float is jumping straight to the "spinTime" float value. Here's the section of relevant code:

     private IEnumerator Spin()
     {
         elapsed = 0;
 
         while (elapsed < spinTime)
         {
             elapsed += Time.deltaTime;
             camHolder.transform.rotation = Quaternion.Slerp(transform.rotation, spinTarget, spinTime);
         }
 
         yield return null;
     }

Thanks in advance, everyone :)

Comment
Add comment · Show 1
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 RG_1azybones · Nov 11, 2019 at 10:38 PM 0
Share

I also tried dividing "elapsed" by "spinTime" in Quaternion.Slerp as advised by someone else, but it made no difference.

      private IEnumerator Spin()
      {
          elapsed = 0;
  
          while (elapsed < spinTime)
          {
              elapsed += Time.deltaTime;
              camHolder.transform.rotation = Quaternion.Slerp(transform.rotation, spinTarget, elapsed / spinTime);
          }
  
          yield return null;
      }

1 Reply

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

Answer by Neran28 · Nov 12, 2019 at 12:36 AM

Put the yield return at the end of the loop.

The elapsed / spintime addition is also required.

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 RG_1azybones · Nov 12, 2019 at 12:49 AM 0
Share

Awesome, thank you! I've been trying to learn to code on and off for years, this is the farthest I've gotten thanks to awesome people like you. If you have some spare time, would you $$anonymous$$d elaborating on why the elapsed / spintime addition is needed?

avatar image Neran28 RG_1azybones · Nov 12, 2019 at 01:59 AM 0
Share

Well slerp is a type of linear interpolation. The s in slerp means spherical. It behaves a bit different than lerp. But in general a linear interpolation takes 3 params. 2 values that are from a specific domain and the third one is the stepsize clamped between 0 and 1. Imagine a 2D carthesian grid. In this case lerp takes two pairs of (x,y) coordinates as the domain params. Lets say (1, 0) and (2, 2). You can then specify the 3. param lets say 0.5f. The lerp function then returns the value from that domain at step 0.5 which in this case is (1.5, 1). You can imagine a straight line between both points (1,0) and (2,2). The 3. param can be seen as a percentage. 0 yields your first param (1,0) and 1 yields your second (2,2) and everything in between 0 and 1 is a value on that line. 0.2 yields the value that lies one fifth along the line seen from the first point in the direction to the second. 0.5 is exactly the midpoint between both points. In lerp the distant for each step is the same. In slerp it is not equidistant. You can also interpolate other things than positions like rotations or colors.

Btw: yield returning at the end of loop makes your coroutine to return after each loop iteration and it will resume with the next iteration in the next frame. This results in an animation. What you did before is calculating the complete animation in one single frame which resulted in just displaying the end of your animation.

Elapsed / spintime is needed because if you just use spintime then the function returns always the same value. And if it is >= 1 it always yields your second param. You could just use elapsed as your third param but this would result in an animation duration of 1 second. This is a result of adding deltatime to your variable. I think that should be clear? If you want your animation to last for spintime seconds then you have to divide your elapsed time by the spintime. Here a few examples with spintime == 3. Elapsed == 0: 0/3=0 which yields the first param. Elapsed == 1: 1/3=0.3333 yields the rotation at one third of the spin. Without the division the animation would be completed at this point. Elpased == 3: 3:3=1 which yields yor second param. End of your animation.

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

116 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Display a loading while processing a while loop 1 Answer

While loop freezes unity 3 Answers

wierd infinite while loop 1 Answer

Vector3.MoveTowards becomes instant in while loop or freezes unity 1 Answer

while loop does not pick up again on continue 0 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