- Home /
Cancel out a Addforce && Addtorque?
The title is pretty straight forward. How can I cancel out the addrelativeforce and addrelativetorque functions to get a force of 0. I am going to incorporate a stop movement button in my space based project. The code in charge of adding force is as follows:
rigidbody.AddRelativeTorque(pitch * turnspeed * Time.deltaTime, yaw * turnspeed * Time.deltaTime, roll * turnspeed * Time.deltaTime);
rigidbody.AddRelativeForce(0, 0, trueSpeed * speed * Time.deltaTime);
rigidbody.AddRelativeForce(strafe);
Gentlemen, anybody has the solution to make the object stop rotating using AddTorque or AddRelativeTorque? Thanks for your time!
Answer by Owen-Reynolds · Sep 06, 2013 at 01:42 PM
You could just set speed and rotation to 0: rigidbody.velocity=Vector3.zero;
and rigidbody.angularVelocity=Vector3.zero;
A little nicer might be to add fake friction: if(stopSoon) rigidbody.velocity*=0.6f;
to quickly coast to a stop (same for rotation.) I think the physics system will automatically snap it to speed 0 when it gets slow enough (or else you could.)
I try to avoid changing the velocity directly but in this case you're right, good idea.
And the friction is a great idea :) I'll add that in. Thanks mate! Just one other problem, rigidbody.angularVelocity.y = 0; gives off the error:
Cannot modify a value type return value of `UnityEngine.Rigidbody.angularVelocity'. Consider storing the value in a temporary variable.
I don't generally deal with the detailed physics.
angularVelocity as well as velocity are properties and not normal variables. Since a Vector3 is a ValueType (struct) accessing "y" of the struct will read the property "value" (which is the Vector3) and then you change the y value on thie temp value. It won't change the actual Vector3 because a Vector3 is not a reference type. You have to do what the error suggests. Use a temp variable like so:
Vector3 vel = rigidbody.angularVelocity; // reads the property
vel.y = 0;
rigidbody.angularVelocity = vel; // writes the property
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Instantiating two rigidbody at a time 0 Answers
In need of help with OnTriggerEnter 0 Answers
How do I control the speed of a rotating rigidbody? 0 Answers