Before you read any of this, please note that I’m not cloning Minecraft. I just need the building system for a game I’m making.
So I’m making a building system “similar” to that of Minecraft.
I have run into a problem that I don’t know how to properly raycast to get both the position of the selection box and the position of the part to place.
This is my current code for raycasting and snapping:
function Snap(Position, Offset)
if Position then
return Vector3.new(math.floor(Position.X / TileSize + 0.5) * TileSize, math.floor(Position.Y / TileSize) * TileSize, math.floor(Position.Z / TileSize + 0.5) * TileSize) + Offset
end
end
function HitPosition(Offset)
local RayParams = RaycastParams.new()
UpdateRaycastBlacklist()
RayParams.FilterDescendantsInstances = RaycastBlacklist
local RayDirection = ((Camera.CFrame.Position - Mouse.Hit.Position).Unit * -12.5)
local Result = workspace:Raycast(Camera.CFrame.Position, RayDirection, RayParams)
if Result and Result.Position then
return Snap(Result.Position, Offset)
else
return Vector3.new(0, -100, 0)
end
end
The grid selection has clear problems with it, and I dont even know where to get started for block placement I did think of using the Raycast normal, but I don’t think that would work.
Selection Box incorrect positions:
(In both of these screenshots, the box should be on the stone brick block)
- For this one, I’m aware that the Offset is causing this, however the Offset is needed for the Selection box to not clip through the wall and be off grid with everything else (Plus, I think i would still be having this problem without the Offset)
- And this one, I dont even know whats causing this. Both screenshots are of the same block being hovered over.
The way the box is probably fully correct according to code, but I want it to be pretty much 1:1 with Minecraft, and same with the building.
Saying this again, I’m not cloning Minecraft. I just need the building system for a game I’m making, and I would prefer if it wasn’t jank.
Edit: My grid size is 2.5 if you’re wondering
Edit 2: Managed to fix the Y snapping being off, updated the supplied code. X or Z offset (second image) is still a problem though.
Edit 3: Y snapping is wrong when you look at the floor, selection box is not on the block. Same problem as Image 2.