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
3
Question by Daniele · Nov 25, 2010 at 10:50 PM · fpsfirst-person-controllerspaceshootercrosshair

Crosshair? How?

I'm curious to know how to implement a crosshair of some sort.

Quite new to Unity so apologies XD

Cheers.

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

6 Replies

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

Answer by oliver-jones · Nov 25, 2010 at 11:09 PM

Okay, this is easy.

Make a new JavaScript, and place this into it - called it GUICrosshair:

 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()
 {
     if(OriginalOn == true)
     {
         GUI.DrawTexture(position, crosshairTexture);
     }
 }


Next, create a Game Empty within your scene, and drag the GUICrosshair script into it. Next find/make an image of a crosshair you want, and drag it into Unity, then into the Texture2D target.

Play the game, you will now have a GUI crosshair in the centre of the game

NOTE: if you have maximize on play, or resize your game screen whilst playing, your GUI will go all funny. This is just a bug in Unity - this will not occur when you export it as a game.

Comment
Add comment · Show 9 · 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 Daniele · Nov 25, 2010 at 11:20 PM 0
Share

$$anonymous$$uch thanks, cheers :}

avatar image oliver-jones · Nov 25, 2010 at 11:33 PM 1
Share

Okay, when your happy - mark this post as correct :)

avatar image muddmover · Feb 29, 2012 at 12:33 AM 0
Share

Whats the 2d target? what can be used?

avatar image Nercoe · Oct 30, 2012 at 07:01 PM 0
Share

"NOTE: if you have maximize on play, or resize your game screen whilst playing, your GUI will go all funny. This is just a bug in Unity - this will not occur when you export it as a game."

That's not a bug, just change function Start() to function Update() so it checks every frame for screen size.

avatar image tloch14 · Nov 03, 2014 at 04:55 AM 1
Share

So far people have proposed the rect calculation being either in Start() or in Update(). The first happens once, but has the side effect of not functioning properly with resizing. There isn't a "OnScreenResize()" function, but you can create your own checks in update, and then call a your own function to recalculate it whenever the checks come back with changes. This has the benefit of only recalculating when the window has actually resized, at a $$anonymous$$or cost of two float comparisons every frame.

 public class GUICrosshair : $$anonymous$$onoBehavior
 {
     Texture2D m_CrosshairTex;
     Vector2 m_WindowSize;    //$$anonymous$$ore like "last known window size".
     Rect m_CrosshairRect;

     void Start () {
     m_CrosshairTex = new Texture2D(2,2);
     m_WindowSize = new Vector2(Screen.width, Screen.height);
     CalculateRect();
     }

     void Update () {
         if(m_WindowSize.x != Screen.width || m_WindowSize.y != Screen.height)
         {
             CalculateRect();
         }
     }

     void CalculateRect()
     {
         m_CrosshairRect = new Rect( (m_WindowSize.x - m_CrosshairTex.width)/2.0f,
                                     (m_WindowSize.y - m_CrosshairTex.height)/2.0f,
                                     m_CrosshairTex.width, m_CrosshairTex.height);
     }

     void OnGUI() { /* Gui call referenced above) */
 }
Show more comments
avatar image
10

Answer by $$anonymous$$ · Feb 26, 2014 at 11:53 PM

This is a simplified version of the above code in C# for anyone that finds this question. The timeScale part is only if you have a pause menu that sets timeScale to 0 and you don't want the crosshair drawn in the menu.

     public Texture2D crosshairTexture;
     public float crosshairScale = 1;
     void OnGUI()
     {
         //if not paused
         if(Time.timeScale != 0)
         {
             if(crosshairTexture!=null)
                         GUI.DrawTexture(new Rect((Screen.width-crosshairTexture.width*crosshairScale)/2 ,(Screen.height-crosshairTexture.height*crosshairScale)/2, crosshairTexture.width*crosshairScale, crosshairTexture.height*crosshairScale),crosshairTexture);
             else
                 Debug.Log("No crosshair texture set in the Inspector");
         }
     }
Comment
Add comment · Show 5 · 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 AlwaysRickNL · Mar 04, 2014 at 09:19 PM 0
Share

The Script is c#

avatar image sacredgeometry · Mar 04, 2014 at 09:21 PM 0
Share

No it isn't it's unity script.

avatar image $$anonymous$$ · Mar 04, 2014 at 10:41 PM 1
Share

https://unity3d.com/learn/tutorials/modules/beginner/scripting/c-sharp-vs-javascript-syntax

avatar image Codessaurus · Oct 26, 2014 at 06:10 PM 1
Share

@WhoTnt Your script it's much more simplified and works pretty well except for the positioning. You forgot to multiply the crosshair scale, here is the fix: 'GUI.DrawTexture(new Rect((Screen.width-crosshairTexture.width*crosshairScale)/2 ,(Screen.height-crosshairTexture.height*crosshairScale)/2, crosshairTexture.width*crosshairScale, crosshairTexture.height*crosshairScale),crosshairTexture);'

avatar image $$anonymous$$ · Oct 27, 2014 at 03:11 AM 0
Share

@Bruno Tavares Damn, that was a stupid mistake. I've updated the code. Thanks

avatar image
0

Answer by Lancemaker_ · Nov 14, 2011 at 01:12 AM

In fact, there is aproblem in your script:

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

function Update() // Start will only get the screen size once. it will not refresh it. the turn around is to use function Update() instead. { position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height); }

function OnGUI() { 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
avatar image
0

Answer by chief1234 · Nov 14, 2011 at 04:48 AM

A more efficient solution with no code required:

  1. Create a new camera in your scene. Change the following settings on the new camera:

  2. Set depth to anything higher than your FPS camera.

  3. Set the camera Clear Flags property to "Depth Only"

  4. Create a plane and position in front of the camera and set desired scale.

  5. Drag crosshair texture from project view onto your new crosshair object.

  6. Set shader on crosshair material to something "transparent unlit". A quick search will give you a few options.

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 sacredgeometry · Nov 14, 2011 at 07:30 AM 0
Share

Im assu$$anonymous$$g you mean parent a plane with the FPS camera, this will only work if he wants a FPS crosshair. If he wants a free moving cursor or more control over it the above way is better.

avatar image
0

Answer by JJOBa · Mar 02, 2013 at 02:13 PM

ming shall tan so ime fal iyt gasi puij so ji fan iyt fgj uyo cgkl kgt yut opi uij. slik ij java script:var crosshairTexture : Texture2D; var position : Rect; static var OriginalOn = true;

function Update() // Start will only get the screen size once. it will not refresh it. the turn around is to use function Update() instead. { position = Rect((Screen.width - crosshairTexture.width) / 2, (Screen.height - crosshairTexture.height) /2, crosshairTexture.width, crosshairTexture.height); }

function OnGUI() { if(OriginalOn == true) { GUI.DrawTexture(position, crosshairTexture); } tan dop jlo gfd jozs shih fucj inze stroten

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
  • 1
  • 2
  • ›

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

19 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Keep player on surface 0 Answers

Make that muzzle flash? How? 1 Answer

Bouncy platforms/spring jump? 4 Answers

How to make crosshairs transparent? 0 Answers

Different crosshairs for different weapons ? 2 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