- Home /
Keeping Character Upright with Addtorque
I'm working on making a character controller that is moved solely through Unity's physics engine with no artificial translation or rotation. The character is capsule shaped for ease of use, so it can rotate on the x and z axes without clipping into the floor. I am using AddTorque to keep the character upright so that the forces such as momentum and friction don't cause it to topple so that it cannot get up. This works great when the player is facing forwards (along the global z axis) or backwards. Rotation is corrected gradually so that the character stays upright. However, when the character faces sideways or even diagonally, its rotation appears to be amplified so that it begins to spin and topple (like a spinning coin), and this can only be neutralised by facing forward again, at which point it begins to level out.
#pragma strict
var walkSpeed = 10;
var rb : Rigidbody;
var uprightTorque = 5;
var right : Vector3;
var left : Vector3;
var forward : Vector3;
var backward : Vector3;
//yDefault is a gameObject that mimics the rotation of the character on the y axis but no other.
var yDefault : Transform;
function Start () {
rb = GetComponent.<Rigidbody>();
}
function Update () {
if(Input.GetKey("up")){
//We add force on the axes of yDefault because using transform.forward would cause the character to progressively
//accelerate face first towards the ground when tilted forward. The global axes would cause the character to
//accelerate forward when up is pressed regardless of where he is facing.
rb.AddForce(yDefault.transform.forward * walkSpeed);
}
if(Input.GetKey("right")){
rb.AddForce(yDefault.transform.right * walkSpeed);
}
if(Input.GetKey("left")){
rb.AddForce(yDefault.transform.right * -walkSpeed);
}
if(Input.GetKey("down")){
rb.AddForce(yDefault.transform.forward * -walkSpeed);
}
//adding torque in the opposite direction of character's rotation on x and z to counterbalance a fall.
rb.AddTorque(transform.right * -transform.rotation.x * uprightTorque);
rb.AddTorque(transform.forward * -transform.rotation.z * uprightTorque);
}
The character also has a force based mouselook from side to side (on the y axis), which appears to work fine. Increasing angular drag just slows down the process described above. I've also changed the force and torque on all the values above and more. Can anyone shed some light on why this is happening?
Try changing transform.rotation to transform.eulerAngles. EulerAngles are x, y and z rotations in degrees. Rotation is a Quaternion
Answer by Lukas-Brunner · Nov 22, 2015 at 05:51 PM
There is a section named "Constraints" in the Rigidbody component. There you could freeze the x and z rotation. EDIT: Sorry I overread that you don't want any artificial Rotation. If you don't want the rotation to be forced to zero, you could try to apply the forces in global x and z Direction with Vector3.right
and Vector3.forward
. Additionally i tried this:
var rot = Quaternion.FromToRotation(transform.up, Vector3.up);
rb.AddTorque(new Vector3(rot.x, rot.y, rot.z)*uprightTorque);
With a uprightTorque of 100 this works quite fine although the rotation is a Quaternion.
Works brilliantly. I suppose because the rb.AddTorque is fed a direct path by the values from "FromToRotation" the 'orbit' effect I was experiencing is counteracted. Can't thank you enough.
also works in 2d,
var rot = Quaternion.FromToRotation(transform.up, Vector3.up); rb.AddTorque(rot.z*uprightTorque);
Thanks man, I was stuck on this for a while. Had no idea the quaternion would work that way.
Your answer
![](https://koobas.hobune.stream/wayback/20220612045956im_/https://answers.unity.com/themes/thub/images/avi.jpg)