Top down view game - roofs and walls go transparent when in between player and camera

I am trying to make a top down game that when you enter a building the roofs and walls between the character and the player go transparent for the player to see inside, I have got it to work but it will only work on 1 singular part at a time and it detects parts when it shouldn’t.

I have attached a gif of what it is doing and a picture of the raycasting and also camera script



https://gyazo.com/20b0ebcf2e56d3a1c8b9d9210b6b8b48

could you copy your code

--like this?

First, to fix the ray detecting parts that it shouldn’t, the ray direction should be

workspace.Camera.CFrame.Position - char.HumanoidRootPart.Position

this way the ray will cast in the direction of the camera from the character’s position rather than casting in the direction of the camera from the origin of the world

Now to make it work with multiple parts, you need to make a loop which raycasts, then hides the object that the ray hit and adds it into the raycast filter, and repeats that until the ray no longer hits an object, to prevent the code from hiding an object that’s already hidden, you can just set an attribute to the object which determines if the object is already hidden or not

local rs = game:GetService("RunService")
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local last = nil

rs.RenderStepped:Connect(function()
	
	local rayDirection = workspace.Camera.CFrame.Position
	local rayOrigin = char.HumanoidRootPart.Position
	local raycastparams = RaycastParams.new()
	raycastparams.FilterDescendantsInstances = {char}
	raycastparams.FilterType = Enum.RaycastFilterType.Exclude

	local RaycastResult = workspace:Raycast(rayOrigin, rayDirection, raycastparams)
	
	if last ~= nil and RaycastResult ~= nil and last ~= RaycastResult.Instance then
		local tween1 = game:GetService("TweenService"):Create(RaycastResult.Instance, TweenInfo.new(0.35), {Transparency = 0.35})
		tween1:Play()
	elseif last ~= nil and RaycastResult ~= nil and last == RaycastResult.Instance then
		print("SAME")
		local tween1 = game:GetService("TweenService"):Create(RaycastResult.Instance, TweenInfo.new(0.35), {Transparency = 0.35})
		tween1:Play()
	end
	
	if RaycastResult then
		last = RaycastResult.Instance
		print(RaycastResult.Instance.Name)
		print(last.Name)
	else
		if last then
			local tween3 = game:GetService("TweenService"):Create(last, TweenInfo.new(0.35), {Transparency = 0})
			tween3:Play()
		end
	end

Could you please give me an example of how I would do the loop? I tried something like that earlier but kept having errors and issues with it

Yea sure, here’s an example:
btw make sure to save your project before you run this because I didn’t test it and it might crash

-- at the start of the script
local HiddenObjects = {}

-- the loop

for object, _ in pairs(HiddenObjects) do
	HiddenObjects[object] = false
end

local Raycast = nil
repeat
	Raycast = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	if Raycast then
		if HiddenObjects[Raycast.Instance] == nil then
			Hide(Raycast.Instance)
		end
		
		HiddenObjects[Raycast.Instance] = true
		
		raycastParams:AddToFilter({Raycast.Instance})
	end
until Raycast == nil

for object, hidden in pairs(HiddenObjects) do
	if hidden == false then
		Show(Raycast.Instance)
		
		HiddenObjects[object] = nil
	end
end

Worked a charm except for the part that is meant to make them visible again

Oh, I see, I made a mistake, here’s the updated code

-- at the start of the script
local HiddenObjects = {}

-- the loop

for object, _ in pairs(HiddenObjects) do
	HiddenObjects[object] = false
end

local Raycast = nil
repeat
	Raycast = workspace:Raycast(rayOrigin, rayDirection, raycastParams)
	if Raycast then
		if HiddenObjects[Raycast.Instance] == nil then
			Hide(Raycast.Instance)
		end
		
		HiddenObjects[Raycast.Instance] = true
		
		raycastParams:AddToFilter({Raycast.Instance})
	end
until Raycast == nil

for object, hidden in pairs(HiddenObjects) do
	if hidden == false then
		Show(object)
		
		HiddenObjects[object] = nil
	end
end
1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.