Search code examples
unity-game-enginerotationgame-physicsphysicsquaternions

Rotate rigid body in one direction with AddTorque


I have a main camera that rotates around an object (which I already move with AddForce). Using AddTorque, I would like the object to rotate its Y axis of rotation in the direction in which the camera's Y axis of rotation points so as to simulate the rotation of a person turning around. To do this I thought of using a force that gave me 0 when the Y axes of the two objects are the same, but I tried and the rigidbody covers only one part of the rotation of the camera while the other part is as if it not is detected.

   var currentR = rb.rotation.y;
 var targetR = Camera.main.transform.rotation.y;
 rb.AddTorque(transform.up * 1000f * (targetR - currentR));

rotation my object desired rotation


Solution

  •     Vector3 camForward = Camera.main.transform.forward;
    Vector3 rbForward = rb.transform.forward;
    
    Vector3 torque = Vector3.Cross(camForward, rbForward);
    rb.AddTorque(torque);