Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 LeiterJakab · Oct 30, 2010 at 07:04 PM · screenconvertcoordinatesworldconversion

world to screen coordinates conversion

Im trying to draw a crosshair texture where a gun in my ame is pointing. I use an Empty that is the child of the gun so it follows its movements. I than convert the coordinates of the Empty with the WorldToScreenPoint function and use GUI.DrawTexture to draw the crosshair at the right place.

However the vertical movement of the crosshair texture is inverted. When I point the gun up to the sky the crosshair moves to the bottom half of the screen and vice versa.

I tried unparenting the target Empty and still experienced the same results. Please tell me if I'm making some mistake here!

This is my script added to my Camera:

var crosshair : Transform; var crosshairTex : Texture2D; private var texPosition : Rect; private var pos : Vector3;

function OnGUI () { pos = camera.WorldToScreenPoint (crosshair.position); texPosition = Rect (pos.x - (crosshairTex.width / 2), pos.y - (crosshairTex.height / 2), crosshairTex.width, crosshairTex.height);

 GUI.DrawTexture (texPosition, crosshairTex);

}

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
5
Best Answer

Answer by Eric5h5 · Oct 30, 2010 at 07:36 PM

OnGUI doesn't use screen coords; it uses GUI coords which are top-down and not bottom-up. It would be a little faster to use a GUITexture gameobject instead, in combination with WorldToViewportPoint, but if you need to use OnGUI, you can invert the Y coord by subtracting it from Screen.height.

Comment
Add comment · Show 3 · 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 LeiterJakab · Oct 31, 2010 at 12:09 AM 0
Share

I think GUITexture uses fractions. So the centre of the screen is (0.50, 0.50). So I would heve to use soemthin like (pos.x / Screen.width) to define the position.

avatar image Eric5h5 · Oct 31, 2010 at 12:49 AM 0
Share

@LeiterJakab: No, you'd use WorldToViewportPoint, as I mentioned.

avatar image LeiterJakab · Oct 31, 2010 at 08:42 AM 0
Share

Got carried away with my plan when I started writing the script. Both of your solutions work fine, thank you for the quick answers.

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

No one has followed this question yet.

Related Questions

getting world coordinates of mouse position 2 Answers

how to calculate screen to world point (custom) 0 Answers

Transform's position wherever the mouse is pointing. 2 Answers

How to get mouse position in World Space when in Editor Mode 1 Answer

Converting world point to screen point 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