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 HectorOfPriamos · May 25, 2020 at 12:50 PM · rotationquaternioncoroutineslerpprogress

Slerp Finishes Rotation When Progress is 0.5f

Hi,

I have a question about using Slerp method. I'm using it to rotate an object so that it will be looking at a point. However as far as I know, when progress is 1, Slerp will be done. But in my case it turns towards the point I want when progress is 0.5f. And If I continue turning until progress is 1, it turns back to its original rotation.

Actually my code is working in that way but I wonder why it doesn't work as it should when slerp is called until progress is 1. Thanks in advance!

 private IEnumerator TurnTowardsFinalTrack_CO()
 {
         float progress = 0f;
 
         Transform centerP = selectedRunway.GetRunwayCenterPoint();
 
         Vector3 dirVector = -centerP.position + transform.position;
         Quaternion targetRotation = Quaternion.LookRotation(dirVector, Vector3.forward);
         Vector3 correctionVector = Vector3.zero;
 
         while (progress < 0.5f) //weirdly works in that way
         {
             transform.transform.rotation = Quaternion.Slerp(transform.transform.rotation, targetRotation, progress);
             correctionVector.z = transform.transform.eulerAngles.z;
             transform.eulerAngles = correctionVector;
             progress += Time.deltaTime;
             yield return null;
         }
  }




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 KoenigX3 · May 25, 2020 at 02:48 PM

Quaternion.Slerp and other interpolating functions usually need 3 parameters:

  • A starting rotation (or position, if its a Vector3 interpolation)

  • The desired rotation (or position)

  • A float between 0 and 1.

Normally, the first and second parameter does not change. In your case, your starting rotation is not static, because it will always be the current rotation. As you interpolate, transform.rotation changes, and you interpolate with the new rotation as the starting point.

Store the starting rotation in a Quaternion before the interpolation, and use it as your first parameter.

 Quaternion startRotation = transform.rotation;
 
 while(progress <= 1)
 {
    transform.rotation = Quaternion.Slerp(startRotation, targetRotation, progress);
 }



As a side note: using transform.rotation as your first parameter is not always a problem. You can create a decreasing interpolation with that. The rotation is actually changing between 0.5 and 1, but the change is so subtle, it is usually unnoticed.

Another side note: you don't have to use transform.transform, you can replace it with transform.

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 HectorOfPriamos · May 25, 2020 at 06:04 PM 0
Share

I missed that startRotation part, now I see where my thinking was faulty. And about transform.transform thing, oh god.. It is totally my unintentional writing. How come I didn't realize it, shameful considering the number of times I checked that code :) Anyway, thanks for the clear answer!

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

176 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 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

Rotating about and only one axis 1 Answer

How to get Angle float of specific axis.(Turret clamping related). 2 Answers

Quaternion Rotation Smooth 1 Answer

Issue With Rotation on Key Press 1 Answer

How to rotate an object around another 60 degrees with a keypress? 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