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 /
  • Help Room /
avatar image
0
Question by haruhi4 · Jul 22, 2016 at 01:05 PM · gameobjectpositionrectrectanglegui.drawtexture

Drawing a rectangle on gameObject's position

I have to use the "GUI.DrawTexture(new Rect..." function to draw a rectangle on my screen(actually, it's a gradient GUI bar i got from https://www.assetstore.unity3d.com/en/#!/content/19972, but deep inside it's just a rectangle). I want to do it right beside a gameobject i have on my screen.

The problem is: It seems like rectangle's coordinates are not the same as a gameobjects coordinates(i have searched online and i guess it's true). I have tried the function below to convert a gameobject's position to a rectangle's coordinates but still no luck:

 public Vector2 WorldToGuiPoint(Vector2 position)
     {
         var guiPosition = Camera.main.WorldToScreenPoint(position);
         guiPosition.y = Screen.currentResolution.height - guiPosition.y;
 
         return guiPosition;
     }


What should i do to create a rectangle that's like, right beside a current gameobject on screen (like, if the gameobject's position is x = -401 and y = -80, i want it on y=-80 and x=-300)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by smnerat · Jul 23, 2016 at 08:39 PM

You're right, the coordinate system for your object and the GUI are different. You generally will have a 2D or 3D world space coordinate system for your game, but your GUI will be in screen space.

Suppose you have a 2D game and an object is positioned at Vector2(0, 0) in world space, and the camera is centered on that position as well. That means, from your object, left is -X, right is +X, up is +Y and down is -Y.

I should mention that I've only recently worked with the new Unity UI system and not the package you're using, but this should still apply. In the Unity UI system positions are from the bottom left at Vector2(0, 0) to the top right at Vector2(1, 1).

Your GUI position is in screen space, not world space. So you wouldn't want to draw your GUI at Vector2(0, 0), because that is the bottom left corner of the screen. Since your object is in the center of the screen, its world space is Vector2(0, 0) and its screen space position is Vector2(0.5f, 0.5f), meaning you would want your GUI to be drawn at Vector2(0.6f, 0.5f). So:

 Vector3 screenPos = Camera.main.WorldToScreenPoint(position);
 Vector2 guiPosition = new Vector2(screenPos.x, screenPos.y);

screenPos.x and screenPos.y will give you the x and y screen space position and screenPos.z is the distance from the camera.

I think the problem is that Camera.WorldToScreenPoint() returns the position in pixels, so your guiPosition is probably something like: Vector3(650, 1270, 300). To get your screen position values in a range from 0 to 1 you need to divide by the screen size.

 Vector3 screenPos = Camera.main.WorldToScreenPoint(position);
 Vector2 guiPosition = new Vector2(screenPos.x / Screen.width, screenPos.y / Screen.height);
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 sysmaya · Jan 30, 2017 at 08:10 AM

 GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
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

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

75 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

Related Questions

CS0120 - Object reference required but it's already assigned. 0 Answers

comparing two gameobject 1 Answer

Game Object doesn't instantiate at Mouse Position 3 Answers

Transform Position is not moving the target 2 Answers

Assinging new coordinates (position) in Unity to imported assets/objects from Rhino 0 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