Part spawns in the ground when it's placed using mouse.Hit.position

When I click to spawn a part using my script, the part is in the ground, when I place it from the side, it’s cut in half.

image

When I tried multiplying the Y position, if you clicked from the bottom, the new part spawned inside another one, or when you placed it from the side it still was cut in half.

local tool = script.Parent

local function spawnPart()
	local mouse = game.Players.LocalPlayer:GetMouse()
	local raycastParams = RaycastParams.new()

	local raycastResult = game.Workspace:Raycast(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 100, raycastParams)
	if raycastResult then
		local part = Instance.new("Part")
		part.Position = raycastResult.Position
		part.Anchored = true
		part.Parent = game.Workspace
	end
end

tool.Activated:Connect(spawnPart)
1 Like

add half of the height of the part to its y position

1 Like

Well for the part clipping to the floor, you just add half of the part’s Y-Axis value to the positon:
NewPart.Position = raycastResult.Position + (NewPart.Size.Y/2)

This will fix for placing for the ground only. It wont fix for clipping onto the side of an object and it will be offsetted incorrectly (in the Y-Axis) when being placed on the side of the part.

I’d say your best bet would be to raycast from each side and see if it collides with a part, if it does, apply the proper amount of offset (In this case, half of the axis).

Edit)
Additionally, if it fits your needs, make a placement grid to snap the parts together based off its surface. I wouldn’t be able to give you the details without trying it myself, but im sure with enough tinkering you could figure it out.

1 Like

just a tip, use
NewPart.Position += (NewPart.Size.Y/2)
instead

wait nvm I misread

1 Like

Very true. I keep on forgetting that operator works on Vector3’s as well.

My apologies, I misread. I read it like this:

NewPart.Position = NewPart.Position + (NewPart.Size.Y/2)

but instead it was like this…

NewPart.Position = raycastResult.Position + (NewPart.Size.Y/2)

Yea I just realized that right now, no worries :sob:

1 Like

I wonder if you could move backwards on a ray, though? That would fix clipping from both the top and sides

Maybe you could invert the lookvector of the ray and move it in that direction

That’s a lot simpler than what I had in mind. Do that then add half of the part’s size to make that work, so something like this I believe?:

local invertedLookVector = mouse.UnitRay.Direction * -1
local offset = invertedLookVector + (part.Size/2) -- Not sure if you can divide a vector --
part.Position = raycastResult.Position + offset

What if we were to add half the size of the part to our position, then pass that position to the closestpoint? that might give a satisfactory result

wouldn’t you need to do

-1 / lookvector

to invert it

I looked it up to make sure and the formula is indeed -1/a

I wouldn’t be able to give much input on that since I did not know that was a thing

It basically does the same thing, If you were to divide 5 by -1, you get -5, If you were to multiply 5 by -1, you get -5

I didn’t either lol, I just found that on the documentation

I’m on my phone right now and I’m not guaranteed to respond

nono, you’re dividing the -1 BY the lookvector, not the other way around

Now that I think of it, you’re probably right

I wonder how games like build a boat do it

They have a grid system I believe (It’s been a while since I’ve played that game). I’d assume by rounding the position of the mouse to a whole number to determine the stud or tile.

local tool = script.Parent

local function spawnPart()
	local mouse = game.Players.LocalPlayer:GetMouse()
	local raycastParams = RaycastParams.new()

	local raycastResult = game.Workspace:Raycast(mouse.UnitRay.Origin, mouse.UnitRay.Direction * 100, raycastParams)
	if raycastResult then
		local part = Instance.new("Part")
		part.Position = raycastResult.Position + Vector3.new(0, part.Size.Y / 2, 0) --This adds half the part's height to the Y position so it doesn't get stuck underground
		part.Anchored = true
		part.Parent = game.Workspace
	end
end

tool.Activated:Connect(spawnPart)