How do I snap a block to the center of the mouse? Right now my part keeps being snapped to the edge pivot of the mouse.hit.position
?
This is the module script that controls snapping:
1. local SnapGrid = {}
2. function SnapGrid.Snap(Part, SizeX, SizeY, SizeZ, YOffset)
3.
4. local X = Part.Position.X
5. local Y = Part.Position.Y
6. local Z = Part.Position.Z
7.
8. Part.Position = Vector3.new(math.floor(X/SizeX)*SizeX,math.floor(Y/SizeY)*SizeY+YOffset,math.floor(Z/SizeZ)*SizeZ)
9. print("SimpleSnapGrid - Snapped " .. Part.Name .. " to grid position: " .. tostring(Part.Position))
10.
11. end
12. return SnapGrid
Here is the other script:
1. local Player = game.Players.LocalPlayer
2. local Mouse = Player:GetMouse()
3. local SnapGrid = require(game.ReplicatedStorage:WaitForChild("SimpleSnapGrid"))
4. local Hologram = game.ReplicatedStorage.Hologram:Clone()
5. Hologram.Parent = game.Workspace
6. Mouse.Button1Down:Connect(function()
7.
8. game.ReplicatedStorage.PlaceBlock:FireServer(Mouse.Hit)
9.
10. end)
11. Mouse.Move:Connect(function()
12.
13. local GridSize = 4
14.
15. Hologram.Position = Mouse.Hit.Position
16. SnapGrid.Snap(Hologram, GridSize, GridSize, GridSize, 2)
17.
18. end)
19. Mouse.Idle:Connect(function()
20.
21. local GridSize = 4
22.
23. Hologram.Position = Mouse.Hit.Position
24. SnapGrid.Snap(Hologram, GridSize, GridSize, GridSize, 2)
25.
26. end)
How do you make it centered to the mouse.hit.position
?
Thank you for reading this.