在Lua中被玩家踩下时减小对象大小

时间:2018-08-09 19:18:53

标签: lua roblox

我想知道如何逐渐增加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)

2 个答案:

答案 0 :(得分:1)

Size.Y表示一个NumberValue,您正在尝试比较并与向量相加。

scriptdir = replace(WScript.ScriptFullName,WScript.ScriptName,"")
Set pso = CreateObject("Scripting.FileSystemObject") 
Set Pile = pso.OpentextFile(scriptdir+"\Alarm_logs.txt",8, True)

您可能希望使用lerp并使过渡更平滑。 同样值得一看的是Wiki的功能。 http://wiki.roblox.com

答案 1 :(得分:0)

就像@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)

那应该可以使事情顺利一些。编码愉快!