[Help pls] How to make increment for raycast (And more)

Alright so I’m working on this placement system to learn more about it, I have couple problems I want to solve that I couldn’t find on other topics but I hope this one will help and will sure help others having the same issue

With the current placement system that I created using mouse and raycast I don’t really know how I would implement increment system so it’s smoother to build and not have a messy crap that isn’t lined up. Here’s a video:

I also have another problem that I don’t know how to make model be flat on the surface and not go through (also so it doesn’t rotate), I don’t know how to explain it but here’s a video:

Raycast:

	local raycastparams = RaycastParams.new()
	raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastparams.FilterDescendantsInstances = {player.Character}
	raycastparams.IgnoreWater = true
	
	local posting = game.Workspace.Camera:ScreenPointToRay(mouse.X, mouse.Y)
	
	raycasthit = game.Workspace:Raycast(posting.Origin, posting.Direction * 300, raycastparams)

Positioning:

blocting.CFrame = CFrame.new(raycasthit.Position, raycasthit.Position + raycasthit.Normal)

If you can help me with either of those I would really appreciate that, if need more information hollar at me in the replies

1 Like

grid system:

For the rotate part, I’m not quite sure. I use mouse.TargetFilter to ignore that part.

2 Likes

What about model colliding with another model, how can I prevent that?

1 Like

Try this:
If the placing block is on another placed block, get the height of the placed block, then do something like placing block.Position.Y = placing block.Position.Y - placed block.Position.Y. I am not sure, this is the only solution I can think of.

2 Likes

I don’t really get what you mean cuz it’s raycast

blocting:SetPrimaryPartCFrame(CFrame.new(raycasthit.Position, raycasthit.Position + raycasthit.Normal))
1 Like

To avoid any unexpected rotation you should work only with Position (not with the whole CFrame). To keep it aligned, the simplest way is to round to the desired integer.

Here I restrict it to 1 stud

while true do
	task.wait()
	
	local raycastparams = RaycastParams.new()
	raycastparams.FilterType = Enum.RaycastFilterType.Blacklist
	raycastparams.FilterDescendantsInstances = {player.Character, blocting}
	raycastparams.IgnoreWater = true

	local posting = workspace.CurrentCamera:ScreenPointToRay(mouse.X, mouse.Y)

	raycasthit = workspace:Raycast(posting.Origin, posting.Direction * 300, raycastparams)
	
	if raycasthit then
		blocting.Position = Vector3.new(math.floor(raycasthit.Position.X), math.floor(raycasthit.Position.Y) + blocting.Size.Y/2, math.floor(raycasthit.Position.Z))
	end
end

This is how I would restrict it to 2 studs

blocting.Position = Vector3.new(math.floor(raycasthit.Position.X/2)*2, math.floor(raycasthit.Position.Y/2)*2 + blocting.Size.Y/2, math.floor(raycasthit.Position.Z/2)*2)
2 Likes

That actually works, I really needed this and it helps!

But can you also solve this problem so it doesn’t collide with other parts I beg you I can’t figure this out

1 Like

Works perfectly! I just need this collide problem solved and this thread will be finished! I appreciate y’all

1 Like

Ok. Usually in a positioning system we handle Models. Then we simply use the MoveTo method.

Here I change blocting to a model

	if raycasthit then
		local pos = Vector3.new(math.floor(raycasthit.Position.X), math.floor(raycasthit.Position.Y) + blocting.PrimaryPart.Size.Y/2, math.floor(raycasthit.Position.Z))
		blocting:MoveTo(pos)
	end
1 Like

It works!

I appreciate for your help, have a great day!