Another EDITOR style camera code

Hi all

I’ve searched the forums and tried a number of codes but have come up dry for this particular problem(s). I am happy with the code for my camera movement, except for the PAN part. If I pan and then try to orbit , the camera snaps back to it’s previous position. It doesn’t do so when I zoom after panning, and it doesn’t do so if I orbit after zooming. So what I need is for the centre of orbit to move from the object on which I was focused to wherever I pan to. The distance doesn’t seem to be an issue.

Secondly, I would like to use a clutch key (eg space bar) plus mouse buttons to control camera movement. This obviously require mouse clicks. Is there a way to disable all other scripts which require mouse clicks while the space bar is pressed? Thanks for all the help.

static var target : Transform;
static var distance;

var panSpeed = 10; 

var xSpeed = 250.0;
var ySpeed = 120.0;

var yMinLimit = -20;
var yMaxLimit = 80;

private var x = 0.0;
private var y = 0.0;
private var z = 0.0;

@script AddComponentMenu("Camera-Control/Mouse Orbit")

function Update () {
			//Orbit
    if (target) {
    	if (Input.GetKey ("left shift")) {
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
 		
 		y = ClampAngle(y, yMinLimit, yMaxLimit);
 		
        var rotation = Quaternion.Euler(y, x, z);
        var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
        
        transform.position = position; 
      	transform.rotation = rotation;
    	}
    	
 			//Pan
     if (Input.GetKey(KeyCode.Space)) 
        { 
        transform.Translate(transform.right * -Input.GetAxis("Mouse X") * panSpeed, Space.World); 
        transform.Translate(transform.up * -Input.GetAxis("Mouse Y") * panSpeed, Space.World); 
        } 
        	//Zoom
     if (Input.GetKey(KeyCode.LeftAlt)) 
        { 
        transform.Translate(transform.forward * -Input.GetAxis("Mouse X") * panSpeed, Space.World); 
        } 
	}
}

static function ClampAngle (angle : float, min : float, max : float) {
	if (angle < -360)
		angle += 360;
	if (angle > 360)
		angle -= 360;
	return Mathf.Clamp (angle, min, max);
}

[/quote]

I believe your problem is that your orbit position does not take in to account the camera’s current position (which changes after a pan or zoom). The orbit is only considering a rotation around the target position at a set distance (which also appears to be a problem because you are not recalculating the distance if the camera gets further away).

Because of this, the camera position is reset to be relative to the target and distance every time you press left shift.

I hope that helps point you in the right direction.

Thanks Jmcclure

I sort of know what the problem is, it’s how to solve it that’s the issue. The zoom works fine, because it works in conjunction with a slightly modded SmoothLookAt script. So the object set as the target is focused in the centre of the screen and zoom only makes the camera closer or further away while the centre point for orbit is the object’s transform. The problem is with pan because the centre point remains the objects transform while the camera moves “off center”.

Ai, there’s the rub.

glebski -

I’m not sure what behavior you’re expecting from pan. Without breaking the camera’s dependency on the target, pan would behave the same as orbit. The camera moving right, maintaining the same distance from the target will have to rotate around the target.

If you want pan behavior, you have to make the camera’s facing direction independent of the target so that the camera doesn’t rotate. Either that or you have to pan the target too (which is what the panning camera on the wiki does).

I guess I didn’t mean the same distance from the object, because you’re right, that would mean orbit. I want a pan behavior from the pan, just like you see in the editor, or in Maya. The camera doesn’t snap back to the object in those programs. Say you click on an object and click the “F” key to focus on it, then if you pan the focus is automatically lost, but the distance remains the same, meaning, one doesn’t get closer or further away, the camera moves in one plane. That’s what I’m after… at least until I select another object and the SmoothLookAt will take over again.

Thanks