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)
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);
Answer by sysmaya · Jan 30, 2017 at 08:10 AM
GameObject plane = GameObject.CreatePrimitive(PrimitiveType.Plane);
Your answer
Follow this Question
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