This is probably so simple, but I literally cannot figure this out after an hour of trying and looking thinigs up.
SO, I’m following this tutorial to create a placement system: Creating A Furniture Placement System
Everything works fine, except I want it so that no matter how the canvas part is rotated around the up and down axis:
This is what I want to happen:
This is what is currently happening:
This is the function in question from the article that calculates the cframe:
(in my code I removed rotation and set it to zero so that players cannot rotate the placements)
function Placement:CalcCanvas()
local canvasSize = self.canvasPart.Size
-- convert to world space
local cf = self.canvasPart.CFrame * CFrame.fromMatrix(-BACK_CFRAME*canvasSize/2, RIGHT_CFRAME, TOP_CFRAME, BACK_CFRAME)
-- use object space vectors to find the width and height
local size = Vector2.new((canvasSize * RIGHT_CFRAME).magnitude, (canvasSize * TOP_CFRAME).magnitude)
return cf, size
end
function Placement:CalcPlacementCFrame(position)
-- use other method to get info about the surface
local cf, size = self:CalcCanvas()
-- rotate the size so that we can properly constrain to the surface
local modelSize = self.model.PrimaryPart.Size
modelSize = Vector3.new(math.abs(modelSize.x), math.abs(modelSize.y), math.abs(modelSize.z))
-- get the position relative to the surface's CFrame
local lpos = cf:pointToObjectSpace(position)
-- the max bounds the model can be from the surface's center
local maxBounds = (size - Vector2.new(modelSize.x, modelSize.z))/2
-- constrain the position using size2
local xClamp = math.clamp(lpos.x, -maxBounds.x, maxBounds.x)
local yClamp = math.clamp(lpos.y, -maxBounds.y, maxBounds.y)
return cf * CFrame.new(xClamp, yClamp, -modelSize.y/2) * CFrame.Angles(-math.pi/2, 0, 0)
end