Hi, i’m trying to check if a part is visible to the camera.
for some reason, it doesn’t behave as it should.
Here is the script(and what i tried):
local VTC = {}
function getinvisibleparts()
local tev = {}
for i, v in next, script.Parent:GetDescendants() do
if v:IsA("Part") or v:IsA("UnionOperation") or v:IsA("MeshPart") then
if v.Transparency < 0.2 then
table.insert(tev,v)
end
end
end
return tev
end
function clampvector(vetor:Vector3,limit:Vector3)
return Vector3.new(math.clamp(vetor.X,limit.X * -1,limit.X),math.clamp(vetor.Y,limit.Y * -1,limit.Y),math.clamp(vetor.Z,limit.Z * -1,limit.Z))
end
function calculatebound(camera:Camera,part:Part,Threshold:number,Offset:number,ignorechar)
local size = part.Size
local limit = Vector3.new((size.X/2) - Threshold,(size.Y/2) - Threshold,(size.Z/2) - Threshold)
local obsp = part.CFrame:ToObjectSpace(camera.CFrame)
local pos = clampvector(obsp.Position,limit)
--7 pos
local positions = {Vector3.new(pos.X + Offset,pos.Y,pos.Z),Vector3.new(pos.X - Offset,pos.Y,pos.Z),Vector3.new(pos.X ,pos.Y + Offset,pos.Z),Vector3.new(pos.X ,pos.Y - Offset,pos.Z),Vector3.new(pos.X ,pos.Y,pos.Z + Offset),Vector3.new(pos.X ,pos.Y,pos.Z- Offset)}
local finalpos = {pos}
for i, v in next, positions do
table.insert(finalpos,clampvector(v,limit))
end
local resulted = {}
local found = false
for i, v in next, finalpos do
local po,val = camera:WorldToViewportPoint(v)
if val then
local param = RaycastParams.new()
param.FilterType = Enum.RaycastFilterType.Blacklist
param.IgnoreWater = true
local t = getinvisibleparts()
if ignorechar then
table.insert(t,game.Players.LocalPlayer.Character)
end
param.FilterDescendantsInstances = t
local rey = workspace:Raycast(camera.CFrame.Position,v,param)
if rey then
else
table.insert(resulted,v)
found = true
end
end
--table.insert(finalpos,clampvector(v,limit))
end
return finalpos,resulted,found
end
function VTC:CheckIfVisible(camera,part,Threshold,Offset,ignorechar)
if not Threshold then
Threshold = 0
end
if not Offset then
Offset = 0.5
end
local fpos,res,found = calculatebound(camera,part,Threshold,Offset,ignorechar)
return fpos,res,found
end
function VTC:CheckIfVisibleCam(cf,part,Threshold,Offset,ignorechar)
if not Threshold then
Threshold = 0
end
if not Offset then
Offset = 0.5
end
local camera = Instance.new("Camera",workspace)
camera.CFrame = cf
local fpos,res,found = calculatebound(camera,part,Threshold,Offset,ignorechar)
camera:Destroy()
return fpos,res,found
end
return VTC
This goes into a ModuleScript.
The part that the script should detect is the big one but for some reason it just detects a random position.
(red and green cubes are just to see the positions to be detected)