Skip to content
The Chat Archive
o1-preview-2024-09-12· OpenAI WildChat-4.8M (AI2)

I want you to create a Roblox script that will force a third-person style camera for all…

  1. User

    I want you to create a Roblox script that will force a third-person style camera for all players. The camera should be behind the player and offset so it is behind the player's right shoulder. Moving the mouse should move the players camera instead of moving a cursor across the screen.

  2. Assistant

    Certainly! Below is a Roblox LocalScript that you can place in StarterPlayerScripts or StarterCharacterScripts. This script forces all players into a third-person over-the-shoulder camera view positioned behind the player's right shoulder. Moving the mouse will rotate the camera around the player, and the cursor will be locked to the center of the screen.

    -- LocalScript placed under StarterPlayerScripts or StarterCharacterScripts
    
    local RunService = game:GetService('RunService')
    local UserInputService = game:GetService('UserInputService')
    
    local player = game.Players.LocalPlayer
    local camera = workspace.CurrentCamera
    
    -- Wait for the character to load
    local character = player.Character or player.CharacterAdded:Wait()
    local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
    
    -- Lock the mouse to the center and hide the cursor
    UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
    UserInputService.MouseIconEnabled = false
    
    -- Set the camera type to Scriptable to allow manual control
    camera.CameraType = Enum.CameraType.Scriptable
    
    -- Variables for camera rotation
    local cameraOffset = Vector3.new(1.5, 1.5, -5) -- Adjust these values to fine-tune the camera position
    local rotationX = 0
    local rotationY = 0
    local sensitivity = 0.002
    
    -- Capture mouse movement to rotate the camera
    UserInputService.InputChanged:Connect(function(input)
        if input.UserInputType == Enum.UserInputType.MouseMovement then
            rotationX = rotationX - input.Delta.X * sensitivity
            rotationY = math.clamp(rotationY - input.Delta.Y * sensitivity, -math.rad(75), math.rad(75))
        end
    end)
    
    -- Update the camera position and orientation each frame
    RunService.RenderStepped:Connect(function()
        local rootPosition = humanoidRootPart.Position
        local cameraRotation = CFrame.Angles(0, rotationX, 0) * CFrame.Angles(rotationY, 0, 0)
        local cameraPosition = rootPosition + (cameraRotation:VectorToWorldSpace(cameraOffset))
        camera.CFrame = CFrame.new(cameraPosition, rootPosition + Vector3.new(0, 1.5, 0))
    end)
    

    Explanation:

    • Locking the Mouse:

      • UserInputService.MouseBehavior is set to LockCenter to keep the mouse centered.
      • UserInputService.MouseIconEnabled is set to false to hide the cursor.
    • Camera Control:

      • The camera is set to Scriptable mode, giving the script full control over its position and rotation.
      • Mouse movement is captured using UserInputService.InputChanged, updating rotation variables.
      • The camera's CFrame is updated every frame in RenderStepped based on the player's position and the rotation variables.
      • The cameraOffset vector adjusts the camera's position relative to the player (over-the-shoulder effect).

    Adjusting the Camera Position:

    • You can tweak the cameraOffset values to adjust the exact position of the camera behind the player’s right shoulder.
      • The X value moves the camera left/right.
      • The Y value moves the camera up/down.
      • The Z value moves the camera forward/backward (negative values move it backward).

    Note:

    • This script doesn't rotate the character to face the camera's direction. If you want the character to turn as the camera rotates (like in many third-person games), you can add the following lines inside the RenderStepped function, just before updating the camera's CFrame:

      -- Rotate the character to face the same direction as the camera
      humanoidRootPart.CFrame = CFrame.new(rootPosition) * CFrame.Angles(0, rotationX, 0)
      

      Be cautious when modifying the character's CFrame directly, as it might interfere with character movement and animations.

    Safety and Performance Tips:

    • Always ensure that scripts modifying the camera are LocalScripts to avoid replication issues.
    • Be mindful of performance when using RenderStepped; keep computations efficient.

    With this script, all players will have an over-the-shoulder third-person camera, and moving the mouse will rotate the camera around the character smoothly. The cursor will be locked and hidden, providing an immersive gameplay experience.