Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
0
Question by Smithy311 · Feb 20, 2015 at 05:30 PM · guiguitexturecrosshairunity4.6

Creating A Crosshair in Unity 4.6.2?

Hi, I am making a game and I need to create a crosshair for the player. I looked at tutorials online, and they are all saying to use GUITexture. Is GUITexture now outdated due to the GUI updates that came out? I ask this because I cannot get this method to work. If anyone can help, that would be great.

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

4 Replies

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

Answer by jakejolli · Feb 21, 2015 at 11:42 PM

Is there a specific reason you want to use GUITexture, or is it just because that's what you've found online?

If I were you I would look into Unity's new GUI tools. They're pretty awesome. You can easily make a crosshair that is perfectly centered on all devices this way.

Just import the image you want to use as a crosshair and set its texture type to sprite.

Then, in the menu bar, go to GameObject>UI>Image. This will automatically create a new canvas for a UI which is just a place for your UI elements, and place an Image on it. The image will appear as a white box, exactly centered on the screen.

Select the image either in the scene view or the hierarchy view and drag your imported crosshair sprite into its Source Image field in the inspector (or you can click the little circle next to Source Image and select your crosshair from the selection menu that appears).

Next, all you have to do is ensure that the crosshair has appropriate transparency (depending on the state in which the image was when you imported it), adjust its alpha levels as you wish, resize it to look the way you want (just don't move it around, or it won't be an accurate indicator of where your shots will go, depending on how you're shooting) and voila! Easy crosshair in just a few minutes.

You can also easily change the crosshair during gameplay to correspond to different weapons through code if desired, or you can hide the crosshair altogether if need be.

Also (and this goes a little beyond what you were asking), if you're making a First-Person game, casting a ray from the main camera's position straight forward will line your aim up with your crosshair.

The code for this looks like:

Physics.Raycast(Camera.main.transform.position, Camera.main.transform.forward, out hit, wpnRange);

Where hit should be replaced with a RaycastHit object, and wpnRange should be replaced by the range of your weapon, or Mathf.Infinity if your weapon has infinite range.

Hopefully this helps..

Comment
Add comment · Show 2 · 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 Smithy311 · Feb 22, 2015 at 02:25 AM 0
Share

Thanks for the help! I did not really want to use GUITexture, I just saw that all the tutorials online were using it, they were obviously outdated. Thanks for all the information, I will be sure to implement them. :)

avatar image shay4545 · Jul 26, 2015 at 04:50 PM 0
Share

how would you change the position of the crosshair with Raycast? I haven't used raycast much before but I thought it returned a bool

avatar image
2

Answer by Graham-Dunnett · Feb 20, 2015 at 05:31 PM

GUITexture is outdated, but can still be used.

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
avatar image
1

Answer by gkepets · Feb 21, 2015 at 12:39 AM

I used

var crosshairTexture : Texture2D; var position : Rect; static var OriginalOn = true;

function Start() { position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height); }

function OnGUI() { Screen.lockCursor = true; Screen.showCursor = false; if(OriginalOn == true) { GUI.DrawTexture(position, crosshairTexture); } }

Comment
Add comment · Show 1 · 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 gkepets · Feb 21, 2015 at 11:40 PM 0
Share
 var crosshairTexture : Texture2D; 
 var position : Rect; static var OriginalOn = true;
 
 function Start() { 
 position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height); }
 
 function OnGUI() { 
 Screen.lockCursor = true; 
 Screen.showCursor = false; 
 if(OriginalOn == true) { 
 GUI.DrawTexture(position, crosshairTexture); }
  }
avatar image
1

Answer by Smithy311 · Feb 21, 2015 at 09:53 PM

Thanks for the answer!

I converted to C# if anyone wanted it in C# rather than JavaScript.

 using UnityEngine;
 using System.Collections;
 
 public class Crosshair : MonoBehaviour 
 {
     public Texture2D crosshairTexture; 
     public Rect position; 
     static bool OriginalOn = true;
 
     void Start() 
     {
         position = new Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height);
     }
 
     void OnGUI() 
     {
         Screen.lockCursor = true; Screen.showCursor = false; 
 
         if(OriginalOn == true) 
             GUI.DrawTexture(position, crosshairTexture); 
     }
 }

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Smart Crosshair 4 Answers

Reduce Draw call for Multiple GUI Textures with same Texture 1 Answer

Simple GUI In game 1 Answer

My GUITexture looks awful 2 Answers

Use the GUITexture to go to the next scene 1 Answer


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