Make Objects too Close to Camera Disappear

Hello! I was wondering how I could make objects too close to the camera that blocks it’s view disappear and then reappear once it is out of the way.

For example, lets say there is a big tree. I walk behind the tree and it blocks the view of the camera. Is there anyway to make the tree invisible so that way I can see through it? Also I would like it to reappear once it is not blocking the view.

2 Likes

Very very simple.

local Magnitude = (Camera.Position- Object.Position).Magnitude
if Magnitude <= 10 then
	Object:Destroy()
end

You’re just getting the magnitude of the object and the camera’s CFrame, and then checking if it’s equal to or lower than a certain amount.

I didn’t test this, so if you run into issues let me know.

2 Likes

How do I detect the object? Should I send out ray casts or something. I feel like sending raycasts every frame to detect objects would be laggy.

1 Like

Remember he wants it to reappear so you don’t want to just destroy the object.

1 Like

Simple, sorry I didn’t read this. Then you need to use and get the player’s HumanoidRootPart CFrame, and calculate it from there. I thought you where talking about a ViewPort. This would look like this;

--//Services

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Players = game:GetService("Players")

--//Variables

local Player = Players.LocalPlayer
local Camera = game.Workspace.CurrentCamera
local Folder = game.Workspace.Folder -- has parts I guess..

--//Main

RunService.RenderStepped:Connect(function(dt)
	for i,v in pairs(Folder:GetChildren()) do
		if Camera:WorldToViewportPoint(v.Position) then
			if (Player.Character.HumanoidRootPart.Position - v.Position).Magnitude <= 10 then
				v.Parent = ReplicatedStorage.Folder -- some object to parent it to other than WS
			end
		end
	end
	--loop to bring em' back
	for i,v in pairs(ReplicatedStorage.Folder:GetChildren()) do
		if (Player.Character.HumanoidRootPart.Position - v.Position).Magnitude > 10 then
			v.Parent = Folder -- back to workspace!
		end
	end
end)

2 Likes

Is the native version of this not sufficient enough for you? If you set StarterPlayer.DevCameraOcclusionMode to Invisicam, it automatically makes parts translucent when they’re blocking your camera. I don’t believe there is a whitelist for it though to make exceptions (thus combining zoom and invisicam), so that might be worth an investigation.

2 Likes

I also want it only if it is a certain distance from the camera, not all parts. Plus I want it to be semi transparent. I actually have found I great forum on it!

3 Likes

where does this go? is it startercharacter?

1 Like