Simple First and Third Person Camera Module


Simple First and Third Person Camera Module

This module provides a basic, scriptable camera system for Roblox games, allowing players to switch between first-person and third-person perspectives with smooth transitions and mouse-based control. It includes basic third-person collision avoidance.

Features

  • Custom scriptable camera control.
  • Smooth Third Person camera with basic collision detection.
  • First Person camera mode.
  • Seamless switching between modes (default key: V).
  • Mouse-based camera rotation (pitch and yaw).
  • Adjustable mouse sensitivity.
  • Optional character rotation sync in Third Person.
  • Optional mouse cursor locking/freeing.

Video

Module Link: https://create.roblox.com/store/asset/106107911174595/Basic-Camera-Module

How to Use

  1. Place the CameraSystem ModuleScript somewhere accessible from your LocalScripts, typically in ReplicatedStorage or StarterPlayerScripts.
  2. In a LocalScript (e.g., inside StarterPlayerScripts), require the module.
  3. Call CameraSystem.Enable() when the player and their character are ready.

Example LocalScript:

local CameraSystem = require(game.ReplicatedStorage.CameraSystem) -- Adjust path if needed

local Players = game:GetService("Players")
local localPlayer = Players.LocalPlayer

-- Wait for the player's character to be added/loaded
localPlayer.CharacterAdded:Connect(function(character)
    -- You might add waits here for specific character components if needed,
    -- though CameraSystem.Enable() does wait for Head, HRP, etc.
    CameraSystem.Enable(localPlayer)
end)

-- If the character already exists when the script runs
if localPlayer.Character then
    CameraSystem.Enable(localPlayer)
end

-- You can call CameraSystem.Disable() later if you need to turn it off.
-- CameraSystem.Disable()

API Reference

Here are the public functions available in the CameraSystem module:


CameraSystem.Enable(player: Player?)

  • Description: Starts the custom camera system. This function takes control of the Workspace.CurrentCamera and begins updating its position and rotation based on mouse input and the player’s character.
  • Parameters:
    • player (Optional Player): The local player instance. If not provided, it defaults to Players.LocalPlayer.
  • Notes:
    • Must be called from a LocalScript.
    • The module will wait for the character’s Head, HumanoidRootPart, and FaceCenterAttachment before fully enabling.
    • This sets the Workspace.CurrentCamera.CameraType to Enum.CameraType.Scriptable.
    • If the system is already enabled, calling this function again will print a warning.

CameraSystem.Disable()

  • Description: Stops the custom camera system and disconnects all event listeners managed by the module.
  • Parameters: None.
  • Notes:
    • This function does not automatically reset the Workspace.CurrentCamera.CameraType back to Custom or Attach. You may need to handle this manually if another script needs to take control.
    • If the system is not enabled, calling this function will print a warning.

CameraSystem.GetYaw(): number

  • Description: Returns the current horizontal rotation (yaw) of the camera.
  • Parameters: None.
  • Returns: A number representing the current yaw angle in degrees.

CameraSystem.GetPitch(): number

  • Description: Returns the current vertical rotation (pitch) of the camera.
  • Parameters: None.
  • Returns: A number representing the current pitch angle in degrees.

CameraSystem.SetSensitivity(newSensitivity: number)

  • Description: Sets the multiplier used to control how fast the camera rotates in response to mouse movement.
  • Parameters:
    • newSensitivity (number): The new sensitivity value. A higher value means the camera will rotate faster for the same amount of mouse movement.
  • Notes: The default sensitivity is 0.2.

CameraSystem.GetSensitivity(): number

  • Description: Returns the current mouse sensitivity multiplier.
  • Parameters: None.
  • Returns: A number representing the current sensitivity value.

CameraSystem.SetFirstPerson(isFP: boolean)

  • Description: Explicitly sets the camera mode to either first-person or third-person.
  • Parameters:
    • isFP (boolean): Set to true to switch to first-person, false to switch to third-person.
  • Notes: The module also allows toggling this mode using the V key by default.

CameraSystem.IsFirstPerson(): boolean

  • Description: Checks whether the camera is currently in first-person mode.
  • Parameters: None.
  • Returns: A boolean - true if the camera is in first-person, false if in third-person.

CameraSystem.SetTorsoRotation(torsoRotation: boolean)

  • Description: Controls whether the character’s HumanoidRootPart automatically rotates horizontally to match the camera’s yaw in Third Person mode (this setting has no effect in First Person or FreeCam modes).
  • Parameters:
    • torsoRotation (boolean): Set to true to enable character rotation sync, false to disable it.

CameraSystem.SetFreeMouse(isFM: boolean)

  • Description: Controls the behavior of the mouse cursor.
  • Parameters:
    • isFM (boolean): Set to true to allow the mouse cursor to move freely (like the default Roblox cursor), false to lock it to the center of the screen (typical for character/camera control).

Notes

  • This module is intended to be the primary script controlling Workspace.CurrentCamera.CFrame. Using other scripts that also modify the camera’s CFrame simultaneously will cause conflicts and unexpected behavior.
  • The module assumes a standard R15 or R6 character model with a Head, HumanoidRootPart, and a FaceCenterAttachment inside the Head.
  • Default keys for toggling modes (V for FP/TP, C for FreeCam) are hardcoded within the module and cannot be changed via the public API. But feel free to change that.

Feel free to use and modify this module for your game!

7 Likes