I want achieve a building system where i can look Infront of the block and place a block from the closets block
I think you have to send a raycast down from the mouse pixel by pixel until it detects the closets block but i think that would lag my game if there are thousands of raycasts always shooting finding the closest block
so I want it when i look ahead with my mouse it finds the closest block to place to, I don’t want scripts I just want someone to give me idea on how you would go with doing that
You can use a raycast and it only has to run on the client. It will not take any server time if done this way. You can send a request to place the block at a specific positon to the server via a remote event. To get the position of the block being placed:
if the player mouses at an existing block, you will get a RaycastResult with a (World) position and normal, if you assume this position and normal sits on the new block and has a normal with opposite direction, you can calculate the position. First add the normal (pointing “in” to the new dotted outline block) times half the block width to the postion, then round this result to your grid. For example if your blocks are 4x4x4:
local blockSize = 4
position += rayResult.Normal * blockSize / 2
position = position / (Vector3.one * blockSize) --Scale down
position = Vector3.new(math.round(position.X), math.round(position.Y), math.round(position.Z)) --Round to grid coordinates
position *= blockSize --Scale back up
--Result is snapped to a 4x4x4 grid
I have an example of how to do this but it just uses a movable sphere as the position input, you just need to add the raycasting:
Cast a ray matching the down part of the arrow in my pic. If it hits a block thats already placed retry it one block further away (double the Z part of the offset) until you’ve checked four blocks ahead. If you find a space with no block place it there.
i made it so you can build under ur self but is there a way to make it to teleport you ontop of the block as my character is getting stuck inside of the block