My Scope Script

Well I am using a gun that when you hit Alt the screen zooms in for a scope. It works fine. But I also have a cross hair for when you are not using the scope. When you zoom in with the Alt key the cross hair gets mixed up with the gun scope and looks bad. Iam looking for a attachment to this script that will help me make the crosshair go away when I use my gun scope and come back when I am not using it. Thanks.

The Crosshair Script-

var crosshair01 : GUITexture;

var FpsPlayer;



function Start() {   

     FpsPlayer = GameObject.FindWithTag("Player"); 

}



function OnMouseOver () { 	

	var targetDistance = Vector3.Distance(FpsPlayer.transform.position, transform.position); 

        if(targetDistance < 12.0){

        crosshair01.guiTexture.color = Color(1,0,0,0.5);

    }

}



function OnMouseExit () {

    	crosshair01.guiTexture.color = Color(1,1,1,0.5); 

}

The Gun Scope Script-

var gun : Transform;

 var nextPos = 0.0;

 var nextField = 40.0;

 var nextPos2 = -0.2;

 var dampVelocity = 0.4;

 var dampVelocity2 = 0.4;

 var dampVelocity3 = 0.4;

 var FPSWalker : float;

 

 function Update () {

    var newPos = Mathf.SmoothDamp(gun.transform.localPosition.x, nextPos, dampVelocity, .3);

    var newField = Mathf.SmoothDamp(Camera.main.fieldOfView, nextField, dampVelocity2, .3);

    var newPos2 = Mathf.SmoothDamp(gun.transform.localPosition.y, nextPos2, dampVelocity3, .3);

    

    gun.transform.localPosition.x = newPos;

    gun.transform.localPosition.y = newPos2;

    Camera.main.fieldOfView = newField;

    

    if (Input.GetButton("Fire2")) {

        //adjust viewpoint and gun position

        nextField = 40.0;

        nextPos = -0.06;

        nextPos2 = -0.2;

        

        //slow down turning and movement speed

        GetComponent(CharacterMotor).Movement.maxForwardSpeed = 1.5;

        GetComponent("MouseLook").sensitivityX = 2;

        camera.main.GetComponent("MouseLook").sensitivityX = 2;

        camera.main.GetComponent("MouseLook").sensitivityY = 2;

    } else {

        //adjust viewpoint and gun position

        nextField = 60.0;

        nextPos = 0.5;

        nextPos2 = -0.4;

        

        //speed up turning and movement speed

        GetComponent(CharacterMotor).Movement.maxForwardSpeed = 6;

        GetComponent("MouseLook").sensitivityX = 6;

        camera.main.GetComponent("MouseLook").sensitivityX = 6;

        camera.main.GetComponent("MouseLook").sensitivityY = 6;

    }

 }

Thanks,

Nick :sunglasses:

This is kind of a mix of both of the scripts that you have. I am not entirely happy with the GUITexture object. I guess I like more control. Here is the same scripts with the Crosshair embedded into the gun script. This allows you to show, or not show the crosshair as needed, and I even put in the mouse in the crosshair rect as you had it.

var gun : Transform;
var crosshair : Texture2D;
var nextPos = 0.0;
var nextField = 40.0;
var nextPos2 = -0.2;
var dampVelocity = 0.4;
var dampVelocity2 = 0.4;
var dampVelocity3 = 0.4;
var FPSWalker : float;
private var FpsPlayer : GameObject;
private var showCrosshair : boolean;
private var crosshairColor : Color = Color(1,1,1,0.5);
private var rect;



function Start() {   
	FpsPlayer = GameObject.FindWithTag("Player");
	SetScreenRect();
}

function Update () {
	var newPos = Mathf.SmoothDamp(gun.transform.localPosition.x, nextPos, dampVelocity, .3);
	var newField = Mathf.SmoothDamp(Camera.main.fieldOfView, nextField, dampVelocity2, .3);
	var newPos2 = Mathf.SmoothDamp(gun.transform.localPosition.y, nextPos2, dampVelocity3, .3);

	gun.transform.localPosition.x = newPos;
	gun.transform.localPosition.y = newPos2;
	Camera.main.fieldOfView = newField;

	if (Input.GetButton("Fire2")) {
		//adjust viewpoint and gun position
		nextField = 40.0;
		nextPos = -0.06;
		nextPos2 = -0.2;

		//slow down turning and movement speed
		GetComponent(CharacterMotor).Movement.maxForwardSpeed = 1.5;
		GetComponent("MouseLook").sensitivityX = 2;
		camera.main.GetComponent("MouseLook").sensitivityX = 2;
		camera.main.GetComponent("MouseLook").sensitivityY = 2;
		
		showCrosshair = false;
	} else {
		//adjust viewpoint and gun position
		nextField = 60.0;
		nextPos = 0.5;
		nextPos2 = -0.4;

		//speed up turning and movement speed
		GetComponent(CharacterMotor).Movement.maxForwardSpeed = 6;
		GetComponent("MouseLook").sensitivityX = 6;
		camera.main.GetComponent("MouseLook").sensitivityX = 6;
		camera.main.GetComponent("MouseLook").sensitivityY = 6;
		
		showCrosshair = true;
	}
	SetScreenRect();
	crosshairColor = Color(1,1,1,0.5);
	if (rect.Contains(Input.mousePosition)) crosshairColor = Color(1,0,0,0.5);
}

function OnGUI () {
	if(showCrosshair){
		GUI.color = crosshairColor;
		GUI.DrawTexture(rect, crosshair, ScaleMode.ScaleToFit, true, 0.0f);
	}
}

function SetScreenRect(){
	var hScreen = new Vector2(Screen.width, Screen.height) / 2;
	var hTex = new Vector2(crosshair.width, crosshair.height) / 2;
	hScreen -= hTex;
	rect = new Rect(hScreen.x, hScreen.y, hTex.x, hTex.y);
}