- Home /
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));
}
}
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.
Answer by Nomibuilder · Apr 21, 2015 at 08:18 AM
transform.rotation = Quaternion.Inverse(target.rotation);
Answer by PhilippG · Jan 14, 2016 at 11:47 AM
You can also do this:
transform.forward = transform.forward * -1;
Your answer
![](https://koobas.hobune.stream/wayback/20220613071541im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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