首页 > 解决方案 > 在 Lua 中被玩家踩到时减小对象大小

问题描述

我想知道如何在 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)

标签: luaroblox

解决方案


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


推荐阅读