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 Diet-Chugg · Nov 06, 2011 at 12:38 AM · rotatesmoothsmoothlyinverse

how to smoothly rotate an object to inverse direction

how can I smoothly rotate an object to inverse direction? Here is what I put together when trying to solve the problem. (However my guy never turns around...)

Also, how I can I check and see when I have successfully turned around.

 function TurnAround()
 {
     if(needInverse)
     {
         //Get the opposite rotation needed.
         rotArd = Quaternion.Inverse(transform.rotation);    
         needInverse = false;
     }    
     //Rotate back.
     transform.rotation = Quaternion.Slerp(transform.rotation, rotArd, 1);
     if(rotArd == transform.rotation)
     {
         //Finished turning around.
         turnAround = false;
         isPickDir = true;
         needInverse = true;
     }
 }
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

2 Replies

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

Answer by aldonaletto · Nov 06, 2011 at 04:11 AM

Quaternion.Inverse(transform.rotation) creates a rotation to make the character return to the orientation it was created or imported - I suspect that this isn't what you want.
If you want a function to rotate some angle lower than 180, you can use Quaternion.RotateTowards in a coroutine like this:

var speed: float = 60; // speed in degrees per second var turning: boolean = false; // this is true while turning

function TurnAngle(angle: float){

if (!turning){ // don't start another rotation while this one is running turning = true; // tells that this rotation is running var degrees = Mathf.Abs(angle); // calculate degrees to rotate var dest = Quaternion.Euler(0, angle, 0) * transform.rotation; while (degrees > 0){ var dt = speed*Time.deltaTime; // rotate dt degrees this frame transform.rotation = Quaternion.RotateTowards(transform.rotation, dest, dt); degrees -= dt; // subtract the angle rotated this frame yield; // let Unity free till the next frame } turning = false; // tells that rotation finished } }
RotateTowards goes in the shortest direction, thus for angles < -180 or > 180 the object will rotate in the opposite direction, and for exactly 180 or -180, the object may rotate around weird axes due to a quaternion problem that I call "South Pole Madness". If you need to rotate to 180, for instance, call TurnAngle(90) two times in another coroutine, waiting turning become false before each call - like this:

 function Turn180(){
     while (turning) yield;
     TurnAngle(90);
     while (turning) yield;
     TurnAngle(90);
 }
 

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 Diet-Chugg · Nov 06, 2011 at 11:27 PM 0
Share

Brilliant! Just what I was looking for. Thanks again!

avatar image
0

Answer by reytuty · Jun 03, 2014 at 10:21 PM

Im using this for invert Input.gyro.attitude quaternion but I have to work with Vector3 and them invert rotate

basicly I do that:

Vector3 rotation = quaternionAngle.eulerAngles ; rotation.x = -1 ; rotation.y = -1 ; Quaternion quaternionInverted = Quaternion.Euler( rotation ) ;

look the class:

using UnityEngine; using System.Collections;

public class DevicePosition {

 // Use this for initialization
 public static void start () {
     Input.gyro.enabled = true;
 }
 
 // Update is called once per frame
 public static Vector3 getVector () {
     Vector3 rotation = Input.gyro.attitude.eulerAngles ;
     rotation.x *= -1 ;
     rotation.y *= -1 ;
     return rotation ;
 }
 public static Quaternion getQuaternion(){
     return Quaternion.Euler ( DevicePosition.getVector() ) ;
 }

}

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Rotate camera around object smoothly 2 Answers

How do I rotate a Gameobject to a given direction smoothly? 1 Answer

Turn page, smooth rotate 1 Answer

OnTriggerEnter smooth Rotate 1 Answer

smooth rotate help 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