Smooth CFrame multiply

Im currently making a realistic shooting game and I want to make it add up the tilt amount with the mouse delta.x but it get snappy

local RunService = game:GetService("RunService"); local UserInputSerice = game:GetService("UserInputService"); local UserGameSettings = game:GetService("UserGameSettings")

local Players = game.Players; local LocalPlayer = Players.LocalPlayer; local Character = LocalPlayer.Character; local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local HumanoidRootPart = Character.HumanoidRootPart

local ReplicateStorage = game.ReplicatedStorage; local GunAnimation = ReplicateStorage.GunAnimation; local GunModel = ReplicateStorage.GunModel
local Loadout = require(Character.Loadout)

local cam = game.Workspace.CurrentCamera

local GunViewmodel
local sway = CFrame.new()

local handmoverad = 0.3

local function Equip(changecurrentslot)

	GunViewmodel = GunModel:FindFirstChild(Loadout.slot[changecurrentslot]).Model:clone()
	GunViewmodel.Parent = cam
	
end
local function UnEquip(changecurrentslot)
	
end

RunService.RenderStepped:Connect(function(dt)
	
	local Delta = UserInputSerice:GetMouseDelta()
	
	sway = CFrame.new(sway.X + Delta.X / 555, sway.Y - Delta.Y / 555, 0) * CFrame.Angles(0, 0, sway.Z - Delta.X / 100)
	--sway = CFrame.new(math.clamp(sway.X, -handmoverad, handmoverad), math.clamp(sway.Y, -handmoverad, handmoverad), 0)
	
	
	GunViewmodel.FakeCamera.CFrame = cam.CFrame * sway

end)



Equip("1")

Have you tried using Tween to smoothen up the CFrame changes?

Lerp

local speedofdt = 10
-- the higher the faster the lower the slower, thats it
-- recommended values: 5, 7.5, 10
-- i hate it when people do a whole lotta yap on the devforum

local RunService = game:GetService("RunService"); local UserInputSerice = game:GetService("UserInputService"); local UserGameSettings = game:GetService("UserGameSettings")

local Players = game.Players; local LocalPlayer = Players.LocalPlayer; local Character = LocalPlayer.Character; local Humanoid = Character:FindFirstChildWhichIsA("Humanoid")
local HumanoidRootPart = Character.HumanoidRootPart

local ReplicateStorage = game.ReplicatedStorage; local GunAnimation = ReplicateStorage.GunAnimation; local GunModel = ReplicateStorage.GunModel
local Loadout = require(Character.Loadout)

local cam = game.Workspace.CurrentCamera

local GunViewmodel
local sway = CFrame.new()

local handmoverad = 0.3

local function Equip(changecurrentslot)

	GunViewmodel = GunModel:FindFirstChild(Loadout.slot[changecurrentslot]).Model:clone()
	GunViewmodel.Parent = cam
	
end
local function UnEquip(changecurrentslot)
	
end

RunService.RenderStepped:Connect(function(dt)
	
	local Delta = UserInputSerice:GetMouseDelta()
	
	sway = CFrame.new(sway.X + Delta.X / 555, sway.Y - Delta.Y / 555, 0) * CFrame.Angles(0, 0, sway.Z - Delta.X / 100)
	--sway = CFrame.new(math.clamp(sway.X, -handmoverad, handmoverad), math.clamp(sway.Y, -handmoverad, handmoverad), 0)
	
	
	GunViewmodel.FakeCamera.CFrame = GunViewmodel.FakeCamera.CFrame:Lerp(cam.CFrame * sway, dt*speedofdt)

end)



Equip("1")
2 Likes

I want to make it add up like this but apply with the rotation too, but the lerp just make it slowly fall back to center

I had try but it just fall back not like what I want to achieve (like I want to make it like unrecord)

Two things happening here.

First of all, you are not only affecting camera rotation, but also position with your current code. Since camera position is still controlled by mouse (shiftlock), you get a conflict. That is likely the cause of “snappines” You should either only control camera rotation, or completely take over camera movement via script.

sway = CFrame.Angles(0, 0, sway.Z - Delta.X / 100)

Second of all, by adding (or substracting) sway XYZ value from the previous sway values, you may end up with sway that will never go back to “zero” on its own, even when the mouse is no longer moving. Once player stops moving their mouse, delta will become “0,0” but the sway will reamain at offset.

sway = CFrame.new(sway.X + Delta.X / 555 --Delta.X is zero, but sway.X is not

I suggest to simply use delta on its own:

sway = CFrame.Angles(0, 0, -Delta.X / 100)

But this is probably not the effect that you are trying to achieve. I am guessing you want the tilt to return to normal some time after the delta is already at “0,0”. In that case follow Emper advise, but instead of camera, apply lerp to sway. In this case :

sway = CFrame.Angles(0, 0, sway.RightVector.Y - Delta.X / 100)
sway = sway:Lerp(CFrame.new(),dt)

Edit: Also make sure that sway.RightVector.Y - Delta.X / 100 is clamped, so it never rotates too much.
IE:

sway = CFrame.Angles(0, 0, math.clamp(sway.RightVector.Y - Delta.X / 100,-math.pi/4, math.pi/4))
sway = sway:Lerp(CFrame.new(),dt)

thx man, but I had figure out the better way to do it by just add it as a separate int and then add it to the angle

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.