Some several months ago, I made a much visited tutorial about rotating, panning, and zooming a camera in Unity3d. I was asked if it was possible to make the camera movement more smooth. While I thought the movement was already smooth, no one can ignore the fact that if you let go of mouse when moving the camera, it will suddenly stop.
A more desired behavior is for the camera to smoothly stop after letting go of the mouse. In order for this to work, the camera needs to have some inertia, thus physics properties.
In the link above with the revised camera script, I implemented a version of camera movement that will stop smoothly. You can even control this smoothness from the pubic variables
Internally, a RigidBody component would be required for this to work, but I made the script take care of that, so that you don’t have to. In other words, if you’re using this script on a camera, do NOT add a RigidBody component because the script will do that automatically.
The only problem it has at this point is that it can no longer re-orientate its up-vector. What this means is that the more you rotate the camera, it may be more likely to be tilted. I tried to find a way to fix this, but couldn’t. Feel free to comment if you found a way. If that’s not the behavior you want, then you’re much better off using the old camera movement script. Just take note that it don’t stop smoothly.
Concerning the implementation, you replace calls to
transform.RotateAround() with calls to
rigidBody.AddTorque(). The parameter aren’t quite the same, but you can compare the two scripts if you want. When asking for a ForceMode, I chose
Acceleration because I did not want to factor mass into the movement of the camera (you shouldn’t have to tweak that).
There’s a little caveat with panning. In the old script, panning could be done in the camera’s local space automatically, but has to be done manually in this new implementation.
To do that, you need a Quaternion to represent the forward orientation of the camera. This can be done like:
Quaternion forwardRotation = Quaternion.LookRotation(transform.forward, transform.up);
Then you apply the quaternion upon the
move vector like so:
move = forwardRotation * move;
Then you use
AddForce(). See the new script linked above for implementation details.