Weird behavior with my placement system and normal vectors - Am I doing this right?

After banging my head against a wall (and my keyboard) for an hour or two I’ve finally come up with a decent placement system. It works great and even offsets based on the normal of the surface it’s being placed against, which is just awesome. Except, it’s being a little strange on some surfaces, and it’s not exactly as I dreamed.
Here’s the problem. When I place the part of the floor or wall, its PrimaryPart is positioned just enough away from the wall to be perfectly touching it. As expected. But when I try to place it on the ceiling, its position is offset off of the ceiling by just about 1 object length (so, the size of the model.) Here’s my math.

preview:PivotTo(
	CFrame.new(
	math.round(cast.Position.X),
	math.round(cast.Position.Y),
	math.round(cast.Position.Z)
) * CFrame.Angles(0, math.rad(angle * 90), 0))

local pos, size = preview:GetBoundingBox()
preview:PivotTo(pos + (size/2 * (cast.Normal.Unit)))

(I’m using math.round to get a nice 1-stud grid snap. it hasn’t done anything weird (to my knowledge) and works fine)

Another problem is when I rotate the object (shown above as the “angle” variable), it doesn’t offset based on the rotated object’s position, which is problematic for irregularly shaped (rectangular) objects. I’m not exactly sure how to do this but it’s not my priority right now. I need to know why the heck it’s offsetting so far on the ceiling, but not the floor or walls. If someone can point out my silly 1-line mistake that I’m clearly making, it would be much obliged.
I would very much like to avoid having to create a giant table with all the different normal values in it. I want to use RaycastResult.Normal as often as possible.
Screenshots of my issue are below. Images 1 and 2 are the functioning behavior, and image 3 is it doing the weird ceiling offset thing. I’m thinking maybe my offset is all screwed up somehow, but I’m not sure.



Nevermind, I figured it out. I was using the position of the PrimaryPart to set the object’s position, but I was offsetting by the size of the entire model. I switched to using the PrimaryPart’s size and it worked. yay.
I’d still like to know how to get it to rotate, but I’m sure that’s been done before.

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