Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by darky · Aug 12, 2011 at 10:58 PM · crosshairdrawtexture

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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.

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image darky · Aug 12, 2011 at 11:37 PM 0
Share

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!

avatar image Statement · Aug 12, 2011 at 11:41 PM 1
Share

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.

avatar image Kat0421 · May 29, 2013 at 08:34 AM 0
Share

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.

avatar image Kat0421 · May 29, 2013 at 08:40 AM 0
Share

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?

avatar image
0

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

 

}

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

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

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

GUI.DrawTexture not working? (nothing shows up) 1 Answer

inventory system - change GUI.DrawTexture texture 3 Answers

GUI.DrawTexture shows in Unity but not in Build 1 Answer

GUITexture makes my image to low quality 1 Answer

How can I trace Alphabet 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges