【问题标题】:Decrease object size when stepped on by player in Lua在 Lua 中被玩家踩到时减小对象大小
【发布时间】:2019-10-04 23:46:00
【问题描述】:

我想知道如何在 Lua 中逐渐增加对象的大小(每次玩家踩到该对象或执行操作时)。 我的代码如下:

local snowPart = game.Workspace.Snow.SnowPart -- part I want to change
while snowPart.Size.Y == Vector3.new(0, 0, 0) do
    wait(10)
    snowPart.Size.Y = snowPart.Size + Vector3.new(0, 0.7, 0) --increment if the part gets too small
end

function onTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        wait(5)
        snowPart.Size = snowPart.Size.Y - Vector3.new(0, 0.7, 0) --increment the part's size when touched by a player
    end

end
snowPart.Touched:Connect(onTouch)

【问题讨论】:

  • 据我所见,如果snowPart.Size.Y 为 (0, 0, 0),则您当前正在强制 snowPart.Size.Y 为 (0, 0.7, 0),如果有人踩到snowPart,您根据此将大小减小 (0, 0.7, 0)。但是,while 循环的下一次迭代会将大小重置为 (0, 0.7, 0)。你的预期行为是什么? [注意:我不知道 Size 和 Size.Y 在这种情况下是如何工作的,也不知道为什么 Size.Y 是 Vector3 而不是简单的 double]

标签: lua roblox


【解决方案1】:

Size.Y 指的是一个 NumberValue,您正在尝试与向量进行比较和相加。

local snowPart = game.Workspace.Snow.SnowPart -- part I want to change
while snowPart.Size.Y <= 0 do
    wait(10)
    snowPart.Size.Y = snowPart.Size + Vector3.new(0, 0.7, 0) --increment if the part gets too small
end

function onTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChildWhichIsA("Humanoid")
    if humanoid then
        wait(5)
        snowPart.Size = snowPart.Size - Vector3.new(0, 0.7, 0) --increment the part's size when touched by a player
    end

end
snowPart.Touched:Connect(onTouch)

您可能想看看使用 lerp 并让过渡更平滑。 此外,可能值得查看 wiki 的功能。 http://wiki.roblox.com

【讨论】:

    【解决方案2】:

    就像@Evan Wrynn 的回答一样,是的,您正在尝试将只读数字值Size.Y 设置为Vector3。我建议TweenService。 (Some documentation on creating tweens, with an example.)

    所以,这里有一个简单的例子:

    local tweenService = game:GetService("TweenService")
    local snowPart = game.Workspace.Snow.SnowPart -- part I want to change
    while snowPart.Size == Vector3.new(snowPart.Size.X, 0, snowPart.Size.Z) do
        wait(10)
        snowPart.Size = snowPart.Size + Vector3.new(0, 0.7, 0) --increment if the part gets too small
    end
    
    function onTouch(otherPart)
        local character = otherPart.Parent
        local humanoid = character:FindFirstChildWhichIsA("Humanoid")
        if humanoid then
            wait(5)
            local info = TweenInfo.new(.5) -- .5 seconds
            local tween = tweenService:Create(snowPart, info, {
                Size = snowPart.Size - Vector3.new(0, 0.7, 0)
            })
            tween:Play()
            -- If you don't want to wait for the tween, remove this.
            wait(.5)
        end
    
    end
    snowPart.Touched:Connect(onTouch)
    

    这应该会使事情变得顺利一些。编码愉快!

    【讨论】:

      猜你喜欢
      • 2021-10-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多