- Home /
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.
Answer by jakejolli · Feb 21, 2015 at 11:42 PM
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..
Thanks for the help! I did not really want to use GUITexture, I just saw that all the tutorials online were using it, they were obviously outdated. Thanks for all the information, I will be sure to implement them. :)
how would you change the position of the crosshair with Raycast? I haven't used raycast much before but I thought it returned a bool
Answer by gkepets · Feb 21, 2015 at 12:39 AM
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); } }
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); }
}
Answer by Smithy311 · Feb 21, 2015 at 09:53 PM
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);
}
}
Your answer
Follow this Question
Related Questions
Smart Crosshair 4 Answers
Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer
Simple GUI In game 1 Answer
My GUITexture looks awful 2 Answers