Creating A Crosshair in Unity 4.6.2?

Hi, I am making a game and I need to create a crosshair for the player. I looked at tutorials online, and they are all saying to use GUITexture. Is GUITexture now outdated due to the GUI updates that came out? I ask this because I cannot get this method to work. If anyone can help, that would be great.

GUITexture is outdated, but can still be used.

Is there a specific reason you want to use GUITexture, or is it just because that’s what you’ve found online?

If I were you I would look into Unity’s new GUI tools. They’re pretty awesome. You can easily make a crosshair that is perfectly centered on all devices this way.

Just import the image you want to use as a crosshair and set its texture type to sprite.

Then, in the menu bar, go to GameObject>UI>Image. This will automatically create a new canvas for a UI which is just a place for your UI elements, and place an Image on it. The image will appear as a white box, exactly centered on the screen.

Select the image either in the scene view or the hierarchy view and drag your imported crosshair sprite into its Source Image field in the inspector (or you can click the little circle next to Source Image and select your crosshair from the selection menu that appears).

Next, all you have to do is ensure that the crosshair has appropriate transparency (depending on the state in which the image was when you imported it), adjust its alpha levels as you wish, resize it to look the way you want (just don’t move it around, or it won’t be an accurate indicator of where your shots will go, depending on how you’re shooting) and voila! Easy crosshair in just a few minutes.

You can also easily change the crosshair during gameplay to correspond to different weapons through code if desired, or you can hide the crosshair altogether if need be.

Also (and this goes a little beyond what you were asking), if you’re making a First-Person game, casting a ray from the main camera’s position straight forward will line your aim up with your crosshair.

The code for this looks like:

Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, wpnRange);

Where hit should be replaced with a RaycastHit object, and wpnRange should be replaced by the range of your weapon, or Mathf.Infinity if your weapon has infinite range.

Hopefully this helps…

I used

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()
{
Screen.lockCursor = true;
Screen.showCursor = false;
if(OriginalOn == true)
{
GUI.DrawTexture(position, crosshairTexture);
}
}

Thanks for the answer!

I converted to C# if anyone wanted it in C# rather than JavaScript.

using UnityEngine;
using System.Collections;

public class Crosshair : MonoBehaviour 
{
	public Texture2D crosshairTexture; 
	public Rect position; 
	static bool OriginalOn = true;

	void Start() 
	{
		position = new Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height);
	}

	void OnGUI() 
	{
		Screen.lockCursor = true; Screen.showCursor = false; 

		if(OriginalOn == true) 
			GUI.DrawTexture(position, crosshairTexture); 
	}
}