movement control with keyboard & mouse

I am using a 2 scripts on my main camera to let the user move around my scene, but can’t get them to work together:

This is the script for the mouse to zoom/orbit/pan:

var target : Transform;
var distance = 10.0;
var xSpeed = 250.0;
var ySpeed = 120.0;
var yMinLimit = -20;
var yMaxLimit = 80;
var zoomRate = 2;


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

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

function Start () {
    var angles = transform.eulerAngles;
    x = angles.y;
    y = angles.x;
}
   
var mover : Transform;
function LateUpdate () {
    if (!target) {return;}
   
    if (Input.GetMouseButton(2)) {
        x += Input.GetAxis("Mouse X") * xSpeed * 0.02;
        y -= Input.GetAxis("Mouse Y") * ySpeed * 0.02;
    }
       
    distance += -Input.GetAxis("Mouse ScrollWheel") * zoomRate * Mathf.Abs(distance);   
    y = ClampAngle(y, yMinLimit, yMaxLimit);

    var rotation = Quaternion.Euler(y, x, 0);
    var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
   
    transform.rotation = rotation;
    transform.position = position + mover.position;   
}

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);
} 

which actually, works in tandem with an empty game object, called “Mover”, that has this script:

function Update () {
    if (Input.GetMouseButton(1)) {
        transform.rotation = cam.rotation;
        transform.Translate(Vector3.right * -Input.GetAxis("Mouse X") * moveSpeed);
        transform.Translate(transform.up * -Input.GetAxis("Mouse Y") * moveSpeed, Space.World);
    }

but my question is, how do I get the above 2 scripts to work with the following script that uses the keyboard to move around the scene:

var speed = 5.0;
function Update () {
    var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
    var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
    transform.Translate(x, 0, z);
}

I think I understand what you are asking. Your first script manipulates the camera while the second manipulates something called ‘mover’. Your third script is attempting to manipulate the camera correct?

Your issue, from what I see, is that your third script is trying to manipulate the position of the camera via transform.Translate (which changes the object’s position). Both scripts are fighting to change the same transform.

Here:
“transform.Translate(x, 0, z);” is being update at UPDATE

“transform.position = position + mover.position;” is being updated every late update which happens AFTER update which over writes UPDATE’s changes so you never get the changes done in UPDATE.

Here’s my idea how to solve this. It can be done in your first script and eliminates the need for the third:

var scrollScreen : boolean = false;

// Pick a scripted modifier or keypress to switch scrollScreen between true and false

if(scrollScreen) {

var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
transform.Translate(x, 0, z);

} else {transform.position = position + mover.position;}

It totally helped, thank you so much. And many apologies for my late reply. Yes, I ended up using the CapsLock key as the scrollScreen activator, but using GetKeyDown (which has to be called in the Update function) so that the script looks like this:

var mover : Transform;
var scrollScreen :boolean = false;

function Update () {

	if (!target) {return;}
	
	var speed = 5.0;
	var rotation = Quaternion.Euler(y, x, 0);
    var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
	
	if(Input.GetKeyDown(KeyCode.CapsLock)){
      scrollScreen = !scrollScreen ;
	}
	
	if(scrollScreen){
            var x = Input.GetAxis("Horizontal") * Time.deltaTime * speed;
            var z = Input.GetAxis("Vertical") * Time.deltaTime * speed;
            transform.Translate(x, 0, z);
			} else {
            transform.rotation = rotation;
            transform.position = position + mover.position;
			}
}

Please note: The above is not the entire script, just the revised portion that is being discussed in this posting. The rest of the script (can post upon request) is tweaked to exclude from the LateUpdate function those commands which are now included in the Update function.