I am creating a custom camera to view a 3D object. The camera is normally a fly cam but on drag of an object, I want the camera to orbit the object. I have achieved this already but the camera jumps between rotations when swapping between the modes reseting to the previous rotation value. How would I smooth this so the camera maintains the same rotation after orbit and the user can continue on the fly cam mode from this rotation. Here is my code:
rotationX += Input.GetAxis("Mouse X") * cameraSensitivity * Time.deltaTime;
rotationY += Input.GetAxis("Mouse Y") * cameraSensitivity * Time.deltaTime;
rotationY = Mathf.Clamp(rotationY, -90, 90);
transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
if (Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
cameraSpeed = 150f;
cameraSpeed = 50f;
transform.position += transform.forward * cameraSpeed * Input.GetAxis("Vertical") * Time.deltaTime;
transform.position += transform.right * cameraSpeed * Input.GetAxis("Horizontal") * Time.deltaTime;
rotationX = Input.GetAxis("Mouse X") * RotateAmount;
rotationY = Input.GetAxis("Mouse Y") * RotateAmount;
Vector3 angles = transform.eulerAngles;
angles.z = 0;
transform.eulerAngles = angles;
transform.RotateAround(rotationTarget.position, Vector3.up, rotationX);
transform.RotateAround(rotationTarget.position, Vector3.left, -rotationY);
rotateToggle is true when the user is dragging the object and rotationTarget is the orbit target's transform.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
3 People are following this question.
Creating a Window - Camera movement
How do I stop the camera from going farther away while I am moving the player and rotating the camera at the same time?
Rotate Camera in Y axis without visibly changing angle in Z axis
Recognize when ever camera looks up and turns back down
camera woobling when i move it around player