I currently have a DragDetector on a part within a model. After looking at the DragDetector documentation and somewhat translating this tutorial, this block of code seems complete.
My goal is to have the limits be the canvas below it (the gray part). I’ve gotten the math down on clamping the proposed motion’s position. What would I put to return the CFrame value?
In this video, I show the proposed motion printed aside the clamped values, which shows that the math works, and the “Allowed Motion” stops at the bounds of the canvas.
https://i.gyazo.com/9f801afc0eba88563229c974a844c026.mp4
local function constraint(proposedMotion: CFrame, canvas: BasePart, gridPart: BasePart, startPartPosition)
if startPartPosition == nil then
return proposedMotion
end
-- GET CANVAS SIZE --
local canvasSize = canvas.Size
-- BUILD CFRAME --
local back = Vector3.new(0, -1, 0)
local top = Vector3.new(0, 0, -1)
local right = Vector3.new(-1, 0, 0)
-- CONVERT TO WORLD SPACE --
local cf = canvas.CFrame * CFrame.fromMatrix(-back*canvasSize/2,right,top,back)
local size = Vector2.new((canvasSize * right).Magnitude, (canvasSize * top).Magnitude)
-- GET MODEL SIZE --
local modelSize = CFrame.fromEulerAnglesYXZ(0,0,0) * gridPart.Size
modelSize = Vector3.new(math.abs(modelSize.X), math.abs(modelSize.Y), math.abs(modelSize.Z))
-- GET THE MODEL'S BOUNDS --
local objectPosition = cf:PointToObjectSpace(gridPart.Position)
local size2 = (size - Vector2.new(modelSize.X, modelSize.Z))/2
-- CONSTRAIN USING CLAMP --
local x = math.clamp(objectPosition.X, -size2.X, size2.X)
local z = math.clamp(objectPosition.Y, -size2.Y, size2.Y)
local worldPosition = cf * CFrame.new(x,z,-modelSize.y/2) * CFrame.Angles(-math.pi/2,0,0)
local newRefTranslation = worldPosition.Position - startPartPosition
print("Proposed Motion: "..tostring(proposedMotion.Position))
print("--------")
print("Allowed Motion: "..tostring(newRefTranslation))
print("--------")
return proposedMotion -- What should I return here? <-
end
Some return values I’ve tried.
return CFrame.new(newRefTranslation) * proposedMotion.Rotation -- Doesn't move
return proposedMotion.Rotation + newRefTranslation -- Doesn't move
Thank you