Stop part going outside an area?

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?

Here is the script inside the Handle:

local handle = script.Parent
local Increment = handle.Adornee.ResizeIncrement
local CF

handle.MouseButton1Down:Connect(function()
	CF = handle.Adornee.CFrame
end)

handle.MouseDrag:Connect(function(id, distance)
	local moving = handle.Adornee
	local axi = {
		[Enum.NormalId.Right] = moving.CFrame.RightVector,
		[Enum.NormalId.Left] = -moving.CFrame.RightVector,
		[Enum.NormalId.Top] = moving.CFrame.UpVector, 
		[Enum.NormalId.Bottom] = -moving.CFrame.UpVector,
		[Enum.NormalId.Front] = moving.CFrame.LookVector,
		[Enum.NormalId.Back] = -moving.CFrame.LookVector,
		}
	moving.CFrame = CF + axi[id] * (math.floor(distance / Increment) * Increment)
end)

Any help is appreciated!

2 Likes

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.

How do I do that? I am not so good in scripting.

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.

With cylinder that looks hard, but how will I do it with a cube?
Here is a screenshot:
Part inside cube

I want it so if you try moving the part outside the cube it stays inside the cube and doesn’t go out.

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.

1 Like

I am fine with half the part being outside the bounds. Also I can’t figure out how to do this. Can you give me an example?

Did you want the bounds to be a cylinder, a circle, a rectangular prism or something else? The shape determines how to constrain it.

I want the bounds on a cuboid so the part doesn’t go out of the cuboid.

If the boundaries have a fixed rotation you just need to do something like this for every axis.

This piece should override the final position of the object

PosX = math.clamp(part.Position.X, bounds.Position.X - bounds.Size.X/2, bounds.Position.X + bounds.Size.X/2)


part.Position = Vector3.new(posX, posY, posZ)

If it does need to be rotated though, you just do the same thing in object space of the boundaries then convert it back to world space.

2 Likes