如何让武器和工具在 Roblox studio 中停留在屏幕上?

How to make arms and tools stay on screen in Roblox studio?

在诸如幻影之力或任何 FPS 之类的游戏中,如果您向上或向下看,武器和工具将保留在屏幕上。在新的 Roblox 工作室项目中,默认情况下不会发生这种情况。基本上我想让手臂和工具跟随相机的旋转。

这个可以,但是你想让其他玩家看到玩家把枪对准镜头吗?

local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer

local Character = workspace:WaitForChild(Player.Name)
local Root = Character:WaitForChild("HumanoidRootPart")

local Neck = Character:WaitForChild("UpperTorso"):FindFirstChildOfClass("Motor6D")

local YOffset = Neck.C0.Y

local CFNew, CFAng = CFrame.new, CFrame.Angles
local asin = math.asin

game:GetService("RunService").RenderStepped:Connect(function()
    local CameraDirection = Root.CFrame:toObjectSpace(Camera.CFrame).lookVector
    if Neck then
        if Character.Humanoid.RigType == Enum.HumanoidRigType.R15 then
            Neck.C0 = CFNew(0, YOffset, 0) * CFAng(0, -asin(CameraDirection.x), 0) * CFAng(asin(CameraDirection.y), 0, 0)
        elseif Character.Humanoid.RigType == Enum.HumanoidRigType.R6 then
            Neck.C0 = CFNew(0, YOffset, 0) * CFAng(3 * math.pi/2, 0, math.pi) * CFAng(0, 0, -asin(CameraDirection.x)) * CFAng(-asin(CameraDirection.y), 0, 0)
        end
    end
end)

此示例仅适用于 R15

如果你不想让玩家看到这个,那就从客户端创建一个枪的模型,然后把它贴在相机上

local Camera = workspace.CurrentCamera
local Player = game.Players.LocalPlayer

local Character = workspace:WaitForChild(Player.Name)
local Root = Character:WaitForChild("HumanoidRootPart")

NAMES = {
    screen_gun = "Local_Gun",
    model= "Gun",
    view = "view"
}
--- For Player
local Gun = {
    screen = Instance.new("ScreenGui",Player:FindFirstChildOfClass("PlayerGui")),
    obj=Instance.new("ViewportFrame",Player:FindFirstChildOfClass("PlayerGui"):WaitForChild("ScreenGui")),
    part =Instance.new("Part",Player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("ViewportFrame")),
    mesh = Instance.new("SpecialMesh",Player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("ViewportFrame"):WaitForChild("Part")),
    offset = UDim2.new(0.7,0,0.6,0),
    cam = Instance.new("Camera",Player:WaitForChild("PlayerGui"):WaitForChild("ScreenGui"):WaitForChild("ViewportFrame")),
    offset2 = CFrame.new(Vector3.new(1,1,1),Vector3.new(0,0,0)),
    size_view = UDim2.new(0,300,0,300)
}
Gun.obj.CurrentCamera=Gun.cam
Gun.part.Position = Vector3.new(0,0,0)
Gun.obj.Position = Gun.offset
Gun.obj.Size = Gun.size_view
Gun.obj.BackgroundTransparency = 1
Gun.cam.CFrame = Gun.offset2
Gun.screen.Name = NAMES.screen_gun
Gun.part.Name = NAMES.model
Gun.obj.Name = NAMES.view
Gun.part.Size = Vector3.new(1,1,2)
--
Gun.obj.Visible = false


local ToolInHand = false
Character.ChildAdded:Connect(function(obj)
    if obj:IsA("Tool") and ( obj:FindFirstChildOfClass("Part") or obj:FindFirstChildOfClass("MeshPart") ) then  --
        if obj:FindFirstChildOfClass("MeshPart") then
            obj:FindFirstChildOfClass("MeshPart").LocalTransparencyModifier = 1
            Gun.mesh.MeshId = obj:FindFirstChildOfClass("MeshPart").MeshId

        elseif obj:FindFirstChildOfClass("Part") then
            obj:FindFirstChildOfClass("Part").LocalTransparencyModifier = 1

        end
        Gun.obj.Visible = true
        ToolInHand = true
    end
end)
Character.ChildRemoved:Connect(function(obj)
    if obj:IsA("Tool") and ( obj:FindFirstChildOfClass("Part") or obj:FindFirstChildOfClass("MeshPart") ) then 
        if obj:FindFirstChildOfClass("MeshPart") then
            obj:FindFirstChildOfClass("MeshPart").LocalTransparencyModifier = 0
        elseif obj:FindFirstChildOfClass("Part") then
            obj:FindFirstChildOfClass("Part").LocalTransparencyModifier = 0
        end
        Gun.obj.Visible = false
        ToolInHand = false
    end
end)