I am making a game where the players resize and move parts and stuff in game. But I dont want them to move or resize a part outside an area so it doesn’t get too big.
I have a part with movement handles on it: Screenshot
I don’t want players to be able to move or resize the part outside the area of the red cylinder below the part. (I hope that made sense). How can I achieve this?
You need to find the point on the part that is the farthest away from the center of the cylinder (XZ plane). Then you push the part back so that the farthest point touches the edge of the cylinder.
I could be wrong, but I don’t think you can directly read the values of vertices in Roblox. So you will need to start by creating an array containing the points to a 1x1x1 cube. Then you need to rotate and scale it to match the current scale/rotation of your part. Iterate through all of those points to find the one that is farthest from the center of the cylinder, only taking into account the X&Z dimensions. Finally you push that point back into the boundaries by moving the part in direction cylinderPos - partPos by (cylinderPos - partPos).magnitude - Radius.
Keep in mind pushing the part back should only use X&Z.
Are you trying to get the out of bounds of a cube or a sphere? or are we dealing with a 2D enviroment?
If your using a 2D checking you can use magnitude easily. If you’re using a cube check then you can do:
if (movePart.Position.x > (boundsPart.Size.x / 2)) then
...
end
You would also do that for the y and z
If your checking for a sphere then I believe you can still use magnitude. Hope this helps
The method I suggested works with a circle (or a cylinder by adding a single direction constraint.) This method works with any rotation which is why I selected it.
You would need to do similar calculations with pretty much any shape if you wanted the the entire part to stay in the bounds if the part can be rotated. If you are fine with up to half of the part being out of bounds or having the object in the boundary never rotate then it can be simplified by a lot.