Gui sway intensity is proportional to the distance between the players camera to their character

The farther away my camera is from the character, the more the gui sways. The exact opposite happens when im in first person mode.
I want to make it so the gui sway is constant regardless of how far away the camera is.

edit: tbh i think the issue lies in the velocity of the camera rather than the distance

Video:

local RunService = game:GetService("RunService")
local ReplicatedStorage = game:GetService("ReplicatedStorage")
local Player = game:GetService("Players").LocalPlayer

local Spring = require(ReplicatedStorage.ModuleScripts.Spring)

local Character = Player.Character or Player.CharacterAdded:Wait()
local Humanoid = Character:WaitForChild("Humanoid")
local Camera = workspace.CurrentCamera

local MainFrame = script.Parent
local originalPosition = MainFrame.Position
local originalRotation = MainFrame.Rotation

local swaySpring = Spring.new(Vector2.new(0, 0))
swaySpring.Speed = 7.5
swaySpring.Damping = 0.4

local rotationSpring = Spring.new(0)
rotationSpring.Speed = 10
rotationSpring.Damping = 0.6

local lastCamPos = Camera.CFrame.Position

RunService.RenderStepped:Connect(function(delta)
	local camPosition = Camera.CFrame.Position
	local camDelta = (camPosition - lastCamPos) / delta
	lastCamPos = camPosition

	local swayInput = Vector2.new(camDelta.X, camDelta.Y) * 0.4

	local rightVector = Camera.CFrame.RightVector
	local moveDir = Humanoid.MoveDirection
	local sideways = moveDir:Dot(rightVector)
	local tiltInput = sideways * 1

	swaySpring.Target = swayInput
	rotationSpring.Target = tiltInput
	
	local offset = swaySpring.Position
	MainFrame.Position = originalPosition + UDim2.new(0, offset.X, 0, offset.Y)
	MainFrame.Rotation = originalRotation + rotationSpring.Position
end)
1 Like

when you zoom your camera moves faster

try subtraction the camdelta by the distance the camera is from the origin of your screen (character)

1 Like

im not sure if i interpreted what you said correctly, but it doesn’t seem to work

sorry abt the laggy video but you can still kinda see whats going on

RunService.RenderStepped:Connect(function(delta)
	local camPos = Camera.CFrame.Position
	local camDelta = (camPos - lastCamPos) / delta
	lastCamPos = camPos
	
	local camToCharDistance = (Character.PrimaryPart.Position - camPos).Magnitude

	local adjustedCamDelta = camDelta - (camToCharDistance * Vector3.new(0,0,0))
	
	local swayInput = Vector2.new(adjustedCamDelta.X, adjustedCamDelta.Y) * 0.4

	local rightVector = Camera.CFrame.RightVector
	local moveDir = Humanoid.MoveDirection
	local sideways = moveDir:Dot(rightVector)
	local tiltInput = sideways * 1 

	swaySpring.Target = swayInput
	rotationSpring.Target = tiltInput

	local offset = swaySpring.Position
	MainFrame.Position = originalPosition + UDim2.new(0, offset.X, 0, offset.Y)
	MainFrame.Rotation = originalRotation + rotationSpring.Position
end)
end)
1 Like
RunService.RenderStepped:Connect(function(delta)
	local camPos = Camera.CFrame.Position
	local camToCharDistance = (Character.PrimaryPart.Position - camPos).Magnitude
	local camDelta = (camPos - (lastCamPos/camToCharDistance)) / delta
	lastCamPos = camPos


	local adjustedCamDelta = camDelta

	local swayInput = Vector2.new(adjustedCamDelta.X, adjustedCamDelta.Y) * 0.4

	local rightVector = Camera.CFrame.RightVector
	local moveDir = Humanoid.MoveDirection
	local sideways = moveDir:Dot(rightVector)
	local tiltInput = sideways * 1 

	swaySpring.Target = swayInput
	rotationSpring.Target = tiltInput

	local offset = swaySpring.Position
	MainFrame.Position = originalPosition + UDim2.new(0, offset.X, 0, offset.Y)
	MainFrame.Rotation = originalRotation + rotationSpring.Position
end)

try that

1 Like

I tried it and this is what happened:

im feeling kinda sleepy rn. ill check up on this post in the morning.

1 Like

ok when you’re back try this

RunService.RenderStepped:Connect(function(delta)
	local camPos = Camera.CFrame.Position
	local camToCharDistance = (Character.PrimaryPart.Position - camPos).Magnitude
	local camDelta = (camPos - lastCamPos) / (delta+(camToCharDistance/10))
	lastCamPos = camPos


	local adjustedCamDelta = camDelta

	local swayInput = Vector2.new(adjustedCamDelta.X, adjustedCamDelta.Y) * 0.4

	local rightVector = Camera.CFrame.RightVector
	local moveDir = Humanoid.MoveDirection
	local sideways = moveDir:Dot(rightVector)
	local tiltInput = sideways * 1 

	swaySpring.Target = swayInput
	rotationSpring.Target = tiltInput

	local offset = swaySpring.Position
	MainFrame.Position = originalPosition + UDim2.new(0, offset.X, 0, offset.Y)
	MainFrame.Rotation = originalRotation + rotationSpring.Position
end)

on relative line four of the code I just provided, change the number ten if the results aren’t good keep messing with it. If the results are just toally bad tho then it’s proably not soluton.

1 Like

sorry for the late reply but i found a better solution for my problem

local lastCamCFrame = Camera.CFrame

RunService.RenderStepped:Connect(function(dt)
	local camCFrame = Camera.CFrame
	local camDelta = lastCamCFrame:ToObjectSpace(camCFrame)
	lastCamCFrame = camCFrame

	local x, y, z = camDelta:ToEulerAnglesXYZ()
-- This is what i added^
	local swayInput = Vector2.new(-math.deg(y*0.7), -math.deg(x)) * 3

	local rightVector = camCFrame.RightVector
	local moveDir = Humanoid.MoveDirection
	local sideways = moveDir:Dot(rightVector)
	local tiltInput = sideways * 1

	swaySpring.Target = swayInput
	rotationSpring.Target = tiltInput

	local offset = swaySpring.Position
	MainFrame.Position = originalPosition + UDim2.new(0, offset.X, 0, offset.Y)
	MainFrame.Rotation = originalRotation + rotationSpring.Position
end)
3 Likes