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
0
Question by paklavins · Mar 07, 2018 at 05:15 AM · uicrosshair

How to draw and move a crosshair/reticle (2D)?

I want to have a crosshair around my Player at a distance R and angle X. All the old posts I see recommend things like OnGUI and Texture2D but apparently those things are old and deprecated so apparently I need to use Canvas GameObjects on the UI layer.

My setup: a Player class with a public GameObject crosshair field, in which I click+dragged a Canvas I created with GameObject -> UI -> Image (just a simple gif) named Crosshair. It exists in my Game scene as well.

Here's the relevant parts of the code:

 public GameObject crosshair;
 private GameObject reticle;
 private void Start () {
     reticle = Instantiate(crosshair, body.position, Quaternion.identity);
 }

 private void Update() {
     SetAimDirection();
 }

 private void SetAimDirection() {
     Debug.Log(reticlePosition);
     reticle.transform.position = new Vector3(reticlePosition.x, reticlePosition.y, 0);
 }

Despite the Debug.Log reporting real values, the crosshair doesn't move. Oh and for some reason I get a second Crosshair object (called Crosshair (clone)) when I run the game. What's the proper way to do this? Should I be using this Canvas/UI layer approach or something else?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Legend_Bacon · Mar 07, 2018 at 10:52 AM

Hello there,

If you already have a crosshair in your scene, you don't need the line in Start(). It creates a new crosshair (which is why you have a (clone) in your scene), and assigns your variable "reticle" to it. Because of that, the function SetAimDirection() updates the position of that clone, instead of your real crosshair.

Here's a simple Unity tutorial that might help you.

Hope that helps!

Cheers,

~LegendBacon

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 paklavins · Mar 08, 2018 at 02:48 AM 0
Share

After playing around some more, I changed my modifying line to: crosshair.GetComponent<RectTransform>().localPosition = new Vector3(reticlePosition.x, reticlePosition.y, 0);. I set the Crosshair GameObject's Canvas Render$$anonymous$$ode property to 'World Space', and its size and initial position were all messed up which was making me think it wasn't being rendered (while it was, just way off screen). So I fixed that and I finally got it to work.

And for reference, my original .transform.position = new Vector3() code also works with that sorted.

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

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

Random movement of crosshair inside ui box 1 Answer

How to make a crosshair move with the player 1 Answer

How to Instantiate a Button in a Constructor(if possible) 1 Answer

how to make crosshair move slightly 1 Answer

change value of child ui text element 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