Welcome all to the second part of our Community Resource series, here we are making a scriptable camera, that is scripted with interpolative camera movement for added smoothness.
[UPDATED] Resource Number 2
|
Interpolative Camera
local Camera = workspace.CurrentCamera
local Mouse = game.Players.LocalPlayer:GetMouse()
-- Set the camera type to Scriptable
Camera.CameraType = Enum.CameraType.Scriptable
-- Define minimum and maximum rotation limits (in degrees)
local minRotationX = -10 -- Limit how much the camera can look up
local maxRotationX = 10 -- Limit how much the camera can look down
local minRotationY = -15 -- Limit how much the camera can rotate left
local maxRotationY = 15 -- Limit how much the camera can rotate right
-- Variables to store current camera angles
local currentRotationX = 0
local currentRotationY = 0
-- Variable to store the previous mouse position
local lastMousePosition = Vector2.new(Mouse.X, Mouse.Y)
-- Smoothness factor (adjust to make movement smoother)
local smoothFactor = 0.1
-- Function to update the camera based on the mouse movement and attach it to CameraPart
local function updateCamera()
local cameraPart = workspace:WaitForChild("CameraPart") -- Ensure CameraPart exists
if cameraPart then
-- Fix the camera's position to be attached to the CameraPart
local targetPosition = cameraPart.Position + Vector3.new(0, 5, 0) -- Adjust height offset if needed
-- Calculate the change in mouse position
local mousePos = Vector2.new(Mouse.X, Mouse.Y)
local mouseDelta = mousePos - lastMousePosition
-- Update current rotations based on mouse movement
currentRotationX = currentRotationX - mouseDelta.Y * 0.2 -- Invert Y-axis for natural feel
currentRotationY = currentRotationY - mouseDelta.X * 0.2
-- Clamp the rotations to the defined min and max values
currentRotationX = math.clamp(currentRotationX, minRotationX, maxRotationX)
currentRotationY = math.clamp(currentRotationY, minRotationY, maxRotationY)
-- Create target CFrame with clamped rotation and attach it to the CameraPart
local targetCameraCFrame = CFrame.new(targetPosition) * CFrame.Angles(math.rad(currentRotationX), math.rad(currentRotationY), 0)
-- Smoothly interpolate to the target CFrame using lerp
Camera.CFrame = Camera.CFrame:Lerp(targetCameraCFrame, smoothFactor)
-- Update last mouse position
lastMousePosition = mousePos
end
end
-- Continuous update loop to keep the camera following the mouse and attached to the CameraPart
game:GetService("RunService").RenderStepped:Connect(updateCamera)
Short Video Representation
|
Click Here - Streamable
On Your End
-
Create a
LocalScript
and put in inside ofStarterPlayerScripts
and name the script however you’d like -
Copy the script above and paste it inside of the
LocalScript
These scripts are some scripts that will be used in the development of our game, use them as you please, no credit is needed. Come back next time! Thank you!