I am currently making a grid placement system but some strange offset is happening to the model I am trying to move inside the baseplate.
Take care, this is a moduleScript.
Placement_Handler.RoundByThree = function(Number) if math.floor(Number) == Number then if Number%3 == 0 then return Number elseif (Number - 1)%3 == 0 then return Number - 1 elseif (Number + 1)%3 == 0 then return Number + 1 end end return Number end Placement_Handler.placeModel = function(plrMouse, modelVisual, modelHitbox) local LastPosition = CFrame.new(); while true do if plrMouse.Target then if plrMouse.Target.Name == "main_Base" then local mainBase = plrMouse.Target; modelVisual.Parent = mainBase.Parent; local mainBaseSizeX, mainBaseSizeZ = mainBase.Size.X, mainBase.Size.Z; local hitboxSizeX, hitboxSizeZ = modelHitbox.Size.X, modelHitbox.Size.Z; local mainBaseTopLeft = mainBase.CFrame * CFrame.new(Vector3.new(mainBaseSizeX/2, 0, mainBaseSizeZ/2)); local baseAngleX, baseAngleY, baseAngleZ = CFrame.new(mainBase.Position, (mainBase.CFrame*Vector3.new(1, 0, 0))):toEulerAnglesXYZ(); local VectorRotation = CFrame.Angles(baseAngleX, baseAngleY, baseAngleZ); local RoundByThreeX = Placement_Handler.RoundByThree(plrMouse.Hit.p.X - mainBaseSizeX); local RoundByThreeZ = Placement_Handler.RoundByThree(plrMouse.Hit.p.Z - mainBaseSizeZ); if RoundByThreeX < -mainBaseSizeX + hitboxSizeX then RoundByThreeX = RoundByThreeX + hitboxSizeX; elseif RoundByThreeZ < -mainBaseSizeZ + hitboxSizeZ then RoundByThreeZ = RoundByThreeZ + hitboxSizeZ; end local VectorComponent1 = CFrame.new(Vector3.new(mainBaseTopLeft.X, plrMouse.Target.Position.Y, mainBaseTopLeft.Z)); local VectorComponent2 = CFrame.new(Vector3.new(RoundByThreeX, plrMouse.Target.Size.Y/2, RoundByThreeZ))* VectorRotation; local VectorComponent3 = CFrame.new(Vector3.new(-hitboxSizeX/2, modelHitbox.Size.Y/2, -hitboxSizeZ/2)); local NewVector = VectorComponent1*VectorComponent2*VectorComponent3; local RegionVectorMin = NewVector * CFrame.new(Vector3.new(hitboxSizeX/2, -modelHitbox.Size.Y/2, hitboxSizeZ/2)); -- TopLeft local RegionVectorMax = NewVector * CFrame.new(-Vector3.new(hitboxSizeX/2, -modelHitbox.Size.Y/2, hitboxSizeZ/2)); -- BottomLeft / Counterpart local Hitboxes = game.Workspace:FindPartsInRegion3( Region3.new( Vector3.new( math.min(RegionVectorMin.X, RegionVectorMax.X), math.min(RegionVectorMin.Y, RegionVectorMax.Y), math.min(RegionVectorMin.Z, RegionVectorMax.Z) ) + Vector3.new(0.1, 0, 0.1), Vector3.new( math.max(RegionVectorMin.X, RegionVectorMax.X), math.max(RegionVectorMin.Y, RegionVectorMax.Y), math.max(RegionVectorMin.Z, RegionVectorMax.Z) ) - Vector3.new(0.1, 0, 0.1) ), nil, 100 ) local ModelColliding = false; for i, Hitbox in pairs(Hitboxes) do if Hitbox.Name == "Hitbox" and not Hitbox:IsDescendantOf(modelVisual) then ModelColliding = true; end end if not ModelColliding then LastPosition = NewVector; end modelVisual:SetPrimaryPartCFrame(LastPosition); end end wait(); end end
It literally does this weird offset: Click here!
Help would be appreciated!