Roblox 触摸事件只针对脚部

我在学习 Lua 和 Roblox,并测试我的第一个脚本。 我想知道正确的方法来处理当角色用脚接触一个块时的 触摸事件,**(无论是走路还是跳跃)**

local function onTouch(hit)
    if hit ~= ??user.legs?? then
        return
    end
    -- 示例动作
    if hit.Parent.Humanoid.JumpPower < 150 then
        hit.Parent.Humanoid.JumpPower = hit.Parent.Humanoid.JumpPower + 5;
    end
end

script.Parent.Touched:connect(onTouch)
点赞
用户88888888
用户88888888

如果你想处理玩家与部件接触的碰撞,你的代码是可以的,但如果你想检测玩家是否站在地面上,那么不行。

一个更好的方法如下:

示例:

IsOnGround=function()
local b=false;
local range=6;
local char=game:service("Players").LocalPlayer.Character;
local root=char:WaitForChild("HumanoidRootPart",1);
if root then
local ray=Ray.new(root.CFrame.p,((root.CFrame*CFrame.new(0,-range,0)).p).unit*range);
local ignore={char};
local hit,pos=workspace:FindPartOnRayWithIgnoreList(ray,ignore,false,false);
pcall(function()
if hit then
b=true;
end
end)
else
print("root not found");
end
return b;
end

然而,这种方法并不是最可靠的,不喜欢 R15 角色,也不喜欢走路。

一种行之有效的方法,也很容易使用,是FloorMaterial。

FloorMaterial是Character的Humanoid属性。如果玩家站在没有东西上面(换句话说,没有接触地面!),这个属性将为nil。这个方法可以放在循环中,不断检测是否站在一个块上。这个方法也适用于R15和R6,比使用.Touched连接更少出故障。

示例:

    coroutine.wrap(function()
        while wait()do
            local floor=humanoid.FloorMaterial
            if(tostring(floor)=="Enum.Material.Air")or(floor==nil)then
                print("on air");
            else
                print("stepping over something");
            end
        end
    end)()

希望我的回答有所帮助。

2018-07-24 20:06:09