You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用:Destroy()销毁部件后,未锚定部件物理异常求助

横版卷轴游戏射击机制物理异常问题

我在横版卷轴游戏中制作了射击机制,让带有Health的部件可被攻击。这些部件被设置为未锚定(Anchored=false),未射击时物理表现正常,但当我射击支撑上方部件的某个部件,在其血量耗尽后调用:Destroy()销毁它时,上方的部件会悬浮在空中且移动卡顿。

补充说明:

  • 我用的是新电脑,能流畅运行《真人快打1》《艾尔登法环》等游戏,排除性能问题
  • 该电脑上的Roblox Studio存在bug,尤其是动画方面
  • 我尝试在Roblox客户端中运行游戏,问题依旧

以下是放置在StarterPlayer.StarterCharacterScripts中的射击功能代码:

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local gui = player.PlayerGui:WaitForChild("GameGui") 
local targetImage = gui:WaitForChild("Target") 
local bulletSpeed = 70 
local damageAmount = 25 
local shootCooldown = 1 
local lastShootTime = 0 

targetImage.Visible = false

local function unanchorBreakableParts()
    local breakableFolder = workspace:WaitForChild("Breakable")
    for _, part in pairs(breakableFolder:GetChildren()) do
        if part:IsA("Part") then
            part.Anchored = false 
        end
    end
end

local function fireBullet(startPosition, targetPosition)
    local bullet = Instance.new("Part")
    bullet.Size = Vector3.new(1, 0.5, 0.5) 
    bullet.Material = Enum.Material.Neon
    bullet.BrickColor = BrickColor.new("Bright yellow") 
    bullet.Position = startPosition
    bullet.Anchored = false
    bullet.CanCollide = false
    bullet.Parent = workspace

    local direction = (targetPosition - startPosition).unit

    bullet.CFrame = CFrame.lookAt(startPosition, startPosition + direction)

    local bodyVelocity = Instance.new("BodyVelocity")
    bodyVelocity.Velocity = direction * bulletSpeed
    bodyVelocity.MaxForce = Vector3.new(math.huge, math.huge, math.huge) 
    bodyVelocity.Parent = bullet

    local connection
    connection = bullet.Touched:Connect(function(hit)
        if hit and hit.Parent and hit.Parent:FindFirstChild("Humanoid") then
            local humanoid = hit.Parent:FindFirstChild("Humanoid")
            local character = player.Character

            if humanoid.Parent ~= character then
                humanoid:TakeDamage(damageAmount)
                bullet:Destroy() 
                connection:Disconnect()
            end
        elseif hit and hit.Parent and hit:FindFirstChild("Health") then
            local health = hit:FindFirstChild("Health")
            if health then
                health.Value = health.Value - damageAmount 

                if health.Value <= 0 then
                    hit:Destroy() 
                end
            end
            bullet:Destroy() 
            connection:Disconnect()
        else
            bullet:Destroy()
            connection:Disconnect()
        end
    end)

    game:GetService("Debris"):AddItem(bullet, 5)
end

mouse.Button1Down:Connect(function()
    unanchorBreakableParts()

    local currentTime = tick()
    if currentTime - lastShootTime < shootCooldown then
        return 
    end

    local mousePosition = Vector2.new(mouse.X, mouse.Y)
    targetImage.Position = UDim2.new(0, mousePosition.X - (targetImage.Size.X.Offset / 2), 0, mousePosition.Y - (targetImage.Size.Y.Offset / 2))
    targetImage.Visible = true

    task.delay(0.3, function()
        targetImage.Visible = false
    end)

    local ray = workspace.CurrentCamera:ScreenPointToRay(mouse.X, mouse.Y)
    local rayTarget = ray.Origin + ray.Direction * 500

    local raycastParams = RaycastParams.new()
    raycastParams.FilterDescendantsInstances = {player.Character} 
    raycastParams.FilterType = Enum.RaycastFilterType.Blacklist

    local raycastResult = workspace:Raycast(ray.Origin, ray.Direction * 500, raycastParams)
    if raycastResult then
        rayTarget = raycastResult.Position 
    end

    local character = player.Character or player.CharacterAdded:Wait()
    local rightArm = character:FindFirstChild("ShootPart")
    local startPosition = rightArm.Position

    local humanoid = character:FindFirstChild("Humanoid")
    local shootAnim = character:FindFirstChild("Animations"):FindFirstChild("Shoot")

    if humanoid and shootAnim and shootAnim:IsA("Animation") then
        local loadedAnim = humanoid:LoadAnimation(shootAnim)
        loadedAnim.Priority = Enum.AnimationPriority.Action3
        loadedAnim:Play()
    else
        warn("Humanoid or Shoot animation not found!")
    end

    fireBullet(startPosition, rayTarget)

    lastShootTime = currentTime
end)

内容的提问来源于stack exchange,提问作者Piraxe

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 22:35:00