How would I detect parts between four positions?

Basically I am making selection box like this:
and I raycast from every corner of the box so I know 3d space positions of corners.
How can I check for parts if they are in the area between theese four coordinates? I also should mention that I can ignore Y axis completely, I just need the X and Z.
I dont need a whole script, I just want a tip or some suggestion I can work with.
Thanks in advance for everyone reading this!

WorldRoot:GetPartBoundsInBox() will return an array of parts whose bounding boxes overlap with a given cframe with a vector3 size. You’d have to figure out the center of the 4 points and the size.

1 Like

thats exactly the issue I have, I know how to check for parts in vector3 or a part but I dont know how to construct it from those four points

RaycastResult gives RaycastResult.Position.

yeah, I said that I already have the positions of the raycasts, I just dont know tow to make vector3 out of them

If your camera is always square to the workspace then you only need the top right and bottom left raycast Positions. From there you can get the X and Z positions of both those points (let’s say X1, Z1 and X2, Z2).
Check if the part is between X1 and X2, then check if it’s between Z1 and Z2.
If it passes both those checks the part’s center is inside the box. You could also subtract half the size of the part in it’s axes to get the part completely in the box.

1 Like

You could call WorldToScreenPoint on the positions of your parts you care about and check if they’re within the 2D rectangle.

That would probably be reasonably fast, and is pretty simple to implement.

You could get fancier with it as necessary, for example projecting multiple points from each part onto the screen for more fine-grain selection behavior.

1 Like

sadly, the camera is a little tilted and also since you can draw the square from “any corner”, it would result in writing if statements to check from which corner the ray was cast from, and then and possibly swap the larger/smaller if statements when checking for the X and Z, which is not very favourable

i believe you need a dot product

I read whole WorldToScreenPoint documentation but I dont know one thing, If there were lets say a tree in front of the selectable part, basically “hiding” it, would it be considered as NOT visible on screen? or is it just when the part is completely out of the camera vision field?

WorldToScreenPoint knows nothing about the possible obstruction of parts in your scene, its a very simple and fast function that says “where is this 3D point projected onto my screen?”.

You could first find the parts inside your selection box ignoring obstruction, and then send a ray to each one from your camera to further filter them to only visible ones, if that’s desired.

1 Like

I dont know how did I miss this

anyways, I think your solution is great, but how do I check if that point is in my selection frame, since the size offsets to minus depending on where are you dragging from/to. I have tried searching for it on this forum, but nothing useful showed up

nvm I found out how to do it, thanks everyone for help