I’m trying to fix the grid snapping feature for when the player hovers their mouse over a terrain cell while having the shovel equipped. I’m using a test version of this tool and I’m gonna make it better but for right now it’s represented as the block that the player holds. The hovering effect is also represented as a block that assumes the position of the terrain cell when the mouse is hovering over the terrain cell.
The problem I’m having is when the mouse is over the terrain cell, sometimes the grid position of the terrain cell get’s mistaken and the hover-part gets positioned outside the cell it’s supposed to assume.
Example of the hovering in it’s correct state:
Example of the hovering in it’s incorrect state:
I’m 90% sure the reason it misplaces the hover part is primarily because I’m using mouse.hit and then translating it over to the terrain grid which is purely integers/real numbers. Yes, the mouse may be hitting a cell but the translation can misinterpret which grid point the mouse is supposed to be selecting.
My Code:
local UIS = game:GetService("UserInputService")
local tool = script.Parent.Parent
local ter = workspace.Terrain
local mouseHover_
local mouseClick_
local lastHover
local part = workspace.HoverPart
tool.Equipped:Connect(function(mouse)
local isHovering = false
mouseHover_ = UIS.InputChanged:Connect(function(input)
local TargetInstance = mouse.Target
if TargetInstance and TargetInstance:IsA("Terrain") then
if lastHover ~= ter:WorldToCell(mouse.Hit.Position) then
local cell = ter:WorldToCell(mouse.Hit.Position)
part.Position = ter:CellCenterToWorld(cell.X,cell.Y,cell.Z)
lastHover = cell
end
end
end)
end)
tool.Unequipped:Connect(function()
mouseHover_:Disconnect()
end)
Given everything said above, I’m wondering if there is a better way to go about doing this?
-
Must use the mouse.
-
Must snap to terrain grid when hovering.