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