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.

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

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 :smiley:

using UnityEngine;

public class Crosshair : MonoBehaviour
{
    public Texture2D crosshair;
    public Rect position;
	
    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);
    }
}