How do i get the highest point of the part, considering the part rotation?

So bassicaly i want get the highest point of a part, you guys will probaly say “just divide the y axis by 2” it just work if the part isnt rotated, if you rotate that wont work anymore, i find a code that shows the lowest point

local function getDir(v)
    return (
        ((v.Y == 0) and Vector3.new()) or -- flat
        ((v.Y > 0) and -v) or -- flip
        v -- perfect
    )
end

local function computeLowestPoint(part)
    
    local cf = part.CFrame
    local dist = part.Size/2
    
    local xVec = getDir(cf.RightVector) * dist.X
    local yVec = getDir(cf.UpVector) * dist.Y
    local zVec = getDir(cf.LookVector) * dist.Z
    
    return (cf + xVec + yVec + zVec).p
    
end

iam bad with math so i dont know how find the highest instead.

1 Like

This works for me, idk if it will work for your application

local dim = part.Size / 2
local corners = {
	part.CFrame * CFrame.new(dim.X, dim.Y, dim.Z),
	part.CFrame * CFrame.new(dim.X, dim.Y, -dim.Z),
	part.CFrame * CFrame.new(dim.X, -dim.Y, dim.Z),
	part.CFrame * CFrame.new(dim.X, -dim.Y, -dim.Z),
	part.CFrame * CFrame.new(-dim.X, dim.Y, dim.Z),
	part.CFrame * CFrame.new(-dim.X, dim.Y, -dim.Z),
	part.CFrame * CFrame.new(-dim.X, -dim.Y, dim.Z),
	part.CFrame * CFrame.new(-dim.X, -dim.Y, -dim.Z)
}

table.sort(corners,function(a,b) return a.Position.Y > b.Position.Y end)
local tempPoint = corners[1]
1 Like

i like to use attachments

local function HighestPoint(part : BasePart)
	if part:FindFirstChildWhichIsA("Attachment") then part:FindFirstChildWhichIsA("Attachment"):Destroy() end
	local att = Instance.new("Attachment",part)
	att.Visible = true
	att.WorldPosition += Vector3.new(0,30,0)
	att.Position = Vector3.new(math.clamp(att.Position.X,-part.Size.X/2,part.Size.X/2),math.clamp(att.Position.Y,-part.Size.Y/2,part.Size.Y/2),math.clamp(att.Position.Z,-part.Size.Z/2,part.Size.Z/2))
	
	att:Destroy()
	return att.WorldPosition
end

Solution without attachments

local function HighestPoint(part : BasePart)
	local Pos = part.CFrame:ToObjectSpace(workspace.Part.CFrame + Vector3.new(0,30,0)).Position
	return Vector3.new(math.clamp(Pos.X,-part.Size.X/2,part.Size.X/2),math.clamp(Pos.Y,-part.Size.Y/2,part.Size.Y/2),math.clamp(Pos.Z,-part.Size.Z/2,part.Size.Z/2))
end

Last update: Find out it works for lowest point too

local function LowestPoint(part : BasePart)
	local Pos = part.CFrame:ToObjectSpace(workspace.Part.CFrame - Vector3.new(0,30,0)).Position
	return Vector3.new(math.clamp(Pos.X,-part.Size.X/2,part.Size.X/2),math.clamp(Pos.Y,-part.Size.Y/2,part.Size.Y/2),math.clamp(Pos.Z,-part.Size.Z/2,part.Size.Z/2))
end

Hello, this post was like a godsend to me since I needed help finding the highest point on a part, and it seems like all you had to do to fix this script was simply invert the Logical Operator at this section to (v.Y < 0)

This would make the script look something like this:

local function getDir(v)
	return (
		((v.Y == 0) and Vector3.new()) or -- flat
			((v.Y < 0) and -v) or -- flip
			v -- perfect
	)
end

local function computeHighestPoint(part)

	local cf = part.CFrame
	local dist = part.Size/2

	local xVec = getDir(cf.RightVector) * dist.X
	local yVec = getDir(cf.UpVector) * dist.Y
	local zVec = getDir(cf.LookVector) * dist.Z

	return (cf + xVec + yVec + zVec).p

end

I hope I helped you!

1 Like

Oh yeah, just to add up to this now that I’m at it, a more compact form of writing the first function would be the following:

local function getDir(v)
	return v*math.sign(v.Y)
end
1 Like