Hello everyone,
I am making a placement system and I seem to have run into a problem with clamps. I posted a question another day and I got an answer on how to Keep an Object in Bounds. Although the answer provided a solution, it had an opposite effect - it kept the object from going into the plot. I experimented by adding different values, changing signs (from positive to negative), and tried to rewrite the clamp method. I would like an answer with an explanation, so I can refer to it if encounter a similar problem.
Video:
Code:
local function clampToBounds(vector,base)
local offset = vector - base.Position
local sx,sz = base.Size.X, base.Size.Z
return Vector3.new(math.clamp(offset.X, -sx/2, sx/2),0,math.clamp(offset.Z, -sz/2, sz/2))
end
function CalculatePosition()
CheckCollisions(model)
local x = math.floor(mouse.Hit.X / grid + 0.5) * grid
--local y = math.floor(mouse.Hit.Y / grid + 0.5) * grid
local y = 4
if y <= 4 then
y = 4
end
local z = math.floor(mouse.Hit.Z / grid + 0.5) * grid
local canvas = game.Workspace.Canvas
local minX = (canvas.Position.X - canvas.Size.X/2)
local maxX = (canvas.Position.X + canvas.Size.X/2)
local minZ = (canvas.Position.Z - canvas.Size.Z/2)
local maxZ = (canvas.Position.Z + canvas.Size.Z/2)
local pp = model.PrimaryPart
if part then
--if pp.Position.X + pp.Size.X/2 >= minX and pp.Position.X - pp.Size.X/2 <= maxX then
-- if pp.Position.Z + pp.Size.Z/2 >= minZ and pp.Position.Z - pp.Size.Z/2 <= maxZ then
--model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame:Lerp(CFrame.new(x,y,z) * CFrame.Angles(0, math.rad(rotation * rotationDegree),
-- 0), speed))
-- previousPos = model.PrimaryPart.CFrame
-- else
-- model:SetPrimaryPartCFrame(previousPos)
-- end
--end
local newPos = clampToBounds(pp.Position, canvas)
print(newPos)
model:SetPrimaryPartCFrame(model.PrimaryPart.CFrame:Lerp(CFrame.new(x,y,z)*CFrame.new(canvas.Position + newPos) *
CFrame.Angles(0, math.rad(rotation * rotationDegree),
0), speed))
end
end
Thanks for the Help!