Keep Rigidbody upright using torque.
Im trying to make some kind off physics based fighting game. The character is a flying cube witch cube hands (see video hah).
Quaternion deltaRotation = Quaternion.Inverse(RightHand.rotation) * transform.rotation;//Transform.rotation is the rotation i want to achive
Vector3 deltaAngles = deltaRotation.ToEulerAngles();
Vector3 worldDeltaAngles = RightHand.transform.TransformDirection(deltaAngles);
float alignmentSpeed = 2f;
float alignmentDamping = 0.01f;
RightHand.AddTorque(alignmentSpeed* worldDeltaAngles- alignmentDamping * RightHand.angularVelocity);
I use this code to make the hands rotate with the body, it works well with empty hands(see green hand). But when i try to put an object in hand with larger mass than the hand itself i seem to get into problems.(hand mass = 0.5, sword mass =1). It seems like i don't have enough torque to deal with the elevated center of mass, but even if i multiply the torque a thousandfold i get the same results.
I have tried many different ways of doing this, the code above seemed to work the best.(Unarmed that is, i cant seem tho solve this problem in any way)
Too be honest, i found this code online and i dont really understand it 100%, cant really get my head around Quaternions. Im not a good programmer at all and i started playing around with unity a couple of days ago(This makes me wish i diddnt tho), so please bear with me.
If you can find i way to help me to get my cube-guy to hold his sword id be really greatfull.
Answer by HanYol0 · Aug 12, 2016 at 05:31 PM
Fixed it, diddnt know about "Physics.maxAngularVelocity". Default 7, upped it to 100 and now it works fine.