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 PurityLake · Jan 08, 2014 at 03:05 AM · positions

Positions in scene editor not same as in-game

Hi, I am trying to create a coin animation in which a value moves up above the coin indicating the value of this coin.

My problem with this is that the positions that I am trying to take from the game objects themselves are very far off. Despite the fact that they are near the center of the screen their x and y coords are less than one.

Is there anyway that I can convert the values that I get from gameObject.transform.position into values that correspond to the coins exact position in-game.

Any and all help is greatly appreciated.

EDIT: Sorry for the lack of information, hopefully I can make it more clear what my problem is.

Ok so I have a coin (blue). Checking the position in the inspector I can see that x is 1.809 and y is 0.075 rounded to 3 decimal places.

Currently the scene looks like: alt text

I have the coins set up so that when they are clicked a their value appears with a sound.

The code for writing the text to the screen is:

 void OnGUI() {
     if (this.displayText) {
         Vector2 pos = gameObject.transform.position;
         GUI.Label(new Rect(pos.x, pos.y, 30, 30), "+" + this.value.ToString());
     }
 }

this.value is the value of the current coin. Using the positions I get from gameObject the number ends up in the left corner.

alt text

As it seems in the scene view everything is being placed in the center of the screen so the values are skewed quite a lot.

Is there anyway that I can somehow convert the points of the coin object to points that can actually be used in-game to display the text directly above the coin?

unity_showcase.png (8.8 kB)
unity_coin_showcase.png (4.3 kB)
Comment
Add comment · Show 1
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 iwaldrop · Jan 08, 2014 at 04:37 AM 0
Share

A transform's position is its world position. Are you talking about GUI stuff? If yes, it doesn't matter where in the world they are, but where on the screen they are. The long and short of it is that you haven't supplied enough information to actually help you out yet.

1 Reply

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

Answer by zharik86 · Jan 08, 2014 at 05:46 PM

Yes, you are able to do it. In Unity there is a specific function which will transform object line item in three-dimensional space to a line item on the screen:

  Vector3 screenPos = Camera.main.WorldToScreenPoint(gameObject.transform.position);

ScreenPos.x and screenPos.y values - object line item on the camera in pixels. ScreenPos.z value - object depth (usually the point of center of object always undertakes). And further to use:

  GUI.Label(new Rect(screenPos.x - 30/2, Screen.height - screenPos.y - 30/2, 30, 30), "+" + this.value.ToString());

I hope it to you will help.

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 PurityLake · Jan 08, 2014 at 08:11 PM 0
Share

It fixes the problem somewhat but in certain cases the text appears below above and beside the coin

avatar image zharik86 · Jan 09, 2014 at 08:09 AM 0
Share

@PurityLake It seems, the text shall be on object center, but I wrote on memory. Try to change value of height of the text as:

  Screen.height - screenPos.y + 30/2

But most likely a problem not in it. At you for an output to the screen standard GUI Skin is used. At it for Label text alignment on the upper left corner is set. Try to change value for center.

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

Moving an object between two positions (C#) 1 Answer

local space position and world space position do not in code match a game object with no parent in inspector, 0 Answers

Ranking System in Racing Game 1 Answer

Player falling down after post processing turned on. 1 Answer

How to get all the GameObjects positions into a .txt file? 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