Roblox无限旋转循环

时间:2016-06-28 05:47:05

标签: loops rotation roblox

我正在为我的游戏为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 

2 个答案:

答案 0 :(得分:1)

尝试以下代码。为了正确旋转对象 (修改旋转属性通常不起作用,它类似于position属性,它符合碰撞)你必须使用 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合作过,所以这可能不是最好的方法。

答案 1 :(得分:0)

local runService = game:GetService("RunService")

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

您可以永久更改部件方向的 y 轴(或任何其他轴),以使用 runService.Heartbeat 缓慢旋转(而 True 会执行循环,但旋转速度更快以实现更平滑的旋转)。