x


Smart Crosshair

Hello Guys,

I'm working on an First Person Shooter and I was searching how to make a smart crosshair and I didn't found anything, I explain (what is my definition of smart crosshair) :

  1. That become bigger when you shoot(not to much, just a touch)
  2. Depending where you focus is size change

And it is basically this. I finally want how you can make it : with a script, raycast... And maybe some explanation or the final script (if is the good way to make it).

Best Regards,
Nbo

more ▼

asked Jan 09 '12 at 06:24 PM

NhommeBeurreOne gravatar image

NhommeBeurreOne
90 7 10 12

Any idea , please ?

Jan 09 '12 at 08:23 PM NhommeBeurreOne

Answering your own question like this won't help. If you want to 'bump' just edit your question a little and save

Jan 09 '12 at 10:12 PM DaveA

Take this in note

Jan 09 '12 at 11:06 PM NhommeBeurreOne

I'm also looking for a Smart Crosshair for my FPS game. And i have seen that Dastarly Banana has a good one. But only the system how he makes that is to difficult. Don't understand it

Jan 13 '12 at 04:08 PM Donilias

... Then work on simpler things, until you reach the level at which you do understand it.

Jan 13 '12 at 04:10 PM syclamoth
(comments are locked)
10|3000 characters needed characters left

3 answers: sort voted first

A basic crosshair that spreads.

//Slap this script on to the main camera or similar and you'll have a GUI driven crosshair in no time without the use of a texture.

#pragma strict

var drawCrosshair = true;

var crosshairColor = Color.white;   //The crosshair color

var width : float = 1;      //Crosshair width
var height : float = 3;     //Crosshair height

class spreading{
    var spread = 20.0;          //Adjust this for a bigger or smaller crosshair
    var maxSpread = 60.0;
    var minSpread = 20.0;
    var spreadPerSecond = 30.0;
    var decreasePerSecond = 25.0;
}
var spread : spreading;

private var tex : Texture2D;

private var newHeight : float;
private var lineStyle : GUIStyle;

function Awake (){
    tex = Texture2D(1,1);
    SetColor(tex, crosshairColor); //Set color
    lineStyle = GUIStyle();
    lineStyle.normal.background = tex;
}

function OnGUI (){
    var centerPoint = Vector2(Screen.width/2,Screen.height/2);
    var screenRatio : float = Screen.height/100;

    newHeight = height * screenRatio;

    if(drawCrosshair){
        GUI.Box(Rect(centerPoint.x-(width/2), centerPoint.y - (newHeight + spread.spread), width, newHeight),GUIContent.none,lineStyle);
        GUI.Box(Rect(centerPoint.x-(width/2), (centerPoint.y + spread.spread), width, newHeight),GUIContent.none,lineStyle);
        GUI.Box(Rect((centerPoint.x + spread.spread), (centerPoint.y - (width/2)), newHeight, width),GUIContent.none,lineStyle);
        GUI.Box(Rect(centerPoint.x- (newHeight + spread.spread), (centerPoint.y -(width/2)), newHeight, width), GUIContent.none, lineStyle);
    }
    if(Input.GetButton("Fire1")){
        spread.spread += spread.spreadPerSecond * Time.deltaTime;       //Make spreading "smooth" and not abrupt
        Fire();
    }

    spread.spread -= spread.decreasePerSecond * Time.deltaTime;      //Decrement the spread
    spread.spread = Mathf.Clamp(spread.spread, spread.minSpread, spread.maxSpread);     
}

function Fire(){
    //Carry out your normal shooting and stuff
}
    //Applies color to the crosshair
function SetColor(myTexture : Texture2D, myColor : Color){
    for (var y : int = 0; y < myTexture.height; ++y){
        for (var x : int = 0; x < myTexture.width; ++x){
            myTexture.SetPixel(x, y, myColor);
            }
        }
        myTexture.Apply();
    }

I spend a little bit of time on this and I managed to create the script above. The script is rather basic and there is a lot that you can do with it, but I guess it will do, for now at least.

more ▼

answered Jan 15 '12 at 08:31 PM

OrangeLightning gravatar image

OrangeLightning
5.3k 47 57 111

Hello,

First Thank You. Second, I tweak a bit with your script and it's really good to make the crosshair. Now do you have any idea to make it focus ? To make it bigger for when it shoot I think about a function that basically said : Add 10 to spread, but I'm not sure if it will be smooth so...

Jan 15 '12 at 09:52 PM NhommeBeurreOne

I don't really get what you mean by "focus". Do you mean aim down sights or not? I edited the example to spread when you hold down right mouse button. Rather basic example, but it will get you going.

Jan 16 '12 at 08:20 AM OrangeLightning

Focus : If the crosshair focus on an object that is close he'll be small and, if he's focus on an object that is far he's gonna be a bit bigger.

Jan 16 '12 at 09:56 PM NhommeBeurreOne

You will have to measure distance between the player and the object he or she is aiming at. Use raycast to retrieve information and use Vector3.distance to measure distance between you and the object.

Jan 16 '12 at 10:11 PM OrangeLightning

It worked. Thanks for the help. I'm not sure if it'll help at all, but I'll post the conversion here for others to look at. As I said before, it's an extremely straightforward conversion.

using UnityEngine;
using System.Collections;
/// <summary>
/// A straightforward C# conversion of the JavaScript smartCrosshair (tentatively named)
/// Works similarly to original
/// </summary>
public class SmartCrosshair : MonoBehaviour
{
    #region Fields
    public bool drawCrosshair = true;
    public Color crosshairColor = Color.white;
    public float width = 1;
    public float height = 3;

    [System.Serializable]
    public class spreading
    {
        public float sSpread = 20;
        public float maxSpread = 60;
        public float minSpread = 20;
        public float spreadPerSecond = 30;
        public float decreasePerSecond = 25;
    }

    public spreading spread = new spreading();

    Texture2D tex;
    float newHeight;
    GUIStyle lineStyle;

    #endregion

    #region Functions
    void Awake () {
        tex = new Texture2D(1, 1);
        lineStyle = new GUIStyle();
        lineStyle.normal.background = tex;
    }

    void OnGUI () {
        Vector2 centerPoint = new Vector2(Screen.width / 2, Screen.height / 2);
        float screenRatio = Screen.height / 100;

        newHeight = height * screenRatio;

        if (drawCrosshair) {
            GUI.Box(new Rect(centerPoint.x - (width / 2), centerPoint.y - (newHeight + spread.sSpread), width, newHeight), GUIContent.none, lineStyle);
            GUI.Box(new Rect(centerPoint.x - (width / 2), (centerPoint.y + spread.sSpread), width, newHeight), GUIContent.none, lineStyle);
            GUI.Box(new Rect((centerPoint.x + spread.sSpread), (centerPoint.y - (width / 2)), newHeight, width), GUIContent.none, lineStyle);
            GUI.Box(new Rect(centerPoint.x - (newHeight + spread.sSpread), (centerPoint.y - (width / 2)), newHeight, width), GUIContent.none, lineStyle);
        }

        if (Input.GetKey(KeyCode.Mouse0)) {
            spread.sSpread += spread.spreadPerSecond * Time.deltaTime;
            Fire();
        }

        spread.sSpread -= spread.decreasePerSecond * Time.deltaTime;
        spread.sSpread = Mathf.Clamp(spread.sSpread, spread.minSpread, spread.maxSpread);
    }

    void Fire() { }

    void SetColor(Texture2D myTexture, Color myColor) {
        for (int y = 0; y < myTexture.height; y++) {
            for (int x = 0; x < myTexture.width; x++)
                myTexture.SetPixel(x, y, myColor);
            myTexture.Apply();
        }
    }
    #endregion
}
Apr 20 at 06:33 PM Rxanadu
(comments are locked)
10|3000 characters needed characters left

Here is my solution for your question: 1. use Screen.showCusor = false to hide mouseCousor 2. use GUI.DrawTexture for your crossHair 3. if shoote, replace one bigger texture when press fire button,you can adjust the size by GUI.DrawTexture function. 4. use Raycast to gernerate one ray from fire position and if it is hit something with tag,for example "Enemy", replace the texture that belongs to "Enemy"(you need assign tag "Enemy" to your Enemy object.)

more ▼

answered Jan 14 '12 at 08:24 AM

luozitian gravatar image

luozitian
51 1 4 4

Thank You for your answer, but I already figure it out. My problem is really when he shoot and he focus. Is your technique is good when he shooting (smooth).

Jan 14 '12 at 01:32 PM NhommeBeurreOne
(comments are locked)
10|3000 characters needed characters left

i dont know how to make that kind of crosshairs but what you can do is add the cross hairs in the game there is a crosshairs script in the game and the texture is the aim one i hope that helps

more ▼

answered Jan 17 '12 at 01:15 AM

skully42 gravatar image

skully42
21 6 9 10

In the Standard Asset ?

Jan 17 '12 at 01:36 AM NhommeBeurreOne

Alright here is what you have to do first make a gameobject and name GUI then search Crosshairs in the bar with everything in unity not your game then when you find that drag it into your GUI gameobject click on you gameobject and you will see the crosshair script with a texture that is not there in the texture folder you will see the aim one then drag that into the crosshairs script and you are done if you have any questions just ask i will help i look at my email almost everyday to see if there is a new comment

Jan 17 '12 at 09:04 PM skully42
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x3668
x1525
x476
x110

asked: Jan 09 '12 at 06:24 PM

Seen: 4490 times

Last Updated: Apr 20 at 06:34 PM