• 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
Question by chrian · Sep 17, 2018 at 06:59 AM · rotationquaternionlerpeuleranglesslerp

Make Quaternion Slerp happen over a 1 second period

Basically, I want my character to Slerp $$anonymous$$s y axis 90 degrees when the "B" button is being held. When "B" is held, it runs the method "TurnArround()," and "TurnArround()" runs as long as the "B" button is held. I want the rotation to occur over a 1 second period. I have tried so many different variations with Time.deltaTime to try to get t$$anonymous$$s transition to work, but no luck. Also whenever trying these variations, for some reason my character doesn't perform any rotation. The only time I get a visible result of my desired rotation is when I make the time perameter = to 1, but the result is an instant rotation, not over a 1 second period. Can someone plese help me with t$$anonymous$$s? I've been stuck for hours.

alt text

capture.png (13.3 kB)
Comment

People who like this

0 Show 0
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

3 Replies

· Add your reply
  • Sort: 
avatar image

Answer by Sonky108 · Sep 17, 2018 at 07:11 AM

Hey! It's better to put code as code, not as image :)

 private float rotationTime = 1f;
 Quaternion targetRotation;
 Quaternion startRotation;
 
 void StartRotation()
 {
     targetRotation = Quaternion.Euler(transform.rotation.eulerAngles.x, transform.rotation.eulerAngles.y + 90, transform.rotation.eulerAngles.z);
     
     startRotation = transform.rotation;
 
     startTime = Time.time;
     currentTime = startTime;
 }
 
 void DuringRotation()
 {
     currentTime += Time.deltaTime;
     transform.rotation = Quaternion.Slerp(startRotation, targetRotation, (currentTime - startTime) / rotationTime);
 }
 

You can try somet$$anonymous$$ng like t$$anonymous$$s. StartRotation when button is pressed and DuringRotation until button is held down :)

Comment

People who like this

0 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 chrian · Sep 17, 2018 at 12:10 PM 0
Share

I don't know what you mean by... "StartRotation when button is pressed and DuringRotation until button is held down."

In my game you hold down the B button and it should make the character lerp 90 degrees. So I don't know what you mean by when button is pressed. In my game the button is just held down. Not just pressed.

avatar image Sonky108 chrian · Sep 17, 2018 at 12:35 PM 0
Share

Hey, every button is meant to be pressed :p kidding. What I meant is you can see within Unity three states of a button: pressed, held down, released. Both pressed and released last only one frame, that are states where user starts to held down a button and is about to release it. There are three methods for this:

  1. Input.GetButton(string);

  2. Input.GetButtonDown(string);

  3. Input.GetButtonUp(string);

So, you have GetButtonDown == true during one frame, GetButton is true until user release the button, and then GetButtonUp == true for one frame. After that all of them are false until next input. Check the link below:

https://docs.unity3d.com/ScriptReference/Input.html

In that example you should fire StartRotation when GetButtonDown is true and fire DuringRotation while GetButton is true :)

avatar image

Answer by Legend_Bacon · Sep 17, 2018 at 02:38 PM

Hello there,


T$$anonymous$$s function here might help you. All you have to do is call RotateOverTime() and pass it:

• The object you want to rotate

• The rotation total duration

• The start rotation (usually just the object's current rotation)

• The target rotation


         private void RotateOverTime(GameObject targetObject, float transitionDuration, Quaternion start, Quaternion target)
         {
             StartCoroutine(RotateOverTimeCoroutine(targetObject, transitionDuration, start, target));
         }
 
         private IEnumerator RotateOverTimeCoroutine(GameObject targetObject, float transitionDuration, Quaternion start, Quaternion target)
         {
             float timer = 0.0f;
 
             w$$anonymous$$le (timer < transitionDuration)
             {
                 timer += Time.deltaTime;
                 float t = timer / transitionDuration;
                 t = t * t * t * (t * (6f * t - 15f) + 10f);
 
                 targetObject.transform.rotation = Quaternion.Slerp(start, target, t);
 
                 yield return null;
             }
 
             yield return null;
         }
     }


I threw in a smoot$$anonymous$$ng functionality, but if you want it to be linear you can throw it away. For more info on that, you can take a look at t$$anonymous$$s page here.


Hope that helps!

Cheers,

~LegendBacon

Comment

People who like this

0 Show 0 · 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

Answer by LCStark · Sep 17, 2018 at 03:23 PM

You're confusing Quaternion.Slerp with Quaternion.RotateTowards. The latter method is the one you are looking for.

transform.rotation = Quaternion.RotateTowards(transform.rotation, targetRotation, Time.deltaTime);

EDIT
The t$$anonymous$$rd parameter is the rotation step - it limits by how many degrees the rotation should change at a single step, so using just Time.deltaTime might make the rotation too slow. If you want to rotate by 90 degrees over 1 second, use Time.deltaTime * 90.0f.

Comment

People who like this

0 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

123 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

Related Questions

Lerp 180 degrees while holding B and lerp back 180 degrees when let go of B. 2 Answers

Transition current camera rotation to 0,0,0 1 Answer

Rotation jumping to a different rotation after rotating a certain amount 0 Answers

Rotate quaternion 90 degrees when clicked? 1 Answer

Changing rotation of Quaternion 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