在 Roblox 中使用水下相机效果

我需要帮助调试这段代码,我想制作一种效果,即每当相机在名为“water”的部件内时,屏幕会呈现出蓝色,并且回声设置为浴室。它在游戏中的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

原文链接 https://stackoverflow.com/questions/69078815

点赞
stackoverflow用户2860267
stackoverflow用户2860267

可能是由于逻辑上的冲突导致问题。 您正在检查每个积木是否在水下,如果其中一个说是,但下一个说不是,那么您将基本上撤销任何应该发生的更改。

因此,其中一个修复方法可能是询问所有水下积木,“我是否在水下?” 如果有任何积木回答是,则更改灯光和混响,但如果所有积木都回答没有,则将其改回到原始状态。

-- 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)
2021-09-10 23:27:50