RectTransform and Physics2D.BoxCastAll.....

Hello All,
I work on “selection box” for my 2D RTS-like project and all is going well except one thing, but i start from the beggining…
So i have script for making “selection box” based on the UI Image, basicaly, using mouse i can draw that “selection box” in run time but want also detect objects that are within box boundrys so i can activate script on them making possible giving them “commands” etc. Selecting units by one is complete by simply raycasting2d from screen space to world space. So comming back to selecting units using “selection box” i come up with idea that i want shoot ray but same size as my “selection box” i draw in runtime.
Physics2D.BoxCastAll is something i need but i struggle to past correct values from RectTransform(which is my selection box) to BoxCastAll. I end up with something like this:

// Draggable inspector reference to the Image GameObject's RectTransform.
    public RectTransform selectionBox;
RaycastHit2D[] unitsSelected = Physics2D.BoxCastAll ((Vector2)selectionBox.transform.position + (Vector2)selectionBox.rect.center, selectionBox.rect.size , 0,Vector2.zero, 0, LayerMask.GetMask("Units"));

And selection works as follow:
!(http://

Selection Ray-box work as intended on left and bottom boundrys but go into infinity on upper and right boundrys and select everything what is behind that two boundrys. I think i made mistake maybe with specifying the size of that box but not sure what to put there exactly, everything i tried fail even worse, if someone willing to help and can advice how to fill BoxCastAll with data from the RectTransform i will be very greatefull.)

I think part of the problem is the first argument : origin. Your taking the selection box’s position and then adding its center to it. I think you want just transform.position or .center but not both. If the box is at the origin these calculations don’t affect things but imagine your RectTransorm is a 100x100 box but its sitting at 200,100 SO its center is 250,150 and your transform.position+ rect.center = 450,250. This is why it seems to be grabbing things up and to the right. your setting the origin too far up and right.

If all your objects are sitting on the same plane in 2d Space (they all have the same z-depth) you could simply things by trying Physics2D.OverlapBox

Thank you for reply,
I do as you suggested and here are results:
1.When i leave only (Vector2)selectionBox.rect.center then there is no selection at all, no units are selected even if i drag entire screen.
2. When i leave (Vector2)selectionBox.transform.position the selection area seems to be so big it select most of unit on my screen, even when i drag little box.
I also try to implement OverlapBoxAll but with same result, in conclusion there is something wrong with size maybe so i play with size component and see.

Thx for advices.

Thanks to drawgizmo stuff i see the problem, while my “selection box” as UI Image is drawn on screen space all dimensions passed from it to BoxCastAll are from this UI Image but drawn on that Big area of canvas where this UI Image is as child to it.

So my main problem is how to “translate” position of that UI image i drawn into space i see on the screen so it will be in same place and size i draw it on my screen… :frowning:

You can try:

Vector3 position = Camera.main.ScreenToWorldPoint(transform.position);

That converts a point on the screen to a position in worldSpace.

Yes i know this but this is only for point and i need something that translate my box i draw from Canvas space into world space.

If your canvas is in screenSpace-overlay mode… then those positions are in screen space. A box is just 4 points. So you can create a rectangle in worldspace by calling the above function 4 times.

Thx again but i have problem with that…To create a Rectangle in world space based on that selection box drawn on canvas (as UI Image) i need floats or Vectors2:

I start with way of creating Rectangle in world space based on the floats representing x,y, width and height so i must assign new floats with that coordinates from my drawn rectangle on canvas so for example i do for the first float x:
float xFromCanvas = Camera.main.ScreenToWorldPoint aaaand fail cause ScreenToWorldPoint needs Vector3 and my x from canvas is a float so that first way of creating rectangle in world space based on rectangle in canvas cant be done that way.
I go with second way using Vectors2:

minCornerForPosition = Camera.main.ScreenToWorldPoint(selectionBox.rect.min);
            widthAndHeightForSize = Camera.main.ScreenToWorldPoint(new Vector2(selectionBox.rect.width, selectionBox.rect.height));
            boxInWorldSpace = new Rect (minCornerForPosition, widthAndHeightForSize);

And do that cast:

RaycastHit2D[] unitsSelected = Physics2D.BoxCastAll (boxInWorldSpace.position, boxInWorldSpace.size, 0, Vector2.zero, 0, LayerMask.GetMask("Units"));
         

            foreach (RaycastHit2D item in unitsSelected)
            {
                UnitControll unitControl = item.collider.gameObject.GetComponent<UnitControll> ();
                unitControl.enabled = true;
            }

Selection not workin, when i draw entire screen most units are selected but not all, all are on “Units” layer.
Rectangle drawn on canvas—>converted to world space—>cast based on the converted coords—>not working, i’m confused :frowning:

P.S.
Also i have main camera set to Screen Space - Camera.

Maybe someone can at least tell if this is bad direction in making “selection box” or point some place where i can pay for advice, anything, i stuck in place with my project cause of this problem.
Or maybe someone know if there is some kind of problems in Unity when doing this, not supported behaviour, anything etc.