Trying to get this table to always snap to the surface of a wall, or floor etc, even when rotated. Similar to these, but allow rotation instead
Regular:
Rotated:
Currently this is what I have. Cant figure out a way to adjust this depending on the rotation of the table
-- item size is model:GetExtentsSize()
connections.runConnect = runs.RenderStepped:Connect(function()
local ray = rayFunction(params) -- raycasts from player mouse if computer, and camera if mobile
if ray == nil then return end
local toGrid = module.Snap(ray) -- returns a rounded ray.Position
local cf = CFrame.new(toGrid + (ray.Normal * itemSize / 2)) * module.Pivot -- the rotation of the obj in CFrame.Angles
selectedItem:PivotTo(cf)
end)
If its a model do GetExtentsSize() get the scale on each axis and depending on how the table is oriented use the correct axis as a cframe. (By just adding it to the cframe). Keep in mind you will have do add scale/2
This is the bit I’m unable to solve. How would I do something like this?. Also forgot to mention, in the script “itemSize” is model:GetExtentsSize().
Well im not 100% sure how you would go about it but the most straightforward approach for me is: checking the difference in rotation on object from the place you want it to attach to. And depending on that difference you change the offset. Does your rotation system only rotate in 90 degrees increments?
Sort of onto something using ChatGPT. It does change depending on the rotation, but it is too far from the wall / floor. It also shouldn’t be doing this for the floor as the rotation doesn’t effect it
local function RotateVector(vector, rotationCFrame)
return rotationCFrame:PointToWorldSpace(vector)
end
connections.runConnect = runs.RenderStepped:Connect(function()
local ray = rayFunction(params)
if ray == nil then return end
local toGrid = module.Snap(ray)
local rotationCFrame = selectedItem:GetPivot()
local rotatedSize = RotateVector(itemSize, rotationCFrame)
local adjustedPosition = toGrid + ray.Normal * (rotatedSize.magnitude / 2)
adjustedPosition = adjustedPosition - Vector3.new(0, rotatedSize.y / 2, 0)
selectedItem:PivotTo(CFrame.new(adjustedPosition) * module.Pivot)
end)