How to manipulate Crosshair with DrawTexture? (total Newbie)

Hello fellow Unity users. I’m just starting with Unity and C# in general, so please excuse the incredible stupidity of a code that you are about to see :wink:

I thought I’d start with something that you’d think is simple: GUI stuff! But wasn’t as simple as I thought and the Documentation is confusing.

What I try to accomplish:

I’m trying to render multiple elements making up a whole Crosshair (perfectly centered). I want to drag elements of this crosshair apart on certain actions, like when firing a shot as additional player feedback.

I tried reading around, and found a few hints and sample codes for “GUI.DrawTexture” to do Crosshairs. So I tried that on a empty GameObject and it’s not quiet working.

What’s going wrong:

I have no way to access the individual elements (so I can manipulate their position individually in the Update function), because I can’t find information how to do that.

And the crosshair elements have larger distances to the center point than they should:
alt text

Here’s the code:

using UnityEngine;
using System.Collections;
    
public class Crosshair : MonoBehaviour {
	
	public Texture2D centerTexture, topTexture, bottomTexture, leftTexture, rightTexture;
	public float lineSpacing = 50.0f;
	
	int centerX = Screen.width / 2;
	int centerY = Screen.height / 2;
	
	void OnGUI()
	{	
		// position crosshair elements
		GUI.DrawTexture(new Rect(centerX, centerY, 1, 1), centerTexture);
		GUI.DrawTexture(new Rect(centerX, centerY - lineSpacing, 1, 25), topTexture);
		GUI.DrawTexture(new Rect(centerX, centerY + lineSpacing, 1, 25), bottomTexture);
		GUI.DrawTexture(new Rect(centerX - lineSpacing, centerY, 25, 1), leftTexture);
		GUI.DrawTexture(new Rect(centerX + lineSpacing, centerY, 25, 1), rightTexture);    		
	}
	
	// Update is called once per frame
	void Update () {
			
		if(Input.GetButtonDown("Fire1"))
		{
			// change color
			GUI.color = Color.red; // not working?
			
			// draw the elements apart for player feedback
			// but how to access them??
		}
     }
 }

I’ve read so many different confusing things and feel a little overwhelmed. Please don’t laugh. I’d appreciate a pointer in the right direction, or some sample code.

Thank you

You also need to subtract the height for the top one, and the width for the left one.

using UnityEngine;
using System.Collections;

public class Crosshair : MonoBehaviour {
    public Texture2D centerTexture, topTexture, bottomTexture, leftTexture, rightTexture;
    public float width = 25.0f;
    public float height = 25.0f;
    public float spacing = 4.0f;
    Color color;

    void OnGUI() {   
        int x = Screen.width / 2;
        int y = Screen.height / 2;
        GUI.color = color;

        // only for text formatting purposes
        float s = spacing;
        float w = width;
        float h = height;

        Draw(x        , y        , 1, 1, centerTexture); // Dot
        Draw(x        , y - s - h, 1, h, topTexture); // Upper line
        Draw(x        , y + s    , 1, h, bottomTexture); // Lower line
        Draw(x - s - w, y        , w, 1, leftTexture); // Left line
        Draw(x + s    , y        , w, 1, rightTexture); // Right line
    }

    // Helper to reduce code.
    void Draw(float x, float y, float w, float h, Texture2D t) {
        GUI.DrawTexture(new Rect(x, y, w, h), t);    
    }
    
    void Update () {
        bool firing         = Input.GetButton("Fire1");
        color               = firing ? Color.red : Color.white;
        float targetSpacing = firing ? 16.0f : 4.0f;
        float targetWidth   = firing ? 25.0f : 50.0f;
        float targetHeight  = firing ? 25.0f : 50.0f;
        float speed         = Time.deltaTime * 10.0f;

        spacing = Mathf.MoveTowards(spacing, targetSpacing, speed);
        width   = Mathf.MoveTowards(width, targetWidth, speed);
        height  = Mathf.MoveTowards(height, targetHeight, speed);
    }
}

I have no way to access the individual elements (so I can manipulate their position individually in the Update function), because I can’t find information how to do that.

Just increase spacing variable. I included some sample code that does this. When it comes to GUI/Update code you usually have to read/write from/to variables since they are called at different times, and that the GUI functions only are callable during OnGUI. If you need finer control over each rectangle, just expose the 5 rectangles as member variables, and edit those.

using UnityEngine;
using System.Collections;

public class crossHair : MonoBehaviour {

public Texture2D centerTexture, topTexture, bottomTexture, leftTexture, rightTexture;
public float width = 25.0f;
public float height = 25.0f;
public float spacing = 4.0f;
Color color;

// Use this for initialization
void Start () {

}


void Draw(float x, float y, float w, float h, Texture2D t){
	GUI.DrawTexture(new Rect(x, y, w, h), t);
}

void OnGUI(){
	int x = Screen.width /2;
	int y = Screen.height /2;
	GUI.color = color;
	
	//only for text formatting purposes
	float s = spacing;
	float w = width;
	float h = height;
	
	Draw(x        , y        ,1,1, centerTexture); // Dot
	Draw(x        , y - s - h, 1, h, topTexture); // Upper Line
	Draw(x        , y + s    , 1, h, bottomTexture); // Lower Line
	Draw(x - s - w, y        , w, 1, leftTexture); // Left Line
	Draw(x + s    , y        , w, 1, rightTexture); // Right Line
	
}

// Update is called once per frame
void Update () {
	
	bool firing = Input.GetMouseButtonDown(0);
	color = firing ? Color.red : Color.white;
	float targetSpacing = firing ? 16.0f : 4.0f;
	float targetWidth = firing ? 25.0f : 50.0f;
	float targetHeight = firing ? 25.0f : 50.0f;
	float speed = Time.deltaTime * 10.0f;
	
	spacing = Mathf.MoveTowards(spacing, targetSpacing, speed);
	width = Mathf.MoveTowards(width, targetWidth, speed);
	height = Mathf.MoveTowards(height, targetHeight, speed);

}

}