- Home /
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!
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.
But if I subtract, wouldn't that push the bar even further left? Here is a picture for reference:
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);
}
Did you need it to left? so subtract or right just use +
Your answer
![](https://koobas.hobune.stream/wayback/20220613154929im_/https://answers.unity.com/themes/thub/images/avi.jpg)