I am making a game like Mashables. There is a building, rotating, moving, and sizing system you can do with objects in Mashables to make robots. I am trying to replicate this in my game. I have decided to start with the sizing system, and it is acting odd.
Here is the problem: It seems like the block is scaling from both sides, and when I scale it to the edge of the screen, it scales very largely. I want to implement an increment.
Video:
Location:
Code:
local scaleHandle = script.Parent
local block = workspace[script.Parent.Adornee.Name]
scaleHandle.MouseDrag:Connect(function(face, distance)
if face == Enum.NormalId.Left or face == Enum.NormalId.Right then
block.Size += Vector3.new(distance, 0, 0)
end
if face == Enum.NormalId.Top or face == Enum.NormalId.Bottom then
block.Size += Vector3.new(0, distance, 0)
end
if face == Enum.NormalId.Front or Enum.NormalId.Back then
block.Size += Vector3.new(0, 0, distance)
end
end)
When I move the handle in the x or z direction, the block gets size in the y as well.
When I move the handle in the y direction, the block gets size in the x as well.
When I move the handle, the handle itself changes position.
I need to add a check in the code to make sure the block doesn’t have a size less than the handle’s size, so the handle stays in the block.
For some reason, I can’t get the handle to rotate with the block. I know how to do this, but I was trying to see if I could get the sizing to work first.
The block is a part. The handle is a cylinder. I made the cylinder invisible, but I sized it to be the size of the block. I hope this is enough information.
I was able to solve the problem. local scaleHandle = script.Parent
local block = workspace[script.Parent.Adornee.Name]
scaleHandle.MouseDrag:Connect(function(face, distance)
if face == Enum.NormalId.Left or face == Enum.NormalId.Right then
block.Size += Vector3.new(distance, 0, 0)
end
if face == Enum.NormalId.Top or face == Enum.NormalId.Bottom then
block.Size += Vector3.new(0, distance, 0)
end
if face == Enum.NormalId.Front or Enum.NormalId.Back then
block.Size += Vector3.new(0, 0, distance)
end
scaleHandle.Size = block.Size
end)
I think that the problem was that the handle was staying the same size as the block was growing. So, I made the handle the same size as the block using the code above.
The code shown above scales the part in both directions.
I suggest you try to piece of code from a scale tool I made not too long ago:
local lastDistance = 0
function scaleSnap(distance, step)
return math.floor(distance / step + 0.5) * step
end
function onScale(face, distance)
local delta = distance - lastDistance
if math.abs(delta) >= block.ResizeIncrement then
local scale = scaleSnap(delta, block)
if block:Resize(face, scale) then
lastDistance = distance
end
end
end
The block:Resize allows you to resize any part from one face without it scaling on both sides (This is what the scale tool uses to resize parts).
Obviously, connect the onScale function to the scaleHandle.MouseDrag event.
And when you hold on one of the scale handles, you reset the lastDistance to 0.
I’m not sure. I suggest you make the block CanCollide off, and when you release one of the handles, you can change it’s collision back to it’s original.
How would I do something like this with position instead?
Edit:
Ended up changing the function with just changing the scale of it normally when its dragged, then subtracting the opposite face with the negative of the scale.
Certainly not the best way, at all, but it works.
function onScale(face, distance)
local delta = distance - lastDistance
if math.abs(delta) >= block.ResizeIncrement then
local scale = scaleSnap(delta, block.ResizeIncrement)
if face == Enum.NormalId.Left then
if block:Resize(face, scale) then
if block:Resize(Enum.NormalId.Right, -scale) then
lastDistance = distance
end
end
end
if face == Enum.NormalId.Right then
if block:Resize(face, scale) then
if block:Resize(Enum.NormalId.Left, -scale) then
lastDistance = distance
end
end
end
if face == Enum.NormalId.Top then
if block:Resize(face, scale) then
if block:Resize(Enum.NormalId.Bottom, -scale) then
lastDistance = distance
end
end
end
if face == Enum.NormalId.Bottom then
if block:Resize(face, scale) then
if block:Resize(Enum.NormalId.Top, -scale) then
lastDistance = distance
end
end
end
if face == Enum.NormalId.Front then
if block:Resize(face, scale) then
if block:Resize(Enum.NormalId.Back, -scale) then
lastDistance = distance
end
end
end
if face == Enum.NormalId.Back then
if block:Resize(face, scale) then
if block:Resize(Enum.NormalId.Front, -scale) then
lastDistance = distance
end
end
end
end
end