How to make crosshair turn red when it targets a enemy

Im having problems in making the Crosshair turn red when it on a enemy? Can anybody give me some tips or a script I can use

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

You can use Physics.Raycast to achive this, send a ray from your cam.transform towards your Crosshair and if you hit an enemy turn it red.

As an example, you could use the following on your Crosshair or someplace for gemeral scripts, the main diff is that you only add this once, you need to tag your enemies, while in your idea, you must add that to all enemies just to check if mouse hovers over it.

  void Update() {
GameObject cam = Camera.mainCamera;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit OutHit;
if (Physics.Raycast(cam.transform.position, fwd, OutHit))
 if(OutHit.transform.tag == "Enemy"){
crosshair01.guiTexture.color = Color(1,0,0,0.5);
}
else{
crosshair01.guiTexture.color = Color(1,1,1,0.5);
}
    }
}

this code might be wrong, i did it without testing nor checking syntax, but it might help you out, good luck !

Nik

You can use Physics.Raycast to achive this, send a ray from your cam.transform towards your Crosshair and if you hit an enemy turn it red.

As an example, you could use the following on your Crosshair or someplace for gemeral scripts, the main diff is that you only add this once, you need to tag your enemies, while in your idea, you must add that to all enemies just to check if mouse hovers over it.

  void Update() {
GameObject cam = Camera.mainCamera;
Vector3 fwd = transform.TransformDirection(Vector3.forward);
RaycastHit OutHit;
if (Physics.Raycast(cam.transform.position, fwd, OutHit))
 if(OutHit.transform.tag == "Enemy"){
crosshair01.guiTexture.color = Color(1,0,0,0.5);
}
else{
crosshair01.guiTexture.color = Color(1,1,1,0.5);
}
    }
}

this code might be wrong, i did it without testing nor checking syntax, but it might help you out, good luck !

Nik

Here is a rewrite of your code that should work. You have several different things going on here. First your crosshair is in GUI coordinates. They start in the upper left of the screen. Second transform.position is the position of the game object you have this script attached to, can be anywhere, and has no relationship to the crosshair. So to do the raycast using your existing logic, you have to convert GUI Coordinates → Screen Coordintates → World Coordinates. Then you can raycast:

var crosshair: Texture2D; // drag the normal crosshair here
var redCrosshair: Texture2D; // drag the red crosshair here
var position :Rect;
 
private var isEnemy: boolean = false;

function Update(){
	var hit: RaycastHit;
	var v3Pos : Vector3 = position.center;  // Get GUI Coordinates
	v3Pos.y = Screen.height - v3Pos.y;      // Convert to Screen Coordinates
	var ray : Ray = Camera.main.ScreenPointToRay(v3Pos);  // Make Ray in World Coordinates.
    if (Physics.Raycast(ray, hit, 100)) {
        isEnemy = (hit.transform.tag == "Enemy"); // assign the comparison result to is enemy
 
    }
    else
    	isEnemy = false;
}
 
function OnGUI(){
    var cross = crosshair; // assume normal crosshair
    if (isEnemy) cross = redCrosshair; // change to red if isEnemy is true
    GUI.DrawTexture(position, cross);
}