Building system: Looking infront

  1. I want achieve a building system where i can look Infront of the block and place a block from the closets block

  2. 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

more detail:


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:

image

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:


image

1 Like

i want it so when you look ahead of the platform it snaps to the closest block infront.

You can use the same code as in the example I linked but make the input position an offset relative to the character:

the offset could be something like HumanoidRootPart.CFrame:PointToWorldSpace(Vector3.new(0, -4, -4))

wow never thought of that ty ill make u solution if this works

i have made it kinda work, im using this to make it snap to the size of 5,5,5

``
local function Snap(Input)

return math.floor(Input / GridSnap) * GridSnap

end
``
but the problem is when i go to the left it snaps to the left because its rounding

use math.round instead of math.floor

1 Like

i want it so when u click it places 1 block infront untill 4 blocks of range, atm it only goes to 1 block forward

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

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.