Object clipping into the ground while using mouse.hit to move a part

Hey there I was trying to make a block placement system. but then i encountered this issue:



I’ve tried to use OverlapParams. but its very confusing.
heres my code:


local Tool = script.Parent

local RunService = game: GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game: GetService("Players").LocalPlayer

local Mouse = Player:GetMouse()

local Template = ReplicatedStorage.Parts.Part

local Overlap = OverlapParams.new()
Overlap.FilterType = Enum.RaycastFilterType.Exclude

local function SnapTo(X : number, Increment : number)
	return math.floor(X / Increment) * Increment
end

local function VectorSnapped(Vector : Vector3, Increment : number)
	return Vector3.new(
		SnapTo(Vector.X, Increment),
		SnapTo(Vector.Y, Increment),
		SnapTo(Vector.Z, Increment)
		
	)
end

local function Equipped()
	print("work")
	local Cloned = Template:Clone() 
	Cloned.Parent = game.Workspace

	Mouse.TargetFilter = Cloned
	
	local run = game["Run Service"].Heartbeat:Connect(function()
		local check = VectorSnapped(Vector3.new(Mouse.Hit.X,Mouse.Hit.Y,Mouse.Hit.Z), 1)
		Cloned.Position = check
	end)
	Tool.Unequipped:Connect(function()
		run:Disconnect()
		Cloned:Destroy()
		return
	end)
end
Tool.Equipped:Connect(function()
	Equipped()
end)
1 Like

That’s because the mouse is on the floor and since a part’s origin is the center, the center will also be on the floor. To avoid this just add or subtract half the part’s size on the final position by using the face the mouse is currently on.

if mouse.TargetSurface == Enum.NormalId.Top then
yourpos += Vector3.new(0, part.Size.Y/2, 0)
elseif mouse.TargetSurface == Enum.NormalId.Left then
--...
end

This will correctly offset the part.
If the part isn’t a rectangle or cube (eg: some mesh or complex model) just act like it is.

2 Likes

what @scavengingbot said is correct, though a faster method would be using Vector3.FromNormalId().

Cloned.Position = Mouse.Hit.Position + Vector3.FromNormalId(Mouse.TargetSurface) * Cloned.Size/2

Mark them as solution tho

2 Likes

Omg I didn’t even know fromnormalid existed.

1 Like

this works great. but theres one more issue
image
i dont know how to fix this

edit: heres a better image:
image

I said you needed to do it per face.
Depending on the face you add or subtract half the part’s size.

That’s why there was a:

--...

In the snippet, to imply that the other cases should follow.

But someone else had a faster solution

Pos = Pos + Vector3.fromNormalId(Mouse.TargetSource) * Size/2

This adds offset accounting for every face.

alright. I will add this. thanks

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