Roblox 无限旋转循环

Roblox infinite rotating loop

我正在为我的游戏制作 Roblox 健康包。代码已经完成并且运行良好,但我希望健康包本身以一种很酷的方式缓慢旋转所以这是我的代码告诉我哪里出了问题

local healthPack = script.Parent
local healAmount = 30
local cooldown = 5
local canHeal = true

local function handleTouch(otherPart)
    local character = otherPart.Parent
    local humanoid = character:FindFirstChild('Humanoid')
    if humanoid and canHeal then
        if game.Workspace.Player1.Humanoid.Health == 100 then

            print("You have enough health")

        else

        canHeal = false

        game.Workspace.HealthPack.Transparency = .75

            local currentHealth = humanoid.Health
            local newHealth = currentHealth + healAmount
            humanoid.Health = newHealth
            wait(cooldown)
            canHeal = true
            game.Workspace.HealthPack.Transparency = 0 

        end
    end
end

healthPack.Touched:connect(handleTouch)


while true do
    local currentRotationX = game.Workspace.HealthPack.Rotation.X
    --local currentRotationX = game.Workspace.HealthPack.Rotation.Y
    local currentRotationZ = game.Workspace.HealthPack.Rotation.Z

    game.Workspace.HealthPack.Rotation.X = currentRotationX + 10
    --game.Workspace.HealthPack.Rotation.Y = currentRotationY + 10  
    game.Workspace.HealthPack.Rotation.Z = currentRotationZ + 10

    wait(.5)
end 

试试下面的代码。为了让一个物体正确旋转(修改旋转属性通常是不行的,它类似于位置属性,符合碰撞)你必须使用 CFrame.

local x = 0
while true do
  game.Workspace.HealthPack.CFrame = game.Workspace.HealthPack.CFrame * CFrame.Angles(0, math.rad(x), 0)
  x = x + 1
  wait()
end

公平的免责声明,我有一段时间没有使用 RBX.Lua,所以这可能不是最好的方法。

local runService = game:GetService("RunService")

runService.Heartbeat:Connect(function()
    script.Parent.Orientation += Vector3.new(0,0.2,0)
end)

您可以永久更改零件方向的 y 轴(或任何其他轴)以使用 runService.Heartbeat 缓慢旋转(虽然 True do 循环但更快以获得更平滑的旋转)。