• 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 PirateMonkey · Jul 25, 2014 at 12:09 AM · screendrawtexturescreen.width

GUI.Draw Texture Positioning

Hey,

So this might be a stupid question but I am somewhat new to Unity and find this confusing. I am currently making a game that uses a health bar and speed boost bar. I have been drawing them as rectangles on the screen using GUI.DrawTexture(xPosition, yPosition, etc...)

Where xPosition = Screen.width/2;

I would therefore expect the rectangle to be located in the middle of the screen (or just off since it moves from the bottom left corner), but instead it pops up maybe 1/4 way from the left. Why is it doing this?

I suspect it has something to do with screen space vs world space, but don't know enough about it. Any insight would be appreciated!

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 Stormizin · Jul 25, 2014 at 12:09 AM

The question is, if your texture has, 220px of width and u put screen.width/2 your texture will be positioned from the extreme left corner in the givin (screen.with/2), so you need just do this: Screen.width/2 - 220, and bang! you got it in the middle.

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 PirateMonkey · Jul 25, 2014 at 12:29 AM 0
Share

But if I subtract, wouldn't that push the bar even further left? Here is a picture for reference: alt text

screen shot 2014-07-24 at 6.23.23 pm.png (56.4 kB)
avatar image PirateMonkey · Jul 25, 2014 at 12:31 AM 0
Share

Also, the code I am using for the boost bar (blue bar in the top left):

 var boostPercent : float;
 var xPos : float = (Screen.width / 2);
 var yPos : float = 30;
 var backgroundWidthBoost : float = Screen.width / 6;
 var boostWidth : float;
 var height : float = backgroundWidthBoost/15;
 var boostRemainingBehindTexture : Texture;
 var boostRemainingTexture : Texture;
 
 function OnGUI()
 {
 
 boostPercent = GameObject.Find("Suber").GetComponent(SubControl).boostTimer / GameObject.Find("Suber").GetComponent(SubControl).boostTime;
 boostWidth = (boostPercent * backgroundWidthBoost * -1) + backgroundWidthBoost;
 
 //Draws a rectangle of x,y position, rec width and height, and wrapped in a specified texture
 GUI.DrawTexture(Rect(xPos,yPos,backgroundWidthBoost,height), boostRemainingBehindTexture, Scale$$anonymous$$ode.StretchToFill, true, 1.0f);
 GUI.DrawTexture(Rect(xPos,yPos,boostWidth,height), boostRemainingTexture, Scale$$anonymous$$ode.StretchToFill, true, 1.0f);
 
 }
avatar image Stormizin · Jul 25, 2014 at 12:37 AM 0
Share

Did you need it to left? so subtract or right just use +

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Detect touch in screen at different resolutions 2 Answers

Android screen orientation: why am I getting wrong screen size values? 1 Answer

Filling the screen doesn't... 1 Answer

Screen height and width confusion 1 Answer

Screen.width and Screen.height sometimes flipped for Android 1 Answer

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges