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
0
Question by Inan-Evin · Mar 15, 2012 at 08:29 PM · quaternionrotatesmoothslerpslowly

Quaternion.Slerp question, rotating back to original

Hello everyone, I simply want to make my gun rotate a bit left while walking to the left. So I thought I can use Quaternion.Slerp, I used Unity's example code :

  var from : Transform;
     var to : Transform;
     var speed = 0.1;
     function LateUpdate () 
     {
     from = myGun.transform;
     if(Input.GetKey("a")){
     myGun.transform.rotation =
           Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);
     }
     }

I made an empty gameObject, and rotate it a bit left, and I attached it as "to". Now, when I press "a" key, it works weapon rotates to left as soon as it reaches to "to" gameObject's rotation, but when I released "a" key, it suddenly rotates to it's original rotation, I want it to rotate to it's original rotation slowly and smoothly. In summary, what I want is, my gun will rotate left slowly when I press "a" , but then when I stopped pressing "a" key, gun will rotate to it's original rotation slowly, how can I do that ? Thanks :)

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 DaveA · Mar 15, 2012 at 08:36 PM

 if(Input.GetKey("a")){
 myGun.transform.rotation =
       Quaternion.Slerp (from.rotation, to.rotation, Time.time * speed);
 }
 else
     myGun.transform.rotation =
       Quaternion.Slerp (to.rotation, from.rotation, Time.time * speed);
Comment
Add comment · Show 7 · 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 DaveA · Mar 15, 2012 at 08:38 PM 1
Share

Note that if you release the 'a' key before the gun rotates all the way, it will snap to what would have been its final rotation before rotating back. Ins$$anonymous$$d, consider using myGun.transform.rotation as the 'from' argument.

avatar image Inan-Evin · Mar 15, 2012 at 08:39 PM 0
Share

well I tried this, but it does not slowly turns back to it's original, I don't know why but as soon as I released "a" key it goes to original instantly, is it about the code, or can it be about my weapon ?

avatar image DaveA · Mar 15, 2012 at 08:45 PM 1
Share

Now that I notice, you use Time.time which is absolute time since starting, an ever-increasing number. Try Time.deltaTime ins$$anonymous$$d.

avatar image Inan-Evin · Mar 15, 2012 at 08:47 PM 0
Share

I changed the from in the else statement like you said, it worked. But now there is another problem occured. I pressed and hold "a", the gun reached "to" rotation, and I released "a", it went back to original, perfect. But when I press a again, it goes to "to" rotation instanly and when I released "a" it goes to original instantly too, like once it completes reaching slowly, it switches to instant mode, do you have any ideas ?

avatar image Inan-Evin · Mar 15, 2012 at 08:54 PM 0
Share

well, with a little glitch which is about my weapon code and not important, it's working I guess, thanks for the help :).

Show more comments

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Doesn't Add Rotation Over 180 2 Answers

How can you make a nav mesh agent rotate 180 degrees? 1 Answer

Choppy rotation of character around y-axis 1 Answer

How to smoothly rotate an object on only two axes? 2 Answers

How to Get Quaternion Slerp to Return to Original Position? 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