Never tried to make a building system but now im trying to. This is the closest i got, but the objects move via the center of he part/model. I tried fixing this with detecting the face the mouse was hitting, but that stops working when rotation is involved.
local Snap = 0.5
local Camera = game.Workspace.CurrentCamera
local Mouse = game.Players.LocalPlayer:GetMouse()
local Dragging = false
local DragObj = nil
local Snap = 0.25
local Hovering = nil
local function SnapToGrid(position)
return Vector3.new(math.floor(position.X/Snap) * Snap, math.floor(position.Y/Snap) * Snap, math.floor(position.Z/Snap) * Snap)
end
local function Drag(ObjModel : Other)
Dragging = true
DragObj = ObjModel
task.spawn(function()
if ObjModel:IsA("Model") then
for _, V in ObjModel:GetDescendants() do
if V:IsA("BasePart") then
V.Transparency = 0.7
V.CanCollide = false
end
end
while Dragging == true do
Mouse.TargetFilter = ObjModel
local Pos = SnapToGrid(Mouse.Hit.Position)
ObjModel:PivotTo(CFrame.new(Pos))
task.wait()
end
else
ObjModel.Transparency = 0.7
while Dragging == true do
Mouse.TargetFilter = ObjModel
local Pos = SnapToGrid(Mouse.Hit.Position)
ObjModel.Position = Pos
task.wait()
end
end
end)
end
-------------------EVERYTHING AFTER THIS IS JUST FOR TESTING, AND IS NOT FINAL---------------------------------------------
task.wait(1)
Drag(game.Workspace.WoodBlock) --Initalize drag
game.UserInputService.InputBegan:Connect(function(KC) --Placement test
if KC.KeyCode == Enum.KeyCode.E then
if DragObj:IsA("Model") then
game.ReplicatedStorage.PlaceObject:FireServer(DragObj:GetPivot(), DragObj.Name)
else
game.ReplicatedStorage.PlaceObject:FireServer(DragObj.CFrame, DragObj.Name)
end
end
end)
Please help