- Home /
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 ;)
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:
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
Answer by Statement · Aug 12, 2011 at 11:14 PM
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.
Wow, that's amazingly clean code and it works fantastic. Beautiful syntax. Thumbs up and if I could I'd give you a gold medal, too, as that's excellent learning material for me!
Happy to help you. And oops, I did forget to change the textures in the Draw function. It will use the center texture for all of the lines. I just updated the code since I had another bug, where Input.GetButtonDown was used ins$$anonymous$$d of Input.GetButton.
Hi, Im a student and currently one of sch assignment is to have a crosshair point out of the gun turret.
However after trying out your codes, it didn't draw out. There is no error mention too. What could be wrong? I attached the script to a empty GameObject.
ins$$anonymous$$d of the "bool firing = Input.GetButton("Fire1");", i change to " bool firing = Input.Get$$anonymous$$ouseButtonDown(0);"
Anyone can help me please?
Answer by Kat0421 · May 29, 2013 at 09:48 AM
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);
}
}