Placement tool doesn't place in the corrent position

I am currently making a placement tool. There is no placement grid. Here’s the type of thing it should do:

image

However, when the tool encounters slopes it messes up.

image

Notice how it clips into the part. In addition, rotating the object also messes it all up.

image

Here’s the current code for it:

adjustedCFrame = CFrame.new(raycastResult.Position)

adjustedCFrame = adjustedCFrame:ToWorldSpace(CFrame.Angles(math.rad(raycastResult.Instance.rotation.x), math.rad(raycastResult.Instance.rotation.y), math.rad(raycastResult.Instance.rotation.z)))
adjustedCFrame = adjustedCFrame:ToWorldSpace(rotatedCFrame) -- the user can rotate the object. this code applies that rotation.
adjustedCFrame = adjustedCFrame:ToWorldSpace(CFrame.new((clonedPart[1].PrimaryPart.Size.x / 2) * raycastResult.Normal.x, (clonedPart[1].PrimaryPart.Size.y / 2) * raycastResult.Normal.y, (clonedPart[1].PrimaryPart.Size.z / 2) * raycastResult.Normal.z))

It’s quite messy so I’ll explain what it is currently doing. First, it creates a cframe from the mouse’s position in the world. Then it relatively rotates the object so that it’s rotation matches that of the object the mouse is pointing to. Next, it rotates the object corresponding to how the user tells it to. Finally, it offsets the object so that (hopefully) it isn’t clipping into any other parts.

I’ve stared at the code so long I don’t know what it means anymore to be honest. Please help me from my CFrame hell.

1 Like

On the last line, you’re moving the part in all three axes relative to itself, with the goal of moving it out of the ground. Since it’s already rotated so that its up direction is pointing in the direction that it should move to be pushed out of the ground, this is incorrect. You should instead be moving it only on its local Y axis = the ground normal, by half the height of the part. Try this instead:

adjustedCFrame *= CFrame.new(0, clonedPart[1].PrimaryPart.Size.y / 2, 0)

I also changed it to just multiplying by the translation instead of doing aCF = aCF:ToWorldSpace(translation), I’m pretty sure it’s the same. Also try adjustedCFrame += (clonedPart[1].PrimaryPart.Size.y / 2) * raycastResult.Normal.