How to forcefully pan camera?

I’m trying to make a horror game where the “Monster” will stalk you in the woods. When the player looks at the “monster”, the player takes damage. I want to add an effect where the mouse’s sensitivity decreases(already done) and the camera slowly pans towards the “monster”. The players should have to fight against the camera to survive. What would be the best way to achieve this?

Note: The camera has to be smooth.

Any help is appreciated! :relieved:

1 Like

Did you consider using CFrame:Lerp() for the smoothing part?

You can make use of lerp, I personally made a Z-Target system, which basically starts at the point from where the camera is to the final destination, yadda yadda, simple lerp functionality. I’d give you a snippet of code but I feel like that’d be too easy for you, so maybe you can try figuring it out yourself?

To access the lerp function, all you have to do is call it on a CFrame value as if you were accessing a function.

For instance:

Camera.CFrame:lerp( CFrame goal,  number alpha);

And maybe you can wrap it in a RunService loop to achieve your desired effect.

1 Like

Thank you so much, I’ll try it out!

You will want to interpolate the camera between its original point and the new viewpoint. To do this I recommend using linear interpolation, a.k.a. Lerp.

local RS = game:GetService("RunService")

local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local hrp = char:WaitForChild("HumanoidRootPart")

local camera = workspace.CurrentCamera

local monster = workspace.Monster.PrimaryPart -- I.E. HumanoidRootPart

local rawStrength = 1.5

	camera.CameraType = Enum.CameraType.Custom
	local distance = (hrp.Position - monster.Position).Magnitude
	if distance <= 25 then
		local panStrength = math.sqrt(distance)/distance^rawStrength
		local monsterCFrame = CFrame.lookAt(camera.CFrame.Position, monster.CFrame.Position)

		camera.CFrame = camera.CFrame:Lerp(monsterCFrame, panStrength)

Of course, if you need any more help, then I’ll be willing to give you some examples if needed, or any more explanation.

1 Like

I also added a lil extra for you, the strength of the panning is based on the distance between you and the monster!


In the future, you should try to give recommendations as opposed to spoonfeeding code; not only do they not learn anything, but they’ll also make a habit of just getting used to people spoonfeeding them stuff…

Sorry if I’m coming off as rude! I just want to make sure he learns. :grinning_face_with_smiling_eyes: