Conflict between scripts

I am trying to create 2 way of navigating in the runtime scene: the first with a gui interface and the second one with the mouse… but the two seems not to work together
Why the first script only works if the second one is switched off?(it actually return the print function but does not move the camera :face_with_spiral_eyes: )

var zoomin: Texture2D;


function OnGUI () {

if (GUI.Button (Rect(0,Screen.height -64,64,64),  zoomin)){
	
	Camera.main.transform.Translate(1,1,1);
		print ("you clicked the zoomin icon");
	}
var target : Transform;
var edgeBorder = 0.1;
var horizontalSpeed = 360.0;
var verticalSpeed = 120.0;
var minVertical = 20.0;
var maxVertical = 85.0;
var minDistance = 1; //Min distance of the camera from the target
var maxDistance = 20;

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

function Start(){
	x = transform.eulerAngles.y;
    y = transform.eulerAngles.x;
    distance = (transform.position - target.position).magnitude;
}

function LateUpdate(){

   //Zooming with mouse
   distance += Input.GetAxis("Mouse ScrollWheel")*distance;
   distance = Mathf.Clamp(distance, minDistance, maxDistance);

	var dt = Time.deltaTime;
	x -= Input.GetAxis("Horizontal") * horizontalSpeed * dt;
	y += Input.GetAxis("Vertical") * verticalSpeed * dt;
	
	y = ClampAngle(y, minVertical, maxVertical);
	 
	var rotation = Quaternion.Euler(y, x, 0);
	var position = rotation * Vector3(0.0, 0.0, -distance) + target.position;
	
	transform.rotation = rotation;
	transform.position = 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);
}

Well, the second one constantly overrides the transform :slight_smile: Try applying the changes only if the user is holding the left button… or do nothing if tthe scroll wheel is zero :wink:

Thanks a lot for the advice however I think would be better to stop the second script when the first gets executed(on a push button) this is because the controls are a combination of mouse and keyboard (sorry forgot to metion before) is there a way to do this?

yes, just use a Finite State Machine or a global flag to tell the second script that it shall not apply the transform. Having the code in separate scripts (why so?) the best way is the FSM :wink:

The only reason I had it in a separete script is because the first is attached to an empty game object of my gui interface, and the second one is attached to the camera directly, should it be not like this?
Can you give me an hint about FSM with separete scripts? I am not a very good programmer

Well, if you are not a programmer it’s difficult to explain the FSM concept on a forum, there are axtensive explanations around, starting on Wikipedia… that maybe the best place to start with, just google for Finite State Machine :wink:

My advice is to merge those two scripts so you can use a class boolean flag to disable the one in the Update while the GUI has taken over.