How do I find the top and bottom of a part?

How could I find the CFrame of the top and bottom surfaces of a part? I Already have some code, but not much since this isn’t a super big problem.

part.CFrame * Vector3.fromNormalId(Enum.NormalId.Top)
3 Likes

You can use transformations of the part’s CFrame.

local topSurface = part.CFrame * CFrame.new(0, part.Size.Y / 2, 0) * CFrame.Angles(math.pi / 2, 0, 0)
local bottomSurface = part.CFrame * CFrame.new(0, part.Size.Y / -2, 0) * CFrame.Angles(math.pi / -2, 0, 0)

They will be positioned in the center of the surface and pointing out along the surface normal vector.

1 Like
local topFaceCF = cube.CFrame * CFrame.new(0,cube.Size.Y*2,0)
local bottomFaceCF = cube.CFrame * CFrame.new(0,-cube.Size.Y*2,0)

It’s definitely gotten a lot closer, but it’s about 5 studs above where it should be

Is the part a sphere or a cube?

The part is a cube

filler text: qwertyuiop

You gotta divide it by 2 instead of multiplying it with 2

That is almost exactly the same code that I posted.

@Q_ubit are you using meshes or anything that distort the size of the cube? A basepart’s CFrame is located in the part’s center so adding size / 2 would shift it right up to the surface.

1 Like

I’m not using any meshes, however the size of the part is (1, 1, 25)
I’m also using lerping to set the position of attachments like this:

local resolution = math.ceil(handle.Size.Y / 2)
	local increment = 1 / (resolution - 1)
	
	local topSurface = handle.CFrame 
		* CFrame.new(0, handle.Size.Y / 2, 0) * CFrame.Angles(math.pi / 2, 0, 0)
	local bottomSurface = handle.CFrame
		* CFrame.new(0, handle.Size.Y / -2, 0) * CFrame.Angles(math.pi / -2, 0, 0)
	
	for i = 0, 1, increment do
		local Attachment = Instance.new("Attachment", handle)
		Attachment.Name = "CastPoint"
		Attachment.CFrame = bottomSurface:Lerp(topSurface, i)
		Attachment.Visible = Settings.DebugMode
	end

It looks like the CFrame you set the attachment to needs to be in object space. Change that line to this:

Attachment.CFrame = handle.CFrame:ToObjectSpace(bottomSurface:Lerp(topSurface, i))

Result:
(Made the part bigger to see more attachments.)

Nice, it works now, thanks for all your help :smile:

Just out of curiosity, do you know how I would be able to find the all the surfaces?

-- Top Surface
handle.CFrame * CFrame.new(0, handle.Size.Y / 2, 0) * CFrame.Angles(math.pi / 2, 0, 0)

-- Bottom Surface
handle.CFrame * CFrame.new(0, handle.Size.Y / -2, 0) * CFrame.Angles(math.pi / -2, 0, 0)

-- Front Surface
handle.CFrame * CFrame.new(0, 0, handle.Size.Z / -2)

-- Back Surface
handle.CFrame * CFrame.new(0, 0, handle.Size.Z / 2) * CFrame.Angles(0, math.pi, 0)

-- Left Surface
handle.CFrame * CFrame.new(handle.Size.X / -2, 0, 0) * CFrame.Angles(0, math.pi / 2, 0)

-- Right Surface
handle.CFrame * CFrame.new(handle.Size.X / 2, 0, 0) * CFrame.Angles(0, math.pi / -2, 0)
17 Likes