Need help with model placement with normal offsets

You can write your topic however you want, but you need to answer these questions:

  1. What do you want to achieve?
    I want my clientStructure model to offset so it is flat against the face my ray is hitting. I want it to also work with 90 degree rotations.

  2. What is the issue?
    It works like intended when i dont rotate my clientStructure.
    Example image:

But when i rotate my clientStructure by 90 degrees it doesnt get offset correctly and clips into the face my ray is hitting.
Example image:

  1. What solutions have you tried so far?
    I have looked around at this devforum and tried some solutions which didnt work. I have also tried asking ChatGPT with no succes (it ususally helps) and i have tried changing up some CFrames in the script, but i just wont understand how they work.

Here is the part of my script:

local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientationSnapped), 0)
local newCFrame = CFrame.new(math.round(position.X), position.Y, math.round(position.Z))
local offsetCFrame = CFrame.new(normal * clientStructure.Hitbox.Size * 0.5)
offsetCFrame *= newAnglesCFrame
newCFrame *= offsetCFrame
clientStructure:SetPrimaryPartCFrame(newCFrame)

Try my GridPlacer, which is designed specifically for this kind of issue: GridPlacer: Highly-featured module for part placement, rotation, and snapping to grid

2 Likes

Thanks for letting me know about this, but i would prefer to use something that doesnt require modules. I am going to try this if i cant figure out what to do.

I found the solution to my problem in this post.

And i changed my script to this:

function GetAABB(part)
	local abs = math.abs

	local cf = part.CFrame
	local size = part.Size
	local sx, sy, sz = size.X, size.Y, size.Z

	local x, y, z, R00, R01, R02, R10, R11, R12, R20, R21, R22 = cf:components() 

	local wsx = abs(R00) * sx + abs(R01) * sy + abs(R02) * sz
	local wsy = abs(R10) * sx + abs(R11) * sy + abs(R12) * sz
	local wsz = abs(R20) * sx + abs(R21) * sy + abs(R22) * sz

	return Vector3.new(wsx, wsy, wsz)
end
local newAnglesCFrame = CFrame.Angles(0, math.rad(yOrientationSnapped), 0)
local newCFrame = CFrame.new(math.round(position.X), position.Y, math.round(position.Z))
local offsetCFrame = CFrame.new(normal * GetAABB(clientStructure.Hitbox) * 0.5)
offsetCFrame *= newAnglesCFrame
newCFrame *= offsetCFrame
clientStructure:SetPrimaryPartCFrame(newCFrame)

I hope someone finds this useful

3 Likes

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