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 franktrog · Jun 29, 2012 at 05:37 PM · rotationpositionobject

Return object to position

I am attempting to slowly rotate an object back to 0,0,0 and when I click on the button that is controlling the function Unity freezes. Any input on this would be appreciated.

 if(iFocusB){        
     while(camObj.transform.rotation != Quaternion.identity){
         var xR = 0;
         var yR = 0;
         var zR = 0;
         
         if(camObj.transform.rotation.x != 0) xR = 5; else xR = 0;
         if(camObj.transform.rotation.y != 0) yR = 5; else yR = 0;
         if(camObj.transform.rotation.z != 0) zR = 5; else zR = 0;
         
         camObj.transform.Rotate(Vector3(xR,yR,zR)*Time.deltaTime*speed);        
     }
     
     //camObj.transform.position = Vector3.zero;
     //camObj.transform.rotation = Quaternion.identity;
 }    

Regards,

Frank


Frank added his code in an answer, I pasted it on pastebin. He also uploaded the .js. (Berenger)

Here is the complete code. I have a few buttons and don't have them doing anything until I can figure out how to properly rotate the object in the scene. Basically I have an object parented to an empty object and am using code to rotate it around to certain sides.

Here is the current code and I uploaded the file since it doesn't look so nice in this area. I'm new to Unity scripting so I want to go ahead and apologize if I am really doing something in a bad way.

Regards,

Frank

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

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by irrationalistic · Jun 29, 2012 at 06:06 PM

This should really be in a coroutine with yields or in the object's Update() function. While statements execute until they are completed and don't care about frame-rate.

Comment
Add comment · 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 Loius · Jun 29, 2012 at 06:13 PM 0
Share

Additionally, it's unlikely that the rotation will ever be Quaternion.identity, because float precision combined with quaternion operations means you'll hit 1.0000001, which isn't 1.0, and then skip over 1.0 and hit 0.9999999 on the other side. There are some angle-comparison functions in Qauternion/Vector3 that can be used to test "if rotation is within 1 degree of identity" that will eventually exit the while.

avatar image franktrog · Jun 29, 2012 at 06:17 PM 0
Share

Sorry, I am new to Unity and dont really understand your answer.

avatar image
1

Answer by Piflik · Jun 29, 2012 at 06:13 PM

Not sure if that helps, but I highly doubt you will ever reach Quaternion.identity this way. There are two problems: Gimbal Lock and accuracy. The former is not solvable with a fixed rotation order, the latter could be fixed by rounding...

Anyway, why don't you simply use Quaternion.Slerp?

 transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.identity, Time.time * speed);
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 franktrog · Jun 29, 2012 at 06:40 PM 0
Share

I actually stumbled upon the article for the Slerp method and tried to implement it with this code.

if(iFocusB){ focusTrigger = true; }

if(focusTrigger == true){ camObj.transform.rotation = Quaternion.Slerp(transform.rotation, Quaternion.identity, Time.deltaTime*smooth);; if(camObj.transform.rotation == Quaternion.identity) focusTrigger = false; }

However focusTrigger is never set back to false even though the if statement triggers. I tested by adding a Debug.Log to the if statement.

Also the object snaps back to Quaternion.identity rather than smoothly transitioning. Any ideas why this may be?

avatar image Piflik · Jun 29, 2012 at 06:50 PM 0
Share

Not sure what you do wrong, but when I use the exact same script you posted, it works.

Post the complete script, preferably formated correctly.

avatar image franktrog · Jul 01, 2012 at 01:16 PM 0
Share

Posted the script and it was added to the top of this page.

avatar image
0

Answer by franktrog · Jul 02, 2012 at 10:45 PM

Well I figured out my issue. In the first part of the Slerp I was forgetting to identify which object to use for the "from" transform.rotation point. Here is the final code.

if(iFocusB){
camTrigger = 1; }

if(iFocusRB){ camTrigger = 2; }

if(iFocusTB){ camTrigger = 3; }

if(Input.GetMouseButton(0)){ camObj.transform.Rotate(Vector3(Input.GetAxis("Mouse Y"), 0, Input.GetAxis("Mouse X")) Time.deltaTime speed); }

if(camTrigger == 1){//Triggers when iFocusB is pushed camObj.transform.rotation = Quaternion.Slerp(camObj.transform.rotation, Quaternion.Euler(92,-50,0), Time.deltaTime*smooth);; if(camObj.transform.rotation == Quaternion.Euler(92,-50,0)) camTrigger = 0; }else if(camTrigger == 2){//Triggers when iFocusRB is pushed camObj.transform.rotation = Quaternion.Slerp(camObj.transform.rotation, Quaternion.Euler(90,90,0), Time.deltaTime*smooth);; if(camObj.transform.rotation == Quaternion.Euler(90,90,0)) camTrigger = 0; }else if(camTrigger == 3){//Triggers when iFocusTB is pushed camObj.transform.rotation = Quaternion.Slerp(camObj.transform.rotation, Quaternion.Euler(20,15,92), Time.deltaTime*smooth);; if(camObj.transform.rotation == Quaternion.Euler(20,15,92)) camTrigger = 0; }

Thanks for the help everyone!

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

6 People are following this question.

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

Related Questions

how to set rotation / position of an object on trigger? 2 Answers

Add force at position with respect to rotation 1 Answer

set child object position when playing the animation 0 Answers

Move object A towards object B 2 Answers

Make object appear within camera view 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