So I’m making a board game that has an overhead camera, and is 3D, but I’m having an issue trying to locate where the mouse last position is and what axis it’s moving from…
I made a 2D grid on the client so I can get every tile and reposition accordingly, but the issue here is, when I move on the X axis (left-right) the model ends up moving .25 studs increment instead of .5 studs, and that’s because I’m sending on the wrong direction when moving left to right.
Here’s a video to show what I mean, moving the ships up and down use the .5 stud increment, but left to right uses .25…
local function generateTileGrid(tilesModel)
local grid = {}
for _, tile in tilesModel:GetChildren() do
if tile:IsA("BasePart") then
local x = math.round(tile.Position.X / 0.5)
local z = math.round(tile.Position.Z / 0.5)
grid[z] = grid[z] or {}
grid[z][x] = tile
end
end
return grid
end
local function getClosestTile(result, grid)
local closest, closestDist = nil, math.huge
for _, row in grid do
for _, tile in row do
local dist = (tile.Position - result.Position).Magnitude
if dist < closestDist then
closest = tile
closestDist = dist
end
end
end
return closest
end
local function getTiles(grid, startTile, modelLength, direction)
local occupied = {}
local startX = math.round(startTile.Position.X / 0.5)
local startZ = math.round(startTile.Position.Z / 0.5)
for i = 0, modelLength - 1 do
local x, z = startX, startZ
if direction == "Z" then
z += i
else
x += i
end
if not grid[z] or not grid[z][x] then
return nil -- out of bounds
end
table.insert(occupied, grid[z][x])
end
return occupied
end
local grid = generateTileGrid(tilesModel)
runService.RenderStepped:Connect(function()
local result = mouseRaycast()
if not result then return end
local hitPart = result.Instance
if not hitPart then return end
if not isPlacing then
if hitPart and hitPart.Name == "Hitbox" then
local shipModel = hitPart.Parent
if shipModel:IsA("Model") then
hoveredShip = shipModel
createHiglight(hoveredShip)
end
else
deleteHighlight(hoveredShip)
hoveredShip = nil
end
else
if selectedShip then
local closestTile = getClosestTile(result, grid)
if closestTile then
local shipSizeZ = selectedShip:GetExtentsSize().Z
local tilesNeeded = math.round(shipSizeZ / 0.5)
local shipTiles = getTiles(grid, closestTile, tilesNeeded, "Z")
if shipTiles then
local finalTile = shipTiles[math.ceil(#shipTiles / 2)]
local height = selectedShip:GetExtentsSize().Y / 2
local targetPos = finalTile.Position + Vector3.new(0, height, 0)
local target = CFrame.new(targetPos)
selectedShip:PivotTo(target)
end
end
end
end
end)
Any help would be appreciated (I’m using userinputservice for raycasting and trying to get the mouse position
)