So I started working on a game similar to creeper chaos. In the game, when you are in build mode you can zoom out through blocks and the blocks that block the player’s character from the camera are made transparent. The goal is the make something similar to that.
The thing I want to make:
When I tried making this system the first clue that came to my mind was using Raycast. I made a ray that starts from the camera and goes in the direction of the player’s rootpart and anything that it hits has its transparency reduced. It doesn’t seem to work very nicely. Following is the script:
local camera = workspace.Camera
local player = game.Players.LocalPlayer
local character = player.Character or player.CharacterAdded:Wait()
local lastpart
game:GetService("RunService").RenderStepped:Connect(function()
if lastpart then lastpart.Transparency = 0 end -- resets transparency back
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.FilterDescendantsInstances = game.Workspace.blocks:GetChildren()
raycastParams.IgnoreWater = true
local raycastResult = workspace:Raycast(camera.CFrame.Position, (character.HumanoidRootPart.Position - camera.CFrame.Position), raycastParams) -- casts ray from camera to player
if raycastResult then
raycastResult.Instance.Transparency = 0.7 --reduce transparency
lastpart = raycastResult.Instance
end
end)
The code in action:
This code seems to work very poorly also I believe that It might not be good for a game to run this as casting so many rays should also cause a huge amount of lag.
I tried searching around the dev forum but couldn’t find anything that helped. I would be thankful if someone would help me out.
There’s a method that’ll save you a lot of writing, it’s called :GetPartsObscuringTarget(), this will probably be more performant and save you from a headache! Let me know if this worked well for you.
Thanks so much. This thing looks like it will be easy to make. A lot easier than the raycasting stuff. Thanks man I’ll test it and let you know if it works.
I don’t know what went wrong but I wrote this code and it doesn’t work:
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
game:GetService("RunService").Heartbeat:Connect(function()
local castPoints = {workspace.CurrentCamera.CFrame.Position, char:FindFirstChild("HumanoidRootPart.Position")}
local ignoreList = char:GetChildren()
local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, ignoreList)
for i, part in pairs(parts) do
part.LocalTransparencyModifier = 0.75
end
end)
Also, in cast points, you don’t need to include the camera’s position. It just checks all the cast points and checks if there’s anything between them and the camera, since your FindFirstChild would nil out, it would just check if there’s anything between the camera and the camera itself, resulting in nothing.
Thank you so much, brother. You helped me a lot. I would also appreciate it if you could kindly tell me how can I make it so that the part’s transparency is set to 0 when the player stops looking at the parts.
I tried using the same lastpart method as I did in the script I started with but it doesn’t seem to work. Thanks in advance.
Thanks again
Personally what I would do is have an array of parts that were affected by the script and, for every check, see if they’re still in the path, if not, change back their transparency.
local char = game.Players.LocalPlayer.Character or game.Players.LocalPlayer.CharacterAdded:Wait()
local cache = {}
game:GetService("RunService").Heartbeat:Connect(function()
local castPoints = {char:FindFirstChild("HumanoidRootPart").Position}
local ignoreList = char:GetChildren()
local parts = workspace.CurrentCamera:GetPartsObscuringTarget(castPoints, ignoreList)
for i, part in pairs(parts) do
part.LocalTransparencyModifier = 0.75
table.insert(cache, part)
end
for _ , part in pairs(cache) do
if not table.find(parts, part) then
part.LocalTransparencyModifier = 0
table.remove(cache, part)
end
end
end)
Not sure if the code works right away or if it’s very well performant, however that’s the only way I could imagine working around this issue. Let me know if it works for ya!