- Home /
Apply torque/force to rotate an object around a fixed point
I have a cube I want to rotate from one face to the next, 90 degrees, moving the distance of one square with each rotate. I've successfully done this with transform.RotateAround(), but I want to use physics so the cube can fall etc.
I'm attempting to do this using rigidbody.AddTorque(), but it spins the whole cube, kinda in place, as if it were spinning when it hit the ground. What I want is for the edge to seem affixed to the ground when it rotates - if enough torque were applied it should bounce back and forth keeping that edge in the same place. Is there any way to do this?
Answer by Fattie · Mar 22, 2013 at 07:59 AM
The secret is you must think: how would you do this in real life
So, how would you do it ?
I'd probably try it like this at first. A -y force on the bottom near the left edge. A force (-1,-1,0) on the left near the top edge. And I'd try to make a "catch" or something to hold the lower right edge as you pushed it, marked in mustard color on the sketch.
(Eg, maybe just add a tiny block or bar or something down there on the floor - experiment with making it disappear after a second or a certain amount of twist or whatever for different results.)
I'd probably make the first mentioned force kick in for a short time and only then add the second mentioned force. Or it could be that only the first mentioned force is necessary, but you have to make it travel with the cube, always pushing at a tangent to that face as you go but perhaps never more than horizontal as the cube goes over the top.
Its very hard to make these things look "ah hah!" when using physics, I suggest begin by thinking what you would actually do with your hands. What if the cube was about 4m tall and you were trying to do it with a group of human friends? What if you had a horse and some rope? How would you go about it?
Also you must think carefully on the size and mass of the object (use meters / KG in Unity) and the two PhysicMaterials involved.
So I did something like that in principle. I put a HingeJoint at the edge it flips over, freeze the axes not being rotated, torque it, and then remove the HingeJoint. It works for a few flips, and then the hinge ends up to low and the cube gets hung and doesn't quite rotate all the way. I'm going to change how I place my HingeJoint, by putting it between the two corners of the BoxCollider, so it is exactly where it need be.
I'll try to remember to post the basic code for this when I get it ironed out.
Thanks for the inspiration, everyone! :)