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

为何GetTouchingParts()在无碰撞部件时仍返回部件?

问题:Roblox中GetTouchingParts()在视觉无碰撞时仍返回接触部件

主调试脚本

local bird = script.Parent
bird.Anchored = true
local uis = game.UserInputService
local player = game.Players.LocalPlayer
local gravity = -1
local Parts = bird:GetTouchingParts()
local bird_flap = bird.Position + Vector3.new(0, 10, 0)
local script_start = false
uis.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton1 then
        script_start = true
    end
    if script_start then
        while not Parts do
            task.wait(0.5)
            gravity = gravity * 2
            uis.InputBegan:Connect(function(input)
                if input.UserInputType == Enum.UserInputType.MouseButton1 then
                    bird.Position = bird_flap
                    gravity = -1
                end
            end)
            bird.Position = bird.Position + Vector3.new(0, gravity, 0)
            task.wait(0.1)
        end
    end
end)

测试排查脚本

local bird = script.Parent
local parts = bird:GetTouchingParts()
if not parts then
    print("no parts")
else
    print("parts touching")
end

尽管bird视觉上处于悬浮无碰撞状态,测试脚本仍输出“parts touching”,疑惑为何GetTouchingParts()会返回部件。


原因分析

  1. 返回值判断逻辑错误:GetTouchingParts()在无接触时返回的是空数组{}而非nil,Lua中空数组属于“真”值,所以if not parts的判断永远不成立,会直接走到else分支输出“parts touching”。
  2. 变量未实时更新:主脚本中Parts变量仅在脚本加载时获取一次接触部件,后续bird移动后不会重新获取,导致循环条件while not Parts永远无法触发。
  3. 重复绑定输入事件:在while循环内反复调用uis.InputBegan:Connect()会生成大量重复事件绑定,每次点击会触发多次逻辑,引发混乱。

修正建议

  • 修正测试脚本的空数组判断逻辑:

    local bird = script.Parent
    local parts = bird:GetTouchingParts()
    if #parts == 0 then
        print("no parts")
    else
        print("parts touching")
        -- 打印具体接触部件,排查实际碰撞对象
        print("Touching parts:")
        for _, part in ipairs(parts) do
            print("-", part.Name)
        end
    end
    
  • 重构主脚本逻辑,解决变量更新和重复绑定问题:

    local bird = script.Parent
    bird.Anchored = true
    local uis = game.UserInputService
    local gravity = -1
    local bird_flap = bird.Position + Vector3.new(0, 10, 0)
    local script_start = false
    
    -- 只绑定一次鼠标点击事件
    uis.InputBegan:Connect(function(input)
        if input.UserInputType == Enum.UserInputType.MouseButton1 then
            script_start = true
            bird.Position = bird_flap
            gravity = -1
        end
    end)
    
    -- 单独开启下落逻辑循环,实时检测碰撞
    task.spawn(function()
        while true do
            task.wait(0.1)
            if script_start then
                local currentTouchingParts = bird:GetTouchingParts()
                if #currentTouchingParts == 0 then
                    gravity = gravity * 2
                    bird.Position = bird.Position + Vector3.new(0, gravity, 0)
                else
                    -- 碰撞后停止循环或添加自定义逻辑
                    break
                end
            end
        end
    end)
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.20 00:26:00