- Home /
read comments.
Question regarding Rect center
HI there, I'm making my own box colliders and while doing this a confusion regarding Rect center appeared.
Apparently, I can have a gameObject at, let's say (0,0), and a script inside this same gameObject instantiating a Rect struct with center as (10, 10).
The Rect will be drawn at (0,0) position, though.
The impression I got is that the center is "virtual" (in the real sense of the word, nothing to do with virtual classes), and if I use Contains or other methods, it will be as if the Rect is solely centered in (10, 10) instead of (0,0), is that right?
Or will it be in the two places in same time?
To avoid any confusions in my own project, I'm always placing the gameObject in the desired spot and doing Rect.center = gameObject.transform.position.
p.s: I think the documentation should have been more specific about this.
Please clarify your question. Are you talking about the struct UnityEngine.Rect? If so, what has this to do with drawing anything? The struct is just 4 float values. So who draws what using what? Can you add the code that draws wrongly?
$$anonymous$$y gut feeling would be, that you copy somewhere the struct and loose your changes (structs are copied on assignment, not referenced). Or that you talking about something completely different here - maybe you meant RectTransform and anchoredPosition and deltaSize and stuff?
I've found out what was wrong... $$anonymous$$y drawing function was:
void OnDrawGizmos()
{
Gizmos.color = Color.white;
Gizmos.DrawCube (transform.position, new Vector2 (box.width, box.height));
}
Of course this would always draw the Rect in the same position as gameObject. Replacing transform.position with box.center fixed the problem.
Rects are only value containers with a few helper methods. $$anonymous$$aybe what you want is Bounds.Contains from either the renderer or the collider (which you get in the position of your object). But you can always set the rect center to your objects position to have the same effect when calling contains on global positions.
Follow this Question
Related Questions
Push only 1 box around at a time (2D) 1 Answer
Rigidbody2d getting stuck when moving horizontally at random points! Can't figure it out :( 1 Answer
Make a GUI.Box with an outline AND a transparent center? 1 Answer
Using rect.contains to check if gameobject is inside in worldspace 1 Answer
How to render a colored 2D rectangle. 6 Answers