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
3
Question by Mike Truescape · Sep 27, 2012 at 10:10 PM · cameraquaternionlookatslerpquaternion.slerp

How do you check if Quaternion.Slerp has finished

This is my first question here. I'm a beginner programmer, just been thrown into it at work. Im actually just a 3D modeller, but anyway.

I've got a situation where I have set up a camera with a look at script that moves and changes depending on which object the user presses. Works fine but now i have been asked to make it so you can look around once from that camera point. This is what i have tried, script is applied to the camera.

 rotation = Quaternion.LookRotation(lookAt.transform.position - transform.position); 
 if(lookAtOn==true){ 
     transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping); 
 } 
 if(transform.rotation==rotation){ 
     lookAtOn=false; tVCamCentred=true; 
 } else{ 
     tVCamCentred=false; 
 }

Then i just have pretty much on click on the screen to reset the camera, lookAtOn=true

This works perfect when it wants to. every so often it just gets stuck with lookAtOn=true and won't turn off, have to close the app and restart.

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 AlucardJay · Sep 27, 2012 at 10:10 PM 2
Share

I just formatted your code. You can do this by highlighting all your code, then clicking the 10101 button at the top of the edit window =]

5 Replies

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

Answer by Bunny83 · Sep 27, 2012 at 10:23 PM

It will never finish because you use Slerp as a "divide-remaining-distance" smoothing function. Slerp is usually ment to linearly interpolate a rotation froma constant start rotation to a constant target rotation. The t-parameter has to be moved from 0 to 1. 0 means start rotation 1 means end rotation a value in between will return the respective linear interpolated rotation. Since your t value never reaches 1.0 you will never reach the end. The difference between your current / start rotation and the target rotation will get incredible small, but you will not reach the end.

You can check the Angle between your current rotation and your target rotation and if it's smaller than for example 1° you can treat it as finished.

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 DoctorSauce · Dec 30, 2014 at 07:16 PM 0
Share

Brilliant. Upvote for you, sir!

avatar image
2

Answer by svitlana · Feb 20, 2016 at 06:29 AM

float diff = player.transform.rotation.eulerAngles.y - targetRotation.eulerAngles.y; float dergee = 1; if (Mathf.Abs (diff) <= dergee) { Debug.Log("ready"); }

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

Answer by Toadill9114 · Aug 29, 2018 at 08:02 AM

It would be like this.

 public void RotateTowards()
 {
         float strength = 1f;
         float str;
         
         Vector3 direction = (transform.position - playerAgentBase.transform.position).normalized;
         direction.y = 0;
         str = Mathf.Min(strength * Time.deltaTime, 1);
         Quaternion lookRotation = Quaternion.LookRotation(direction);
         playerAgentBase.gameObject.transform.rotation = Quaternion.Slerp(playerAgentBase.gameObject.transform.rotation, lookRotation, str);
         float angle = Quaternion.Angle(playerAgentBase.gameObject.transform.rotation, transform.rotation);
         if (angle >=179f)
         {
             rotate = false;
         }
  }
 void Update()
 {
     if (rotate)
     {
         RotateTowards();
     }
 }





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

Answer by usernameHed · Nov 06, 2018 at 05:32 PM

For checking an angle diff, in degree, I have this function:

 public static bool IsAngleCloseToOtherByAmount(float angleReference, float angleToTest, float differenceAngle)
 {
    if (angleReference < 0 || angleReference > 360 ||
        angleToTest < 0 || angleToTest > 360)
     {
           Debug.LogError("angle non valide: " + angleReference + ", " + angleToTest);
      }
         
     float diff = 180 - Mathf.Abs(Mathf.Abs(angleReference - angleToTest) - 180);
         
     //diff = Mathf.Abs(angleReference - angleToTest);        
         
      if (diff <= differenceAngle)
            return (true);
     return (false);
 }

Then For checking the ending of an Quaternion.slerp, this function:

 /// <summary>
     /// Is a Quaternion.Slerp is over ?
     /// </summary>
     public static bool IsQuaternionSlerpOver(Quaternion objectInRotation, Quaternion targetRotation, float marginInDegree = 1)
     {
         if (!IsAngleCloseToOtherByAmount(objectInRotation.x, targetRotation.x, marginInDegree))
             return (false);
         if (!IsAngleCloseToOtherByAmount(objectInRotation.y, targetRotation.y, marginInDegree))
             return (false);
         if (!IsAngleCloseToOtherByAmount(objectInRotation.z, targetRotation.z, marginInDegree))
             return (false);
         return (true);
     }

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

Answer by JahnStar · Jun 03, 2020 at 06:29 PM

 transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
 if (Quaternion.LookRotation(lookAt.transform.position - transform.position) == transform.rotation)
 {
 // write your code here
 }




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

15 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

Related Questions

Quaterion.Slerp + Lookat, camera flipping 0 Answers

Look at rotation at a moving object while moving (C#)(2D) 3 Answers

Gameobject Rotation Calculation 0 Answers

Using MouseLook script with LookAt function 2 Answers

Get slerp to work just as LookAt(,Vector3.right) does 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