I’ve got everything about it done. Whenever you go into a building, the roof of the building and goes half-transparent and the overall system works well. But, I want the transparency thing to be fully automatic because what I am doing right now is coding each building so that the roof goes transparent when the player is inside. This is very inefficient because I am going to have a lot of buildings and having to code for each building seems a bit pointless so I came up with an idea that I can make this work. I thought that I could raycast from the center of the camera and make everything on it’s way transparent although I will need some guidance.
My questions are?
Is this method even good? If not what should I do?
How would I raycast from the center of the screen (camera) towards the player so I can make everything on its way transparent
local camera = game.Workspace.CurrentCamera
local direction = (somecharacter.Head.Position - camera.CFrame.Position).Unit * 100 --this will raycast for 100 studs
I guess you could do torso, but I think head would be easier. If you do Torso, you need to account for R6 and R15 characters (if you allow both).
local camera = game.Workspace.CurrentCamera
local direction = (game:GetService("Players").LocalPlayer.Character.Head.Position - camera.CFrame.Position).Unit * 100
game:GetService("RunService").RenderStepped:Connect(function()
local ray = Ray.new(
camera.CFrame,
direction
)
local ignore = game.Players.LocalPlayer.Character
local hit, position = workspace:FindPartOnRay(ray, ignore)
if hit and hit:IsA("BasePart") then
hit.Transparency = 0.8
end
end)
I kind of did it. It works alright with buildings that dont have too many parts (ofcourse I can create a filter and everything). Imma just post the script here. Is there anything you reckon I can improve it with? (And thank you so much for the help:
wait()
local camera = game.Workspace.CurrentCamera
local direction = (game:GetService("Players").LocalPlayer.character.Head.Position - camera.CFrame.Position).Unit * 100
local Parts = {}
game:GetService("RunService").RenderStepped:Connect(function()
local ray = Ray.new(
camera.CFrame.Position,
direction
)
local ignore = game.Players.LocalPlayer.Character
local hit, position = workspace:FindPartOnRay(ray, ignore)
if hit and hit:IsA("BasePart") then
table.insert(Parts, 1, hit)
hit.Transparency = 0.8
end
for i,v in pairs(Parts) do
if hit ~= v then
v.Transparency = 0
table.remove(Parts, i)
end
end
end)
Oh and is there a way I can make the raycast more thick so that it makes everything in its way transparent rather than a thin line or will I have to cast more than 1 line?
Another Edit: The ray doesnt seem to be casting straight to the player. Not sure.
Sorry to be a pain. I did all your methods that you suggested (except the last one since I am a bit unsure of how to do so) but here is what I came up with:
local camera = game.Workspace.CurrentCamera
local direction = (game:GetService("Players").LocalPlayer.character:WaitForChild("Head").Position - camera.CFrame.Position).Unit * 100
local ignoreList = {game:GetService("Players").LocalPlayer.Character.Head}
local Parts = {}
game:GetService("RunService").RenderStepped:Connect(function()
local hit = workspace.CurrentCamera:GetPartsObscuringTarget(direction, ignoreList)
if hit and hit:IsA("BasePart") and hit.Name == "EngineCompatible" then
table.insert(Parts, 1, hit)
hit.Transparency = 0.8
end
for i,v in pairs(Parts) do
if hit ~= v then
v.Transparency = 0
table.remove(Parts, i)
end
end
end)
local camera = game.Workspace.CurrentCamera
local direction = (game:GetService("Players").LocalPlayer.Character:WaitForChild("Head").Position - camera.CFrame.Position).Unit * 100
local Parts = {}
local ignore = {game:GetService("Players").LocalPlayer.Character.Head}
print("decalred all vars and tables")
game:GetService("RunService").RenderStepped:Connect(function()
local castpoints = {game:GetService("Players").LocalPlayer.Character.Head}
local hit = camera:GetPartsObscuringTarget(castpoints, ignore)
for i,v in pairs(hit) do
if v:IsA("BasePart") and v.Name == "EC" then
table.insert(Parts, 1, v)
v.Transparency = 0.8
end
end
for i,v in pairs(Parts) do
if hit.v ~= v then
v.Transparency = 0
table.remove(Parts, i)
end
end
end)
There are no errors. It just doesn’t work. Pls help.
game.Workspace:WaitForChild(game.Players.LocalPlayer.Name) --humanoid may not be loaded in yet
local camera = game.Workspace.CurrentCamera
--local direction = don't need this, but if you did, it needs to be calculated every frame
local Parts = {}
local ignore = {game:GetService("Players").LocalPlayer.Character} --ignore the whole character, not just the head
print("decalred all vars and tables")
game:GetService("RunService").RenderStepped:Connect(function()
local castpoints = {game:GetService("Players").LocalPlayer.Character.Head.Position} --don't do just Head, do Head.Position (Vector3 needed)
local hit = camera:GetPartsObscuringTarget(castpoints, ignore)
local freshparts = {}
for i,v in pairs(hit) do
if v:IsA("BasePart") and v.Name == "EC" then
table.insert(Parts, 1, v) --1 not needed, but ok
v.Transparency = 0.8
freshparts[v] = true
end
end
--as soon as you made the parts transparent, you immediately made them visible again in the code you had
for i,v in pairs(Parts) do
if not freshparts[v] then
v.Transparency = 0
table.remove(Parts, i)
end
end
end)