Help with making a peaking script for horror game (head tilting to peak around walls)

Goals:

I want a smooth but quick motion that either accelerates then slowly stops or accelerates and overshoots, losing energy as it slowly goes to the wanted position.

What I’ve tried:

I have tried implementing a dampened spring system to make the head peaking better, but it didn’t go to plan. :frowning:

What I want yall to do:

Don’t give me a script or a system, tell me how I’d do it. (I am very new to programming on roblox, so I am trying to learn)

Script:

--Made by ArticFox
--Place in StarterPlayer ---> StarterCharacterScripts

--Variables--
local cam  = game.Workspace.CurrentCamera
local val = 0 --The Z value that the head turns to
local e = false
local q = false
local headMove = 5 --Amount that val changes
local dampening = 0.86 --The amount dampening applied to val
local plr = game.Players.LocalPlayer
local char = plr.Character or plr.CharacterAdded:Wait()
local human = char:WaitForChild("Humanoid")
local animationId = "9742421861" --sets up anim id
local animation = Instance.new("Animation") -- creates anim
animation.AnimationId = "rbxassetid://"..animationId --adds the id
local animationTrack = human:LoadAnimation(animation) --Loads the animation
local AnimationId = "9742428682"
local Animation = Instance.new("Animation")
Animation.AnimationId = "rbxassetid://"..AnimationId
local AnimationTrack = human:LoadAnimation(Animation) --Loads the animation

--Survice Functions--
--IMPORTANT, DO NOT EDIT UNLESS YOU KNOW WHAT YOU ARE DOING!!!--

game:GetService("UserInputService").InputBegan:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		e = true
	end
	if input.KeyCode == Enum.KeyCode.Q then
		q = true
	end
end)

game:GetService("UserInputService").InputEnded:Connect(function(input)
	if input.KeyCode == Enum.KeyCode.E then
		e = false
	end
	if input.KeyCode == Enum.KeyCode.Q then
		q = false
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if e then
		animationTrack:Play()
		--val = math.clamp(val-headMove,-headMoveMax, headMoveMax)
		val -= headMove
		val *= dampening
	end
	if q then
		AnimationTrack:Play()
		--val = math.clamp(val+headMove, -headMoveMax, headMoveMax)
		val += headMove
		val *= dampening
	end
	if q and e then
		animationTrack:Stop()
		AnimationTrack:Stop()
		if val > -0.1 and val < 0.1 then
			val = 0
		else
			if val > 0 then
				--val -=headMove
				val *= dampening
			else
				--val +=headMove
				val *= dampening
			end
		end
	end
	if not q and not e then
		animationTrack:Stop()
		AnimationTrack:Stop()
		if val > -0.1 and val < 0.1 then
			val = 0
		else
			if val > 0 then
				--val -=headMove
				val *= dampening
			else
				--val +=headMove
				val *= dampening
			end
		end
	end
	cam.CFrame = cam.CFrame * CFrame.Angles(0, 0, math.rad(val)) --Turns The head
end)

thanks for the help! :slight_smile: