local function V(V)if V.Magnitude==0 then return Vector3.zero end return V.Unit end local function K(V,K,s,P)local g=P-V local o=g:Dot(K)o=math.clamp(o,-s,s)return V+K*o end local function s(s,P,g,o)local p=K(s.Position,s.UpVector,P*.5,o)local I=(p-o).Magnitude local y=V(o-p)local e=I<=g return e,p+y*g,y end return function(V,K,P,g)do end local o=(K.Y<K.Z and K.Y or K.Z)*.5 local p=K.X V*=CFrame.Angles(math.rad(90),-math.rad(90),0)local I,y,e=s(V,p,o,P)if I then return I,y,e end local S=(y-P).Magnitude I=S<g do end return I,y,e end
--!native
local function SafeUnit(v3)
	if v3.Magnitude == 0 then
		return Vector3.zero
	end

	return v3.Unit
end

local function solve(p0, d0, len, p1)
	local v = p1 - p0
	local k = v:Dot(d0)
	k = math.clamp(k, -len, len)
	return p0 + d0 * k
end

local function ClosestPointFunc(cframe, length, radius, point)
	local l0 = solve(cframe.Position, cframe.UpVector, length * 0.5, point)

	local distance = (l0 - point).Magnitude
	local normal = SafeUnit(point - l0)
	local is_inside = (distance <= radius)

	return is_inside, l0 + (normal * radius), normal
end

return function(CapsuleCFrame, CapsuleSize, Point, Radius)
do end	
local CapsuleRadius = (CapsuleSize.Y < CapsuleSize.Z and CapsuleSize.Y or CapsuleSize.Z) * 0.5
	local CapsuleLength = CapsuleSize.X

	CapsuleCFrame *= CFrame.Angles(math.rad(90), -math.rad(90), 0) -- Optomize

	local IsInside, ClosestPoint, Normal = ClosestPointFunc(CapsuleCFrame, CapsuleLength, CapsuleRadius, Point)

	if IsInside then
		return IsInside, ClosestPoint, Normal
	end

	local DistanceToCp = (ClosestPoint - Point).Magnitude

	IsInside = (DistanceToCp < Radius)
do end	
return IsInside, ClosestPoint, Normal
end
