首页 > 解决方案 > roblox 上的水下相机效果

问题描述

我需要帮助调试此代码,我正在尝试制作一种效果,其中屏幕被染成蓝色,并且每当相机位于名为“水”的部分内时,混响设置为浴室,它适用于 1 水体游戏和其余部分不会导致效果发生,尽管它会为游戏中的每一个水体打印,我假设它选择工作的那个是第一个加载的那个,但我不确定为什么只有一个注册,它会检查所有这些,但只有一个部分实际上会导致它发生

while true do
    wait(0.1)
    for i,v in pairs(game.Workspace:GetChildren()) do
        if v.Name == "water" then
            print ("lolhehehhevljl")
            local parms = RaycastParams.new()
            parms.FilterDescendantsInstances = {v}
            parms.FilterType = Enum.RaycastFilterType.Whitelist
            local ray = require(game.Workspace.Modules.raymodule).raycast(game.Workspace.CurrentCamera.CFrame.Position, v.Position, parms)
            if ray then
                game.Lighting.ColorCorrection.TintColor = Color3.new(1, 1, 1)
                game.SoundService.AmbientReverb = Enum.ReverbType.NoReverb
            else
                game.Lighting.ColorCorrection.TintColor = Color3.new(0, 0.65098, 1)
                game.SoundService.AmbientReverb = Enum.ReverbType.Bathroom
            end
        end
    end
end

标签: luaroblox

解决方案


It could be that your logic is fighting with itself. You are checking that you are underwater for each block, and if one says yes, but the next says no, you will essentially be undoing any changes that should be happening.

So one fix might be to ask all of the water blocks, "am I underwater?" And if any say yes, then change the lighting and reverb, but if ALL of them say no, then change it back.

-- local variables
local Modules = game.Workspace.Modules
local raymodule = require(Modules.raymodule)
local TINT_UNDERWATER = Color3.new(0, 0.65098, 1)
local TINT_NORMAL = Color3.new(1, 1, 1)

-- find all of the water parts
local water = {}
for i, v in pairs(game.Workspace:GetChildren()) do
    if v.Name == "water" then
        table.insert(water, v)
    end
end

-- create a filtered list for raycasting
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.FilterDescendantsInstances = water

-- every frame, check if we are underwater
game.RunService.Heartbeat:Connect(function()
    local isUnderwater = false
    local cameraPos = game.Workspace.CurrentCamera.CFrame.Position

    for i, v in ipairs(water) do
        local didHit = raymodule.raycast(cameraPos, v.Position, raycastParams)
        if not didHit then
            isUnderwater = true
        end
    end

    -- update the camera state
    if isUnderwater then
        game.Lighting.ColorCorrection.TintColor = TINT_UNDERWATER
        game.SoundService.AmbientReverb = Enum.ReverbType.Bathroom
    else
        game.Lighting.ColorCorrection.TintColor = TINT_NORMAL
        game.SoundService.AmbientReverb = Enum.ReverbType.NoReverb
     end
end)

推荐阅读