Make a crosshair turn red when over an enemy

Well I know what I need to do, I’m just having trouble writing the code the way unity likes it if u kno what i mean…I want to have a crosshair that turns red when over an enemy by raycasting. But I haven’t set up the Red crosshair texture yet. Now I just don’t know how to write the script.

This is the normal crosshair script that works fine which is attached to my camera on the first person controller:

var crosshairTexture : Texture2D;

var position : Rect;

static var OriginalOn = true;

function Start()

{

    position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - 

        crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height);

}

function OnGUI()

{

    if(OriginalOn == true)

    {

        GUI.DrawTexture(position, crosshairTexture);

    }

}

Then this is my raycasting somewhat…

if(Physics.Raycast(transform.position, transform.forward, hit, 100))

but as you can see it’s not complete. Someone told me I needed to use the update function for the raycasting, but as you can see the crosshair is set up with the OnGuI and the Function Start. So that’s rly got me confused. Can someone give me a hand here? Thank you so much!

It’s better to use Raycast in Update - OnGUI is called multiple times in each Update cycle and will waste time doing unnecessary Raycasts. You should do the Raycast in Update and compare the hit.transform.tag to “Enemy” (or whatever you tag your enemies), setting a boolean flag when the result is true. In OnGUI, check this variable and change the crosshair texture to the red version when it is true - something like this:

var crosshair: Texture2D; // drag the normal crosshair here
var redCrosshair: Texture2D; // drag the red crosshair here

private var isEnemy: boolean = false;

function Update(){
    if (Physics.Raycast(transform.position, transform.forward, hit, 100)){
        isEnemy = (hit.transform.tag == "Enemy"); // assign the comparison result to is enemy
        ...
    }
}

function OnGUI(){
    var cross = crosshair; // assume normal crosshair
    if (isEnemy) cross = redCrosshair; // change to red if isEnemy is true
    GUI.DrawTexture(position, cross);
}

I did the same thing but I created it so it would turn green over allies and red over enemies. All I did what set a raycast forward from the camera and added tags to allies and enemies that were something along the lines of “Enemy” or “Ally”. Then something like this I guess:

    if(hit.point.gameObject.tag == "Enemy")
{

currentReticle = redReticle;

}
else 
{

currentReticle = regularReticle;

}

Assuming that you defined variables for the regular, red, and green textures.

You can also do something along the lines of this:

var Crosshair : GUITexture;

function Update () {

var hit: RaycastHit;

if (Physics.Raycast(transform.position, transform.forward, hit)){

var rot = Quaternion.FromToRotation(Vector3.up, hit.normal);
if (hit.transform.tag == "Enemy"){
	Crosshair.color = Color.red;
	}

else {
	Crosshair.color = Color.white;
	}
}

}

Assign this to the camera so that the raycast is sent from the camera, assuming that your crosshair is camera based. This will change the color of the crosshair GUI texture when the raycast collides with an object with the tag “Enemy”.