Lua中对象大小受玩家踩踏时逐渐缩小

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

local snowPart = game.Workspace.Snow.SnowPart -- 我想更改的对象
while snowPart.Size.Y == Vector3.new(0, 0, 0) do
    wait(10)
    snowPart.Size.Y = snowPart.Size + Vector3.new(0, 0.7, 0) -- 如果零件太小,则递增
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) -- 玩家踩踏零件时递增零件的大小
    end

end
snowPart.Touched:Connect(onTouch)
点赞
用户10191806
用户10191806

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

local snowPart = game.Workspace.Snow.SnowPart -- 要更改的 part
while snowPart.Size.Y <= 0 do
    wait(10)
    snowPart.Size.Y = snowPart.Size + Vector3.new(0, 0.7, 0) -- 如果 part 缩小了,就增加它的大小
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) -- 当被玩家触摸时,增加 part 的大小
    end

end
snowPart.Touched:Connect(onTouch)

您可能希望考虑使用lerp让过渡更加顺畅。还建议查看函数的 wiki:http://wiki.roblox.com

2018-08-10 15:26:35
用户11815974
用户11815974

像@Evan Wrynn的答案一样,是的,你正在尝试将“Size.Y”,一个只读的数字值,设置为Vector3。我建议使用TweenService。(一些关于创建缓动的文档,附带示例。

因此,这里是一个简单的例子:

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)

那应该就可以让事情变得平滑一些了。愉快的编码!

2019-10-04 15:46:00