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 tdg911 · Apr 28, 2014 at 05:50 PM · guiworldtoscreenpoint

Converting world coordinates to screen coordinates

Currently I'm working on an particle system to display over a GUI.Label I have when a new best score has been achieved. The labels are created using this snippet of code "GUI.Label (new Rect(95, 160, 175, 40), "" + bestScore, style);"

I made my particle system object a prefab, so what I would like to do is Instantiate this in my script so that the particle object is directly over my new best score label. Figured it would be much easier doing this since I have multiple scenes but I'm just not familiar enough in Unity3d right now. I'm mainly used to working in pixels only.

From what I've been reading many say that I should be using Camera.WorldToScreenPoint?

Any help/nudge in the right direction would be great.

TiA,

T

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

Answer by robertbu · Apr 28, 2014 at 06:08 PM

The only tricky part of this problem is that GUI uses a GUI coordinate system that starts in the upper left of the screen, where Screen coordinates start in the lower left of the screen. First pull out the Rect into a variable at the top of your C# class:

  private Rect bestScoreRect = new Rect(95, 160, 175, 40);

Then the code to create a world position would be:

  Vector3 pos = bestScoreRect.center;
  pos.y += bestScoreRect.height / 2.0;  // Position at top of rect
  pos.y = Screen.height - pos.y;  // Convert from GUI to Screen
  pos.z = someDist;  // Distance in front of the camera
  pos = Camera.main.ScreenToWorldPoint(pos);

'someDist' is the distance in front of the camera in world space to place the object. And as a final note, your particle system prefab lives in World space, so what the code here does is convert the GUI/Screen coordinate to world space...the opposite of your title.

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 tdg911 · Apr 28, 2014 at 07:12 PM 0
Share

Thanks for the quick reply. So basically I instantiate my particleSystem object and pass in the pos.x, pos.y, pos.z as Vector3?

avatar image robertbu · Apr 28, 2014 at 08:04 PM 0
Share

You can use 'pos' in the Instantiate():

 Instantiate(prefabPS, pos, Quaternion.identity);

Note you may need to make changes to the ParticleSystem settins, or perhaps raise the 'y' position up some more on line 2 of the code fragment. You'll have to try it and play to get the look you want.

avatar image tdg911 · Apr 28, 2014 at 09:06 PM 0
Share

I appreciate your time and help along with the speedy responses. Works just fine :)

avatar image tpainton · Sep 30, 2014 at 08:03 PM 1
Share

Line 2 makes no sense. Vector3 doesn't have a height property. Did you mean bestScoreRect.height? $$anonymous$$ust have.

avatar image robertbu · Sep 30, 2014 at 11:26 PM 0
Share

@tpainton - you are right. Fixed. Thanks for bringing it to my attention.

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

20 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

Related Questions

Not working gui buttons 1 Answer

Fixed GUILabel with WorldToScreenPoint 0 Answers

Font sizes, GUI, Iphones and even more head scracthing problems 0 Answers

Random spawning papers, plus GUI texture book help. 0 Answers

GUI.FocusControl & GUI.SetNextControlName doesn't work 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