Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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
1
Question by xJames720x · Oct 13, 2012 at 09:45 PM · rotationgameobjectquaternionslerp

How to reset a game object to its original rotation.

I have a game object that when clicked, moves upwards a little, and rotates to demonstrate selection. I am trying to get it so that when I click the object again (deselect it), the object moves back down to the original way it was facing.

Moving the object down again is no problem, however I am having trouble getting the object to rotate, so that if faces the way it was originally facing. This is the latest code I have tried, however when I deselect the object it simply moves back down and keeps its new rotation.

 public Transform originalRotationValue;
 float rotationResetSpeed = 1.0f;
 
 void Start () {
         originalRotationValue = gameObject.transform;
     }
 
 void Update () {
     //rotate selected piece
     if(isSelected)
         gameObject.transform.Rotate(0, 1, 0);
     //otherwise rotate it back to its original rotation
     else
     transform.rotation = Quaternion.Slerp(transform.rotation, originalRotationValue.rotation, Time.time * rotationResetSpeed);
     }


What am I missing/doing wrong?

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 byerdelen · Oct 13, 2012 at 09:52 PM 0
Share

You should try storing the rotation of the Transform at the Start, not the transform. The transform you have defined will change with the code so the start value

1 Reply

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

Answer by aldonaletto · Oct 13, 2012 at 09:51 PM

You're saving the object's transform, but should instead save the original rotation!

 public Quaternion originalRotationValue; // declare this as a Quaternion
 float rotationResetSpeed = 1.0f;
 
 void Start () {
     originalRotationValue = transform.rotation; // save the initial rotation
 }
 
 void Update () {
     //rotate selected piece
     if(isSelected)
         gameObject.transform.Rotate(0, 1, 0); // this rotates only 1 degree!
     //otherwise rotate it back to its original rotation
     else
         transform.rotation = Quaternion.Slerp(transform.rotation, originalRotationValue, Time.time * rotationResetSpeed); 
 }
Comment
Add comment · Show 3 · 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 sidhi · Aug 01, 2014 at 11:56 PM 1
Share

Do you have in javascript or JS ?

avatar image StartStart · Apr 13, 2015 at 11:37 AM 1
Share

Whoa this is helpful thanks!

avatar image mksindia · Dec 20, 2020 at 05:25 PM 0
Share

2020 still works Has anyone told you yet that you are a life saver

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

13 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

Related Questions

Troubles instatiating a gameobject with rotation 0 Answers

Rotating about and only one axis 1 Answer

Quaternion.Slerp with Quaternion.LookRotation causes unexpected results 1 Answer

Adding Rotation to a Gameobject 1 Answer

Rotating a sprite toward the mouse position on instantiation. 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