- Home /
Rigidbody rotating around a point instead on self
Hi, How can i rotate a rigidbody around an arbitrary point instead around itself by applying a force on it? inertiaTensor can help? Thanks in advance -frens
Answer by Ehren · Jan 13, 2010 at 06:27 PM
Here are a few suggestions:
- Use the animation system
- Attach a joint to the rigidbody and anchor it at the pivot point
- Change the rigidbody's center of mass
Answer by Sandy Gifford · Mar 21, 2015 at 06:50 PM
So it may be a little late to help your problem specifically, but if anyone else comes by this question: Using RigidBody.MovePosition, RigidBody.MoveRotation, and the mysterious yet surprisingly helpful Quaternion, this can be achieved relatively easily:
public void rotateRigidBodyAroundPointBy(Rigidbody rb, Vector3 origin, Vector3 axis, float angle)
{
Quaternion q = Quaternion.AngleAxis(angle, axis);
rb.MovePosition(q * (rb.transform.position - origin) + origin);
rb.MoveRotation(rb.transform.rotation * q);
}
Build a
Quaternion
using the provided angle, and the axis around which we would like to rotateTranslate the point back to the origin (point around which we are rotating) multiply it by the quaternion to rotate it around the desired axis, translate it back, and use the resulting point as a target for
Rigidbody.MovePosition
Set the object's rotation with
Rigidbody.MoveRotation
by multiplying the current rotation by the new (multiplication is like addition for quaternions, I couldn't tell you why).
Nice solution, without the need to use an empty parent object. It works perfect even when pivot point moves every frame. I changed Quaternion.AngleAxis for Quaternion.Euler and i pass a full Vector3 rotation ins$$anonymous$$d of an axis and an angle and it works fine. Still I dont know why it does not move the rigidbody when you use the $$anonymous$$ovePosition, I guess it's the operation inside with the quaternion and the $$anonymous$$overotation after that what makes it fixed. But I'm not sure, as you said, quaternions are complex to understand. This said, I think the reason why you multiply a real number by a quaternion to add rotations is because quaternions are 4 numbers, the first one is a real number and the other 3 are multiplied by imaginary identities (i, j, k where i² = j² = k² = -1). So a quaternion is (a + b•i + c•j + d•k), where a is the real part and the vector (bi, cj, dk) the imaginary part. To stay in a 3D environment the real part (a) is equal to zero, so you get a 3D vector of the form (bi, cj, dk). Now if you use a vectorial product of x axis by y axis you get z axis. Those are 90º each, but in general you get the axis of the plain the members of the product are in, and this is particulary useful for rotations. Actually if you want to rotate an horizontal table only horizontally in the x and z, you rotate by the y axis, isn't this counter intuitive? It's not when you think about the axis of the rotation, not the direction. This is why quaternions are used, plus euler angles can't express every full rotation, due to an axis rotation when you are rotating the other angles too becomes restricted to certain planes. Actually if you want to rotate a sphere in its 3 axis at the same time by incrementing all its 3 components x, y and z, every time an axis rotates 180 degrees makes the other axis to turn back their rotation directions. You can try in unity. It happens. Well, I was trying to clarify this but I think I made it more complex, and that's because I don't understand quaternions in an intuitive manner plus my english is not very good. Sorry for that but I leave this comment anyway, maybe someone finds it useful.
Answer by EggQuiz857 · Mar 21, 2015 at 12:50 PM
It is cause the it will rotate around its anchor point so try to move the anchor point the the place you need it. hope that helped
Your answer
Follow this Question
Related Questions
Question about topdown-movement and mouse-facing-foward-check 0 Answers
Rigidbody makes NavmeshAgent stutter in its movement 2 Answers
How to stop MoveRotation when rigidbody is facing a specific euler angle 2 Answers
limiting rotation of a rigidbody in C# 3 Answers
Smooth Player Ball Rolling 1 Answer