Hey DevForum! Getting ready for the holidays?
I’ve been looking at a piece of code for almost a week and I can’t seem to be finding a solution.
I’m making something similar to a Placement System but this rotation problem is confusing me…
local function SnapToGrid(Items,Base,ItemHolder,Rotations,OrigRotation)
local Coordinates = {}
local HitCFrame = Hit({unpack(Items)},Base) -- Ray
if HitCFrame then
local ModelSize, ModelCFrame = GetBoundingBox(Items)
local HitRelativeToBase = Base.PrimaryPart.CFrame:ToObjectSpace(HitCFrame)
HitRelativeToBase = CFrame.new(Round(HitRelativeToBase.X,3),HitRelativeToBase.Y,Round(HitRelativeToBase.Z,3))
ModelSize = CFrame.new(math.abs(ModelSize.X),math.abs(ModelSize.Y),math.abs(ModelSize.Z)) * CFrame.Angles(0,OrigRotation,0)
ModelCFrame = Base.PrimaryPart.CFrame:ToObjectSpace(ModelCFrame)
ModelCFrame = CFrame.new(Round(ModelCFrame.X,3),ModelCFrame.Y,Round(ModelCFrame.Z,3)) * CFrame.Angles(0,OrigRotation,0)
local GridExtents = CFrame.new((Base.PrimaryPart.Size - Vector3.new(ModelSize.X,0,ModelSize.Z))/2) * CFrame.Angles(0,OrigRotation,0)
HitRelativeToBase = CFrame.new(Round(HitRelativeToBase.X,3),0,Round(HitRelativeToBase.Z,3))
HitRelativeToBase = CFrame.new(Round(math.clamp(HitRelativeToBase.X,-GridExtents.X,GridExtents.X),3),HitRelativeToBase.Y,Round(math.clamp(HitRelativeToBase.Z,-GridExtents.Z,GridExtents.Z),3))
for Index, Item in ipairs(Items) do
local RotationalY = Rotations[Index]-OrigRotation -- Table of older rotations subtracted by the rotation given here.
local ItemCFrameOffset = ModelCFrame:ToObjectSpace(Item.PrimaryPart.CFrame)
local ItemCoordinates = (HitRelativeToBase ) * ItemCFrameOffset
ItemCoordinates = CFrame.new(ItemCoordinates.X,((Base.PrimaryPart.CFrame.Y + Base.PrimaryPart.Size.Y/2) + Item.PrimaryPart.Size.Y/2),ItemCoordinates.Z) * CFrame.Angles(0,RotationalY,0)
Coordinates[Index] = ItemCoordinates:ToWorldSpace(Base.PrimaryPart.CFrame)
end
return Coordinates
end
end
This specific part of the code seems to be rerunning the rotation.
for Index, Item in ipairs(Items) do
local RotationalY = Rotations[Index]-OrigRotation -- Table of older rotations subtracted by the rotation given here.
local ItemCFrameOffset = ModelCFrame:ToObjectSpace(Item.PrimaryPart.CFrame) local ItemCoordinates = (HitRelativeToBase ) * ItemCFrameOffset
ItemCoordinates = CFrame.new(ItemCoordinates.X,((Base.PrimaryPart.CFrame.Y + Base.PrimaryPart.Size.Y/2) + Item.PrimaryPart.Size.Y/2),ItemCoordinates.Z) * CFrame.Angles(0,RotationalY,0)
Coordinates[Index] = ItemCoordinates:ToWorldSpace(Base.PrimaryPart.CFrame)
end
Trying to explain it but if OrigRotation
was set to 90 (in radians), it would continue reoffsetting it every RunService.RenderStepped
I can’t even figure out a solution to the repeating offset. If anybody has any way to make my code not repeat the rotation multiple times or make it more efficient. Please leave something down. I’ll explain what else I can if asked for.
Have a happy holiday.