I am making a building system for my game, for which I need to make a clamping function.
This function should clamp the hit position of the player´s mouse for a part following the mouse to stay within specific bounds (the player´s baseplate) so it doesn´t clip out on any side in any rotation.
My problem here, is that I don´t know how to execute this. I´ve prepared some variables for myself (such as the part´s and baseplate´s Look- and RightVectors and their negatives) but I don´t exactly know how to use them.
I am thinking about:
-
getting the hit position relative to the baseplate
-
clamping that position along x and z so that the max value is (baseplateSize-(partSize/2))
This would work, but only if the part couldn´t rotate, and even so could work on only most sides.
My current progress:
function clamp(hit,obj:Model)
local part = obj.PrimaryPart
local partSize = part.Size/2
local partFront = part.CFrame.LookVector
local partBack = -partFront
local partRight = part.CFrame.RightVector
local partLeft = -partRight
local plateFront = platePart.CFrame.LookVector
local plateBack = -plateFront
local plateRight = platePart.CFrame.RightVector
local plateLeft = -plateRight
return CFrame.new()
end
(I really don´t know how to write proper StackOverflow questions, please ask me for more context/information if needed)