Block placement system not positioning correctly

Like this:
image
spawn on top of the surface

Video of the current code:


It spawns half way inside.

The attachment has the wrong orientation. See the picture I posted. Yellow arrow should point out of the surface.

I tried it but it didn’t work. It didn’t change anything.
image

Anything wrong with my code?

local rayOrigin = characterPrimary.Position
local rayDirection = (characterPrimary.CFrame.LookVector - characterPrimary.CFrame.UpVector * placeBlockAngle).Unit * 8
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, placeBlockRaycastParams)

if raycastResult then
	local hitPosition = raycastResult.Position
	local hitPart = raycastResult.Instance

	local surfaceCFrame, surfaceLocalPosition = placer.SnapSurfaceCFrame(raycastResult)
	local ppSpaceAttachmentCFrame = VisualBlock.CFrame:ToObjectSpace(VisualBlock.HandleAttachment.WorldCFrame)
	surfaceLocalPosition = surfaceCFrame * ppSpaceAttachmentCFrame:Inverse()
	VisualBlock.CFrame = surfaceLocalPosition
end

You need to use the correction rotation for attachments:

local rayOrigin = characterPrimary.Position
local rayDirection = (characterPrimary.CFrame.LookVector - characterPrimary.CFrame.UpVector * placeBlockAngle).Unit * 8
local raycastResult = workspace:Raycast(rayOrigin, rayDirection, placeBlockRaycastParams)

if raycastResult then
	local hitPosition = raycastResult.Position
	local hitPart = raycastResult.Instance

	local surfaceCFrame, surfaceLocalPosition = placer.SnapSurfaceCFrame(raycastResult)

	 --Edit
	local correctionRotation = CFrame.Angles(0, math.pi / 2, 0)
	local ppSpaceAttachmentCFrame = draggedModel.PrimaryPart.CFrame:ToObjectSpace(attachmentWorldCFrame * correctionRotation)

	surfaceLocalPosition = surfaceCFrame * ppSpaceAttachmentCFrame:Inverse()
	VisualBlock.CFrame = surfaceLocalPosition
end

Nice! It works very good! Thanks!

1 Like

Does it also prevent it from placing it inside other blocks?

No, you would need separate checking for that. You should find an option to do that which is the minimum work needed for your case. It looks like you might have a grid system in your game which can make this significantly easier. If thats the case make another thread and ask about that there.

Fixed it by using GetTouchingParts!

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