Roblox模块脚本射线检测仍命中已加入忽略列表的角色问题
问题分析与解决方法
- 核心问题:你的模块脚本中射线检测的调用顺序完全错误——先执行了
workspace:Raycast,之后才创建并配置RaycastParams,而且调用射线检测时根本没传入过滤参数,导致你设置的忽略角色规则完全没生效,射线自然会命中自己Avatar的面具。
修正后的模块脚本
local Raycast = {} function Raycast.Cast(origin, dir, char) -- 先创建并配置射线过滤参数 local Params = RaycastParams.new() Params.FilterType = Enum.RaycastFilterType.Exclude Params.FilterDescendantsInstances = {char} -- 调用射线检测时传入配置好的参数 local raycastResult = workspace:Raycast(origin, dir, Params) return raycastResult end return Raycast
额外优化建议
- 确认面具确实是角色对象的子级:如果面具被放在其他容器中,过滤规则不会生效,可以通过打印
result.Instance:GetFullName()和角色的层级路径来排查。 - 优化伤害判定逻辑:只检查父对象可能无法正确找到目标Humanoid,建议向上查找包含Humanoid的模型,同时额外排除自己的角色:
local result = Raycast.Cast(Character.PrimaryPart.Position, Character.PrimaryPart.CFrame.LookVector * 5, Character) if result then print(result.Instance:GetFullName()) -- 向上查找带有Humanoid的模型 local targetModel = result.Instance:FindFirstAncestorWhichIsA("Model") if targetModel then local h = targetModel:FindFirstChildWhichIsA("Humanoid") if h and h.Parent ~= Character then h.Health -= 5 end end end
内容的提问来源于stack exchange,提问作者Rayyan Rabbi Rahman
相关产品推荐
相关产品推荐

