Need help with camera manipulation/effect

So the issue is that camera doesnt move with the player and you cant move the camera with your mouse. Ive never done camera stuff before (except really simple stuff)

External Media
local CameraService = {}

local Players = game:GetService("Players")
local RunService = game:GetService("RunService")

local cam = workspace.CurrentCamera
cam.CameraType = Enum.CameraType.Scriptable

local function EaseInSine(x)
	return 1 - math.cos((x * math.pi)/2)
end

local function EaseInQuad(x)
	return x * x
end

local function EaseInCubic(x)
	return x * x * x
end

local function MakeEasing(inFunc)
	return {
		In = inFunc,
		Out = function(x) return 1 - inFunc(1 - x) end,
		InOut = function(x)
			return x < 0.5 and inFunc(x*2)/2 or 1 - inFunc((1 - x)*2)/2
		end
	}
end

local EASING = {
	Sine   = MakeEasing(EaseInSine),
	Quad   = MakeEasing(EaseInQuad),
	Cubic  = MakeEasing(EaseInCubic),
	Linear = {
		In = function(x) return x end,
		Out = function(x) return x end,
		InOut = function(x) return x end
	}
}

local ActiveEffects = {}

function CameraService:AddEffect(func)
	table.insert(ActiveEffects, func)
	return func -- token for removal
end

function CameraService:RemoveEffect(token)
	for i, v in ipairs(ActiveEffects) do
		if v == token then
			table.remove(ActiveEffects, i)
			break
		end
	end
end

function CameraService:AddRotationOffsetTemp(pitch, yaw, time, easingStyle, easingDirection)
	local half = time / 2
	local t = 0

	local style = EASING[easingStyle.Name] or EASING.Linear
	local ease = style[easingDirection and easingDirection.Name or "InOut"] or style.InOut

	local token
	token = self:AddEffect(function()
		if t >= time then
			CameraService:RemoveEffect(token)
			return CFrame.new()  -- reset to the default state once finished
		end

		local dt = RunService.RenderStepped:Wait()
		t += dt

		local alpha = (t <= half) and (t / half) or (1 - ((t - half)/half))
		alpha = ease(math.clamp(alpha, 0, 1))

		-- make sure both directions are covered smoothly
		return CFrame.Angles(
			math.rad(pitch) * alpha,  
			math.rad(yaw) * alpha, 
			0
		)

	end)

	return token
end

function CameraService:Shake(power, time)
	local t = 0
	local token
	token = self:AddEffect(function()
		if t >= time then
			CameraService:RemoveEffect(token)
			return CFrame.new()
		end

		t += RunService.RenderStepped:Wait()

		local x = (math.random() - 0.5) * power
		local y = (math.random() - 0.5) * power

		return CFrame.Angles(math.rad(x), math.rad(y), 0)
	end)
	return token
end

function CameraService:Recoil(upAngle, recoveryTime)
	local angle = upAngle
	local token
	token = self:AddEffect(function()
		if angle <= 0 then
			CameraService:RemoveEffect(token)
			return CFrame.new()
		end

		local dt = RunService.RenderStepped:Wait()
		angle -= (upAngle / recoveryTime) * dt

		return CFrame.Angles(math.rad(angle), 0, 0)
	end)
	return token
end

RunService.RenderStepped:Connect(function()
	local base = cam.CFrame
	local offset = CFrame.new()

	for _, effectFunc in ipairs(ActiveEffects) do
		offset = offset * effectFunc()
	end

	cam.CFrame = base * offset
end)

return CameraService

1 Like

Based on the video, my assumption is that your camera motion logic is moving around itself rather than around the player.

In broad terms:
For moving with the player, when you set the cam.CFrame, have the player’s current position in WorldSpace as one of your reference points for where your base should be. The camera’s position is always going to be an offset from where the player currently is in the world, so motion is all about having the camera follow and zoom out appropriately.

For moving with the mouse, I’d look at the API docs for UserInputService, as those will help you get the proper X and Y deltas of the mouse, if it’s being held. With these deltas, you could convert them into a 3D rotational angle that you apply onto your camera. After the rotation, you would then apply the positioning of the camera (zooming it out from the initial humanoid position)

I don’t want to self-promote, but if you are interested, I’ve written a Roblox camera in the past that’s open-sourced if you want to comb through it and see the general logic.

ill look into your camera and just kinda apply what you did to mine, cuz i wanna learn some new stuff thanks :)

1 Like