如何在Roblox Lua中找到角色部分的方向?

我正在为我的角色添加腰带,但无论我的角色面向何方,腰带都朝向同一个方向。我该如何使我的角色根部朝向与腰带相同的方向?

点赞
用户2860267
用户2860267

一个 人形物体的根部 仅仅是一个普通的 Part,所有的 Part 都有一个 CFrame,代表它们在三维世界空间中的位置和方向。所有的 CFrames 都有一个前方方向 LookVector。

如果你要给一个 Character 模型添加一个腰带,我推荐使用模型中的 Part,并且使用它的 CFrame 作为起点,然后进行相应的偏移。

例如:

-- 访问工作区中的 Character 模型
game.Players.PlayerAdded:Connect(function(player)
    player.CharacterAppearanceLoaded:Connect(function(character)

        -- DEBUG : 等待玩家停止掉落
        wait(2)

        -- 选择一个起点
        local hips = character:WaitForChild("LowerTorso", 10)

        -- 创建一个腰带
        local belt = Instance.new("Part")
        belt.Name = "Belt"
        belt.Size = Vector3.new(2,1,2)

        -- 把腰带定位在玩家角色上
        -- 注意 - 尝试改变这些值来调整腰带的位置和旋转
        local positionOffset = CFrame.new(Vector3.new(0, 0, 0))
        local rotationOffset = CFrame.Angles(math.rad(0), math.rad(0), math.rad(0))
        belt.CFrame = hips.CFrame * positionOffset * rotationOffset

        -- 把腰带焊接到玩家身上
        local weld = Instance.new("WeldConstraint", belt)
        weld.Part0 = belt
        weld.Part1 = hips

        -- DEBUG : 给腰带的正面涂上颜色,确保它的方向正确
        local surface = Instance.new("SurfaceGui", belt)
        surface.Adornee = belt
        surface.Face = Enum.NormalId.Front
        local frame = Instance.new("Frame", surface)
        frame.Size = UDim2.fromScale(1, 1)
        frame.Position = UDim2.fromScale(0, 0)
        frame.BackgroundColor3 = Color3.new(0, 0, 1)

        -- 把腰带放入世界中
        belt.Parent = character
    end)
end)
2020-12-22 01:45:53