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 kurai · May 16, 2012 at 09:20 AM · rotationrigidbodyquaternioninvert

Invert the rotation of a moving object

I need to rotate an object instantly by 180 degrees at the press of a button. The object is moving, I can rotate it on its Z axis using the joypad. Then, if I press a key I need my object to immediately invert its direction (ie: if it's moving left, it flips and starts moving right).

I tried using Quaternion.Inverse, but it's not working :/

Can you explain to me why not and point me to the right way to do it? (Sorry I'm kinda a newbie with Quaternions)

 var craftSpeed:Vector3 = Vector3 (20,0,0);
 var eulerAngleVelocity:Vector3=Vector3 (0,0,200);
 var controlAxis:String ="HorizontalP1";
 
 function Start () {
     rigidbody.velocity =  craftSpeed;
 }
 
 function FixedUpdate () {
     // this aligns the velocity with the right vector, it makes the plane move towards its nose
     rigidbody.velocity = Vector3.zero;
     rigidbody.velocity = transform.right * craftSpeed.magnitude;
 
     //this one just rotates my plane on input
     var deltaRotation : Quaternion = Quaternion.Euler(eulerAngleVelocity * Time.deltaTime* Input.GetAxis(controlAxis));
     rigidbody.MoveRotation(rigidbody.rotation * deltaRotation);
 
     //and this one SHOULD make the plane invert direction on input
     if (Input.GetKeyDown ("q"))
     {
         rigidbody.MoveRotation(Quaternion.Inverse(rigidbody.rotation));
     }
 }
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 Reavenk · May 25, 2012 at 04:53 AM 1
Share

That's not what Quaternion inverse does exactly. Have you tried something like: gameObject.transform.rotation = Quaternion.Euler( 0.0f, 0.0f, 180.0f) * gameObject.transform.rotation; //?

That's globally axis aligned though, so depending on how you have your model set up, you may want to replace that Euler vector with gameObject.transform.forward * 180.0f.

That should take care of object's actual rotation, and if you want to make the rigidbody rotate the other way, invert the z component of the rigidbody's angularVelocity member variable.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by Nomibuilder · Apr 21, 2015 at 08:18 AM

  transform.rotation = Quaternion.Inverse(target.rotation);
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
avatar image
1

Answer by PhilippG · Jan 14, 2016 at 11:47 AM

You can also do this:

 transform.forward = transform.forward * -1;
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 Necronomicron · Feb 22, 2016 at 07:43 AM 0
Share
 transform.forward = -transform.forward;

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

9 People are following this question.

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

Related Questions

Finding A Rigidbody's Rotation Speed And Direction 2 Answers

Player character X rotation going haywire on game start 1 Answer

Rotating Rigidbodies 2 Answers

Alternative to Quaternion.LookRotation() ? 2 Answers

Keeping transform.forward facing forwards 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