The problem is that Unity saves orientation data of previous camera. So when I switch to other camera the script works (camera get 0, 0, 0 orientation). But when I’m starting to drag view with my mouse (try to look around), it jumps to the first camera’s finish position.
It look like all cameras on the scene are connected to each other. So when I rotate with one camera, the movement applies automatically to the rest of them.
The aim is to reset camera orientation, so when I switch camera and start drag (look) with my mouse it wouldn’t jump.
As for the problem you describe, there’s no reason the orientations of the camera objects would be tied to each other unless you have them set up that way. Do you perhaps have a player control script attached to each camera that’s always active for all camera objects? (That is, it’s responding to user input and changing the orientation of the object even when the camera itself is disabled?)
thank you for your reply and for “Quaternion.identity” feature.
You are right, I have a Unity’s default MouseLook script on both cameras. Do you have any ideas, please on how I can solve my problem (MouseLook script on both cameras, reset to defaul rotation after camera switched)?
Here is the default MouseLook code. I’m not very much familiar with Quaternion function. I do understand, that it takes some data from mouse movements and uses it.
using UnityEngine;
using System.Collections;
/// MouseLook rotates the transform based on the mouse delta.
/// Minimum and Maximum values can be used to constrain the possible rotation
/// To make an FPS style character:
/// - Create a capsule.
/// - Add a rigid body to the capsule
/// - Add the MouseLook script to the capsule.
/// -> Set the mouse look to use LookX. (You want to only turn character but not tilt it)
/// - Add FPSWalker script to the capsule
/// - Create a camera. Make the camera a child of the capsule. Reset it's transform.
/// - Add a MouseLook script to the camera.
/// -> Set the mouse look to use LookY. (You want the camera to tilt up and down like a head. The character already turns.)
[AddComponentMenu("Camera-Control/Mouse Look")]
public class MouseLook : MonoBehaviour {
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
public RotationAxes axes = RotationAxes.MouseXAndY;
public float sensitivityX = 15F;
public float sensitivityY = 15F;
public float minimumX = -360F;
public float maximumX = 360F;
public float minimumY = -60F;
public float maximumY = 60F;
float rotationX = 0F;
float rotationY = 0F;
Quaternion originalRotation;
void Update ()
{
if(Input.GetMouseButton(1)){
if (axes == RotationAxes.MouseXAndY)
{
// Read the mouse input axis
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * xQuaternion * yQuaternion;
}
else if (axes == RotationAxes.MouseX)
{
rotationX += Input.GetAxis("Mouse X") * sensitivityX;
rotationX = ClampAngle (rotationX, minimumX, maximumX);
Quaternion xQuaternion = Quaternion.AngleAxis (rotationX, Vector3.up);
transform.localRotation = originalRotation * xQuaternion;
}
else
{
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
rotationY = ClampAngle (rotationY, minimumY, maximumY);
Quaternion yQuaternion = Quaternion.AngleAxis (rotationY, Vector3.left);
transform.localRotation = originalRotation * yQuaternion;
}
}
}
void Start ()
{
// Make the rigid body not change rotation
if (rigidbody)
rigidbody.freezeRotation = true;
originalRotation = transform.localRotation;
}
public static float ClampAngle (float angle, float min, float max)
{
if (angle < -360F)
angle += 360F;
if (angle > 360F)
angle -= 360F;
return Mathf.Clamp (angle, min, max);
}
}
You have the right idea with resetting the transform’s rotation; all you need to do is assign Quaternion.identity to transform.rotation at the appropriate time.
Actually, quaternions (and Unity’s quaternion functions) have nothing to do with mouse movement.