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 /
This question was closed Oct 25, 2016 at 08:32 AM by JincSoft for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by JincSoft · Apr 05, 2015 at 07:49 PM · c#unity 5rotationquaternion.slerp

Quaternion.Slerp rotating immediately not over time

I am trying to create a camera system akin to the DSi/3DS version of Persona or Demon Gaze for the PS Vita. The problem is when I rotate the camera, it snaps to the end rotation immediately rather than over time (there's an empty game object with this script and a character controller attached with the camera as the child). The debug.log spits out that the timeProgressed variable is still counting upwards after it rotates 90 degrees (it's still less than one) but it's like Slerp/Lerp (tried both) ignores it.

  private float rotation;
  private Transform to;
  private Transform from;
  public float rotateSpeed;


  void Update () {
             ControlPlayer();
         }      
     
 
     void ControlPlayer(){
 
             from = transform;
 
             if(cInput.GetKeyDown("Left")){
                 rotation -= 90.0f;
                 to.rotation = Quaternion.Euler(0, rotation, 0);
                 StartCoroutine(SlerpRot(from, to, rotateSpeed));
             }
             if(cInput.GetKeyDown("Right")){
                 rotation += 90.0f;
                 to.rotation = Quaternion.Euler(0, rotation, 0);
                 StartCoroutine(SlerpRot(from, to, rotateSpeed));
             }
             
     }
 
 
     IEnumerator SlerpRot(Transform startRot, Transform endRot, float slerpTime){
 
         float startTime = Time.time;
         float endTime = startTime + slerpTime;
 
         while(Time.time < endTime)
         {
 
             float timeProgressed = (Time.time - startTime) / slerpTime;
             Debug.Log(timeProgressed);
             transform.rotation = Quaternion.Slerp(startRot.rotation, endRot.rotation, timeProgressed);
             
             yield return new WaitForEndOfFrame();
 
         }
     }


 }
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

  • Sort: 
avatar image
1
Best Answer

Answer by Deadcow_ · Apr 05, 2015 at 08:13 PM

You don't show where you instantiate to variable, but it seems like variables from and to references the same object. Try to use Quaternion instead of Transform:

 StartCoroutine(SlerpRot(transform.rotation, Quaternion.Euler(0, rotation, 0), rotateSpeed));

And the coroutine (I changed the way to calculate time a bit, I'd prefere this strict way instead of calculating global time):

 IEnumerator SlerpRot(Quaternion startRot, Quaternion endRot, float slerpTime) 
 {
     float elapsed = 0;
     while(elapsed < slerpTime) 
     {
         elapsed += Time.deltaTime;
 
         transform.rotation = Quaternion.Slerp(startRot, endRot, elapsed / slerpTime);
              
         yield return null;
     }
 }
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 JincSoft · Apr 05, 2015 at 10:57 PM 0
Share

Thanks, that seemed to fix it. To was initially assigned in the start function (the bit I pasted was only part of the entire script). I don't know if it was on my end, but I had to change the startRot to a transform and separate the Quaternion.Euler part since it would only rotate "smoothly" if I tapped the left or right arrow twice.

Update code:

               if(cInput.Get$$anonymous$$eyDown("Left")){
                 rotation -= 90.0f;
                 to = Quaternion.Euler(0, rotation, 0);
                 StartCoroutine(SlerpRot(transform, to, rotateSpeed));
             }
             if(cInput.Get$$anonymous$$eyDown("Right")){
                 rotation += 90.0f;
                 to = Quaternion.Euler(0, rotation, 0);
                 StartCoroutine(SlerpRot(transform, to, rotateSpeed));
             }





Coroutine:

 IEnumerator SlerpRot(Transform startRot, Quaternion endRot, float slerpTime) 
     {
         float elapsed = 0;
         while(elapsed < slerpTime) 
         {
             elapsed += Time.deltaTime;
 
             transform.rotation = Quaternion.Slerp(startRot.rotation, endRot, elapsed / slerpTime);
             
             yield return null;
 
         }
 
     }



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Flip over an object (smooth transition) 3 Answers

How do i determin if an objects rotation is between 2 values on each axis? 0 Answers

Cube Gravity - Apply Gravity to a Curve 0 Answers

Distribute terrain in zones 3 Answers

Player rotation issues (Player returns to default forward rotation after releasing keys) 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