Trying to create a specific camera rotation behavior

Hi All

I’m trying to get something to work but it is eluding me…

Basically, I want to have a fixed camera that, when the right mouse button is held down, looks around. I can achieve this, however the issue arises with the initial camera rotation. In my script, as soon as I hit the right mouse button, the camera snaps to 0,0,0 rotation, and then allows the user to look around.

My desired workflow is:

  1. Set camera rotation manually by rotating the camera object in the editor
  2. Hit play, and then when the right mouse button is held down, allows the view to be rotated smoothly from the initial manual rotation

This script needs to be added to more than one camera, hence the need to be able to set an initial rotation in the editor.

My script is a modified version of the camera fly script from the wiki:

var currentCamera : Camera;
var lookSpeed = 3;
var rotationX;
var rotationY;

function Start ()
{
	rotationX = currentCamera.transform.rotation.x;
	rotationY = currentCamera.transform.rotation.y;
}

function Update ()

{
	if (Input.GetMouseButton(1))
	{

	    rotationX += Input.GetAxis("Mouse X")*lookSpeed;
	    rotationY += Input.GetAxis("Mouse Y")*lookSpeed;
	    rotationY = Mathf.Clamp (rotationY, -90, 90);
	    
	    transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
	    transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
		
	}
	
}

Can anyone help out with this? I think my issue is that I don’t know how to ‘trap’ the initial manual rotation and then feed it into the rotation calculation.

Thanks a lot!

Replace with this code:

        rotationX = Input.GetAxis("Mouse X")*lookSpeed;
        rotationY = Input.GetAxis("Mouse Y")*lookSpeed;
        rotationY = Mathf.Clamp (rotationY, -90, 90);
        currentCamera.transform.Rotate(Vector3(rotationY,rotationX,0));

Thanks Batigol… this almost works.

Now, the problem is that it doesn’t stay level on the horizontal plane… despite the fact that you specify 0 on the z rotational axis, it is still changing the z rotation and thus producing funky viewing angles.

I’ll need to comprehend the code a bit more to figure out why it is still altering the z axis despite the code explicitly telling it not to.

Thanks though, much appreciated!

EDIT: I just realised that it might clarify things to say that I want my camera to behave just like the editor camera in terms of rotating with the RMB down.

I figured it out by adapting another script… if anyone is curious here it is:

var sensitivity : float = 2;
var upAndDownExtents : float = 80;

private var rotationY : float;
    
function Start ()
{
    rotationY = ((transform.eulerAngles.x)*-1);
}

function Update ()
{
	if (Input.GetMouseButton(1))
	{
        var rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivity;
        rotationY += Input.GetAxis("Mouse Y") * sensitivity;
        rotationY = Mathf.Clamp (rotationY, (upAndDownExtents*-1), upAndDownExtents);
        transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
	}
}