- Home /
Crosshair? How?
I'm curious to know how to implement a crosshair of some sort.
Quite new to Unity so apologies XD
Cheers.
Answer by oliver-jones · Nov 25, 2010 at 11:09 PM
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.
Okay, when your happy - mark this post as correct :)
"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."
That's not a bug, just change function Start() to function Update() so it checks every frame for screen size.
So far people have proposed the rect calculation being either in Start() or in Update(). The first happens once, but has the side effect of not functioning properly with resizing. There isn't a "OnScreenResize()" function, but you can create your own checks in update, and then call a your own function to recalculate it whenever the checks come back with changes. This has the benefit of only recalculating when the window has actually resized, at a $$anonymous$$or cost of two float comparisons every frame.
public class GUICrosshair : $$anonymous$$onoBehavior
{
Texture2D m_CrosshairTex;
Vector2 m_WindowSize; //$$anonymous$$ore like "last known window size".
Rect m_CrosshairRect;
void Start () {
m_CrosshairTex = new Texture2D(2,2);
m_WindowSize = new Vector2(Screen.width, Screen.height);
CalculateRect();
}
void Update () {
if(m_WindowSize.x != Screen.width || m_WindowSize.y != Screen.height)
{
CalculateRect();
}
}
void CalculateRect()
{
m_CrosshairRect = new Rect( (m_WindowSize.x - m_CrosshairTex.width)/2.0f,
(m_WindowSize.y - m_CrosshairTex.height)/2.0f,
m_CrosshairTex.width, m_CrosshairTex.height);
}
void OnGUI() { /* Gui call referenced above) */
}
Answer by $$anonymous$$ · Feb 26, 2014 at 11:53 PM
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");
}
}
@WhoTnt Your script it's much more simplified and works pretty well except for the positioning. You forgot to multiply the crosshair scale, here is the fix: 'GUI.DrawTexture(new Rect((Screen.width-crosshairTexture.width*crosshairScale)/2 ,(Screen.height-crosshairTexture.height*crosshairScale)/2, crosshairTexture.width*crosshairScale, crosshairTexture.height*crosshairScale),crosshairTexture);'
@Bruno Tavares Damn, that was a stupid mistake. I've updated the code. Thanks
Answer by Lancemaker_ · Nov 14, 2011 at 01:12 AM
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); } }
Answer by chief1234 · Nov 14, 2011 at 04:48 AM
A more efficient solution with no code required:
Create a new camera in your scene. Change the following settings on the new camera:
Set depth to anything higher than your FPS camera.
Set the camera Clear Flags property to "Depth Only"
Create a plane and position in front of the camera and set desired scale.
Drag crosshair texture from project view onto your new crosshair object.
Set shader on crosshair material to something "transparent unlit". A quick search will give you a few options.
Im assu$$anonymous$$g you mean parent a plane with the FPS camera, this will only work if he wants a FPS crosshair. If he wants a free moving cursor or more control over it the above way is better.
Answer by JJOBa · Mar 02, 2013 at 02:13 PM
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
Your answer
Follow this Question
Related Questions
Keep player on surface 0 Answers
Make that muzzle flash? How? 1 Answer
Bouncy platforms/spring jump? 4 Answers
How to make crosshairs transparent? 0 Answers