GUI sway effect when moving the camera

I have seen this in many games, when you are in first person, when you move your camera the scope has a like a bounce/sway effect. If you know how to achieve this please tell me.

Video of what I want to achieve:

3 Likes

Using a sin wave or more commonly a spring (hookes law) are good ways to do this sway effect, essentially a player moves mouse to this position, instead of instantly snapping that position to that spot a scripter can use hooke’s law to first accelerate the movement (creating a easing effect) and then actually overshoot the intended position the player was trying to get at (this is good since players arent pixel perfect when aimming)

Recreated this with motion blur as well:

local userInputService = game:GetService("UserInputService");
local runService = game:GetService("RunService");

local scopeFrame = (script.Parent:WaitForChild("Frame"));
local SCOPE_FRAME_POS = (scopeFrame.Position);

function Lerp(a, b, m)
	return a + (b - a) * m
end;

local swayX, swayY = 0, 0;
local SWAY_BLUR = Instance.new("BlurEffect");
SWAY_BLUR.Parent = workspace.CurrentCamera
SWAY_BLUR.Size = 0
local SWAY_SPEED = (5);
function RenderStep(dt)
	userInputService.MouseIconEnabled = false
	local delta = userInputService:GetMouseDelta();	
	swayX = Lerp(swayX, delta.X * 2, dt * SWAY_SPEED)
	swayY = Lerp(swayY, delta.Y * 2, dt * SWAY_SPEED)
	
	SWAY_BLUR.Size = Vector2.new(swayX, swayY).Magnitude
	scopeFrame.Position = SCOPE_FRAME_POS + UDim2.fromOffset(swayX, swayY)
end;

runService.RenderStepped:Connect(RenderStep)

Result:

14 Likes