根据距离更改 Y 轴上的位置

Change position on Y axis based on distance

我一直在尝试制作一个游戏,让你在一个正方形中,当你走到两边时,零件会出现并挡住你。

除了一些问题外,我已经达到了它工作正常的地步:

零件在不凸起时会低于正方形,我希望它们在不凸起时可见

跳跃时零件会下降,便于逃脱。

零件涨得太早了

这是处理墙定位的代码。

for _, v in pairs(model:GetChildren()) do
    if string.sub(v.Name,1,4) == "Wall" then
        local walls = {}
        walls[v] = {v.CFrame,Vector3.new(1, 1, 1)}
        game:GetService("RunService").RenderStepped:connect(function()
            if(workspace[game.Players.LocalPlayer.Name]:FindFirstChild("HumanoidRootPart")) then
                local mag = (v.Position - workspace[game.Players.LocalPlayer.Name]:FindFirstChild("HumanoidRootPart").Position).magnitude
                sizeFactor = math.floor(mag)
                v.CFrame = walls[v][1]*CFrame.new(0,-sizeFactor+(walls[v][1].Y*1.8),0)

            end
        end)    
    end
end

你可以在这里看到我的游戏:https://www.roblox.com/games/400391033/Marble-walls

查看注释代码。

                for _, v in pairs(model:GetChildren()) do
                if string.sub(v.Name,1,4) == "Wall" then
                    local walls = {}
                    walls[v] = {v.CFrame,Vector3.new(1, 1, 1)}
                    game:GetService("RunService").RenderStepped:connect(function()
                        if(workspace[game.Players.LocalPlayer.Name]:FindFirstChild("HumanoidRootPart")) then
                            local mag = (v.Position - workspace[game.Players.LocalPlayer.Name]:FindFirstChild("HumanoidRootPart").Position).magnitude
                            if (mag <= 2) then --[[
                                    Currently your issue is that you never actually do ANYTHING regarding magnitude
                                    you essentially change the y-Axis as soon as the player spawns.. hence why it does it too early
                                    kappa
                                ]] 
                                sizeFactor = math.floor(mag)
                                v.CFrame = walls[v][1]*CFrame.new(0,-sizeFactor+(walls[v][1].Y*1.8),0)
                            end;
                        end
                    end)    
                end
            end