- Home /
Texture Size doesn't match collider size
Hi, I am running in to the issue where I want a given texture, (in this case solid color), to match up to the size of the collider. Both of these things are in the same game object.
I tried to use code to match the size of texture to the exact size of collider so it is visible in the game of where the collider is. My attempt was this:
void Start ()
{
collider_a = gameObject.GetComponent<BoxCollider2D> ();
collider_a.size = ....;
collider_a.center = ....;
}
void FixedUpdate ()
{
//constantly changing the sizes of the collider
collider_a.size = new Vector2 (colliderSize, 2); //colliderSize is already declared in the class
}
private void OnGUI(){
GUI.DrawTexture (new Rect (x, y, colliderSize, 2), a, ScaleMode.StretchToFill); //texture a is already declared
}
This is a very stripped down version of my code. variables are all declared and working.
The issue is, i need to find a way to make the texture as big as the collider. This makes the texture much smaller than the actual collider size, even though the parameters are identical. collider of size (5,2) would need a texture of size (~90,~100) to have the same size.
I have x,y currently set to 0 for debugging, but x,y is also messed up if i try to relate it to the collider center - size. Can anyone give me some insights into what im doing wrong?
Update: The texture is just a whole picture of one color. It is drawn by the script GUI function. I did it this way so that I can change it's size as the collider zones changes. This texture is already created, but it's size is monitored by the rectangle variables at the second last line of code. new rect (.....).
The script and the collider is in the same EMPTY game object. fixed update allows me to control the colliders sizes
I'm a total beginner at GUI, but I believe the Rect constructor uses pixel units where as colliders use Unity units.
You might resolve that issue by using a textured plane and scale that ins$$anonymous$$d.
can you expand a little bit on textured plane and scale? so i should draw gui in textured planes?
It is unclear where the issue is here. First you need to define what kind of object you are using to display the texture. Second, you need to explain how you are dealing with the texture. Is it a sprite created at edit time, or are you creating a new sprite at runtime?