AddRelativeTorque and gameobject's angularVelocity
I've been experimenting around with some of the physics in Unity and something seems weird to me with the AddRelativeTorque function. I have the following code in order to slow and eventually stop a gameobject's spinning.
if ( ship.angularVelocity != Vector3.zero )
{
ship.AddRelativeTorque( ship.angularVelocity * -1 );
}
Elsewhere I have this line to apply a spin to it using input from a controller or keyboard.
ship.AddRelativeTorque( Vector3.up * Input.GetAxis( "Rotate" ) / 2 );
I don't fully understand how the physics behind AddTorque work, but from what I understand, this should apply a force opposite to the current velocity, thus slowing down and stopping the object. It does work, but if I apply enough other forces on the object, then it'll start spinning uncontrollably.
I know how to achieve what I'm trying to do in a number of different ways that I know work. I just want to know what is happening and why it's happening. I assume it has something to do with how Unity handles forces and other physics, but I don't fully understand what's going on.
In case there's something in the rest of the code, here is the full set of functions. It happens most often when the explosion force is activated several times in a row, but the strange spinning will still occur even if I'm just navigating around the world and not colliding with anything.
void moveShip ()
{
if ( !collided )
{
ship.AddRelativeForce( Vector3.forward * Input.GetAxis( "Vertical" ) );
ship.AddRelativeForce( Vector3.right * Input.GetAxis( "Horizontal" ) );
ship.AddRelativeForce( Vector3.up * -Input.GetAxis( "Up" ) );
ship.AddRelativeTorque( Vector3.up * Input.GetAxis( "Rotate" ) / 2 );
}
if ( ship.velocity != Vector3.zero )
{
ship.AddRelativeForce( -ship.velocity.x / 2, - ship.velocity.y / 2, -ship.velocity.z / 2 );
}
if ( ship.angularVelocity != Vector3.zero )
{
ship.AddRelativeTorque( ship.angularVelocity * -1 );
}
}
void OnCollision( Collision obj )
{
if ( obj.gameObject.CompareTag( "Cube" ) )
{
ship.AddExplosionForce( 1, obj.transform.position, 1 );
collided = true;
}
}
void OnCollisionExit( Collision obj )
{
if ( obj.gameObject.CompareTag( "Cube" ) )
{
collided = false;
}
}
Answer by Joaquito · Mar 14, 2017 at 09:36 AM
I've been struggling with the same, more exactly trying to stabilize a spaceship's spinning on X and Z, and yes, at some point you see it starts spinning uncontrollably as you say. I've come to the conclusion that AddRelativeTorque takes the values that you put, and makes "Relativeness calculations" instead of simply adding Torque to "Local X/Y/Z Axis". It's kinda hard to understand and explain, but the point is that AddRelativeTorque is not as good and as life-simplifying as it looks, but rather weird. I'd just use AddTorque and make any necessary calculations myself within code rather than using it.
Your answer
Follow this Question
Related Questions
How control Rigidbody2d using GetAxis with AddForce and angularVelocity (2D) 1 Answer
How do I create Sling-Shot like propulsion? 1 Answer
How to set rigidbody velocity and angularVelocity to Vector3.zero over time? 1 Answer
Confused about addForce behavior on 3D objects w/ sub-objects in 2D movement 0 Answers
bullet wont move forward 2 Answers