Crosshair? How?

I'm curious to know how to implement a crosshair of some sort.

Quite new to Unity so apologies XD

Cheers.

Okay, this is easy.

Make a new JavaScript, and place this into it - called it GUICrosshair:

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

Next, create a Game Empty within your scene, and drag the GUICrosshair script into it. Next find/make an image of a crosshair you want, and drag it into Unity, then into the Texture2D target.

Play the game, you will now have a GUI crosshair in the centre of the game

NOTE: if you have maximize on play, or resize your game screen whilst playing, your GUI will go all funny. This is just a bug in Unity - this will not occur when you export it as a game.

This is a simplified version of the above code in C# for anyone that finds this question. The timeScale part is only if you have a pause menu that sets timeScale to 0 and you don’t want the crosshair drawn in the menu.

    public Texture2D crosshairTexture;
    public float crosshairScale = 1;
    void OnGUI()
    {
        //if not paused
        if(Time.timeScale != 0)
        {
        	if(crosshairTexture!=null)
        				GUI.DrawTexture(new Rect((Screen.width-crosshairTexture.width*crosshairScale)/2 ,(Screen.height-crosshairTexture.height*crosshairScale)/2, crosshairTexture.width*crosshairScale, crosshairTexture.height*crosshairScale),crosshairTexture);
        	else
        		Debug.Log("No crosshair texture set in the Inspector");
        }
    }

In fact, there is aproblem in your script:

var crosshairTexture : Texture2D;
var position : Rect;
static var OriginalOn = true;

function Update() // Start will only get the screen size once. it will not refresh it. the turn around is to use function Update() instead.
{
    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);
    }
}

A more efficient solution with no code required:

  1. Create a new camera in your scene. Change the following settings on the new camera:
  2. Set depth to anything higher than your FPS camera.
  3. Set the camera Clear Flags property to “Depth Only”
  4. Create a plane and position in front of the camera and set desired scale.
  5. Drag crosshair texture from project view onto your new crosshair object.
  6. Set shader on crosshair material to something “transparent unlit”. A quick search will give you a few options.

ming shall tan so ime fal iyt gasi puij so ji fan iyt fgj uyo cgkl kgt yut opi uij. slik ij
java script:var crosshairTexture : Texture2D;
var position : Rect;
static var OriginalOn = true;

function Update() // Start will only get the screen size once. it will not refresh it. the turn around is to use function Update() instead.
{
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);
}
tan dop jlo gfd jozs shih fucj inze stroten

@script ExecuteInEditMode()
var crosshair : Texture2D;

function OnGUI () {
var w = crosshair.width/2;
var h = crosshair.height/2;
position = Rect((Screen.width - w)/2,(Screen.height - h )/2, w, h);

if (!Input.GetButton ("Fire2")) { 
	GUI.DrawTexture(position, crosshair);
}

}