- Home /
C# Crosshair
I am trying to add a Crosshair to my mouse look on my FPS using C#. I am totally clueless on how to do this, I been searching and found some java script tutorials, but I don't know java script or have any idea how exactly the crosshair works in a game.
Can someone please help me? I know I didn't put any code, but really I am clueless about this.
Answer by syclamoth · Jan 02, 2012 at 08:53 AM
Why don't you just do something really, really simple. There are a couple of things, but they depend on exactly what you want to achieve.
First up, make a crosshair image in your pixel manipulator of choice. Import into your project, preferably with some kind of transparency so that you can see through parts of it.
Then, in your C# file put the line
public Texture2D crosshairImage;
in the variable declarations section.
This will allow you to drop a texture onto the component, and use it in your script!
Then, you can draw it on the screen with this-
void OnGUI()
{
float xMin = (Screen.width / 2) - (crosshairImage.width / 2);
float yMin = (Screen.height / 2) - (crosshairImage.height / 2);
GUI.DrawTexture(new Rect(xMin, yMin, crosshairImage.width, crosshairImage.height), crosshairImage);
}
This will draw the texture in the middle of the screen!
Of course, this isn't always what you want. Sometimes, you need it to be drawn on the mouse position instead!
float xMin = (Screen.width - Input.mousePosition.x) - (crosshairImage.width / 2);
float yMin = (Screen.height - Input.mousePosition.y) - (crosshairImage.height / 2);
I had to do the following for the crosshair at mouse position to work: substract (Screen.width - Input.mousePosition.x) from Screen.width. Otherwise the crosshair will mirror your mouse position at the center.
// draw on current mouse position
float x$$anonymous$$in = Screen.width - (Screen.width - Input.mousePosition.x) - (crosshairImage.width / 2);
float y$$anonymous$$in = (Screen.height - Input.mousePosition.y) - (crosshairImage.height / 2);
GUI.DrawTexture(new Rect(x$$anonymous$$in, y$$anonymous$$in, crosshairImage.width, crosshairImage.height), crosshairImage);
Although I do not know why but you don't have to do it on the y$$anonymous$$in value. Could someone explain this?
It's because in screen coordinates, the Y values are "inverted" relative to how the mouse axis works. What I mean by this is that Y values near 0 are near the top of your screen, and Y values that are near Screen.height are near the bottom of your screen.
Compare this to the Y values from your mouse input - where moving your mouse "up" (Away from your body) gives you higher Y values and "down" (towards your body) gives you lower Y values.
The reason screen space coordinates work this way dates back to how CRT screens draw images - they draw "scan lines" across the image from left to right, starting at the top of the screen and going down. Thus the top line of the screen is y = 0.
Answer by Jam0kid · Jul 30, 2012 at 10:15 AM
Hey bro, why not just try this simple C# code for a GUI Crosshair?
void OnGUI(){
GUI.Box(new Rect(Screen.width/2,Screen.height/2, 10, 10), "");
}
//this should be an easy fix if you are impatient :D
Answer by SETHI-ONE · Mar 17, 2015 at 04:18 PM
/ When I find myself in trouble, mother Mary comes to me. Speaking word of wisdom. Let it be! Just copy and paste / using UnityEngine; using System.Collections;
public class Crosshair : MonoBehaviour {
public Texture2D crosshair;
public Rect position;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
position = new Rect((Screen.width - crosshair.width) / 2, (Screen.height - crosshair.height) /2, crosshair.width, crosshair.height);
}
void OnGUI() {
GUI.DrawTexture (position, crosshair);
}
}