How to smoothen out camera?

Hello,

I’m working on a smoother source camera, and right now i’m trying to make it when you look left/right, your screen tilts whichever direction you look, for a more realistic feeling. Here’s a video of it (notice it’s not very smooth)

Here’s my code:



local _Mx = 0;
local _MxLerp = 0;
local _MyLerp = 0;
local _My = 0;
 
hook.Add( "CreateMove", "Camera:GetValues", function( _cmd )
	local _p = LocalPlayer( );
	local tempMx = _cmd:GetMouseX( ) * FrameTime();
	if ( tempMx != 0 ) then
		tempMx = -_cmd:GetMouseX( );
	end

	if ( tempMx != 0 ) then
		_MxLerp = 0;
	end

	_MxLerp = math.Clamp( _MxLerp + 0.02, 0, 1 );

	_Mx = Lerp( _MxLerp, tempMx, 0 );

	if ( _Mx != 0 ) then
		chat.AddText(tostring(_Mx));
	end

	_My = -_cmd:GetMouseX( );

	if ( _My != 0 ) then
		_MyLerp = 1;
	end

	_MyLerp = math.Clamp( _MyLerp - 0.1, 0, 1 );

	_My = Lerp( _MxLerp, 0, _My );

end );

function vx.EngageCamera( ply, pos, angles, fov )
	local view = {};
	view.origin = pos;
	angles:RotateAroundAxis( angles:Forward(), -_Mx/32 )
	view.angles = angles;
	view.fov = 80;
    return view;
end

hook.Add( "CalcView", "EngageCamera", vx.EngageCamera )


This may be a little inefficient somewhere, but I’d like to know, how to smoothen it out (especially when your FPS starts dipping). I’ve already tried multiplying _Mx by FrameTime(), but it didn’t help much.

Thanks!

You may be over-complicating, maybe I am… But, here is a simple rocker/roller system I made just to do what you wanted. Random var names…


//
// The roll view
//
hook.Add( "CalcView", "CameraRoll:CalcView", function( _p, _pos, _ang, _fov, _znear, _zfar )
	local view = {
		origin 		= _pos;
		angles		= _ang;
		fov 		= _fov;
		znear		= _znear;
		zfar		= _zfar;
		drawviewer	= false;
	};

	// The view...
	view.angles.r = DMX;
end );

//
// The Roller
//
// Vars
local DMX = 0; -- Roll value
local DMX_ADJUST_OVER_TIME = 0.1; -- The multiplier for how quickly it rolls
local DMX_REBOUND_OVER_TIME = 25; -- the multiplier based on time for how fast it rebounds
local DMX_MAX = 10; -- max roll
local DMX_MIN = 10; -- min roll
hook.Add( "CreateMove", "CameraRoll:CreateMove", function( _cmd )
	// Mouse Delta X
	local _deltaX = _cmd:GetMouseX( );

	// Amount to roll
	local _amount = ( DMX_ADJUST_OVER_TIME * FrameTime( ) ) * _deltaX;

	// Rebound amount
	local _rebound = ( DMX_REBOUND_OVER_TIME * FrameTime( ) );

	// Update the roll value
	DMX = DMX + _amount;

	// Clamp between min and max to avoid the rocking back and forth - and approach 0 based on rebound
	DMX = ( DMX > 0 ) && math.Clamp( DMX - _rebound, 0, DMX_MAX ) || ( ( DMX < 0 ) && math.Clamp( DMX + _rebound, -DMX_MIN, 0 ) || 0 )
end );