local function constrain(pos,targ,size)
local halfsize = size/2
local add,sub = pos+halfsize,pos-halfsize
if targ>add then
return add
elseif targ<sub then
return sub
end
return targ
end
local function constrainV3(pos,targ,size)
targ = pos + (CFrame.new(pos,targ).LookVector.Unit*size)
return Vector3.new(constrain(pos.X,targ.X,size.X),constrain(pos.Y,targ.Y,size.Y),constrain(pos.Z,targ.Z,size.Z))
end
should be pretty self explainatory,
pos = green part position
targ = grey part position
size = green part size
*this isn’t going to be completely accurate + dont ask me how it works + there is a better way using linear algebra which i couldnt be bothered to code (: