Viewmodel tilt/lean function

  1. What do you want to achieve?
    A way to understand how to create a lean mechanic and fix this script so i can also do it in the future

  2. What is the issue?
    Am trying to create a tilt/lean mechanic you see in most roblox fps games. When you press E you’re viewmodel and camera and character would rotate somewhere 20-45 degress the direction and you would be able to see others, shot, and be shot at.
    Same goes for when you press D

  3. What solutions have you tried so far?
    I’ve tried looking for a tutorial on youtube and looked in the forums. But either I was very unlucky and could not find any that could help me or i looked at the wrong area. Or there isn’t any (very unlikely)

This is the code for my viewmodel. Its a tool so when you equip it. A viewmodel would show

local RunService = game:GetService("RunService")
local UIS = game:GetService("UserInputService")

local Viewmodel = workspace.Viewmodel:Clone()

local player = game.Players.LocalPlayer
local Mouse = player:GetMouse()

local Tool = script.Parent.Parent
local Camera = workspace.Camera

local RemoteEvents = Tool.RemoteEvents
local FireEvent = RemoteEvents.Fire

local render
local viewmodel

local WorkPlr = script.Parent.Parent.Parent.Parent

local sounds = Tool.Sounds
local EquipSound = sounds.Equip
local ShootSound = sounds.Shot
local Dequip = sounds.Dequip
local Reload = sounds.Reload

local Flash = Viewmodel.HandleGun.Flash

local mult = 1
local Sway_Offset = CFrame.new()
local LastCameraCF = workspace.CurrentCamera.CFrame

local debounce = true

Tool.Equipped:Connect(function()
	EquipSound:Play()
	
	render = RunService.RenderStepped:Connect(function()
		Viewmodel.PrimaryPart.CFrame = Camera.CFrame
		
		local rotation = workspace.CurrentCamera.CFrame:ToObjectSpace(LastCameraCF)
		local x,y,z = rotation:ToOrientation()
		Sway_Offset = Sway_Offset:Lerp(CFrame.Angles(math.sin(x)*mult,math.sin(y)*mult,0), 0.1)
		Viewmodel.PrimaryPart.CFrame = Viewmodel.PrimaryPart.CFrame * Sway_Offset
		LastCameraCF = workspace.CurrentCamera.CFrame
		
		local PartVector = Viewmodel.HandleGun.Position
		local Direction = Vector3.new(0, -100, 0)
		
		local result = workspace:Raycast(PartVector, Direction)
		
		if result.Position.Y < 0.23 then
			Viewmodel.PrimaryPart.CFrame *= CFrame.new(0,0,result.Position.Y*-1*2)
		end
		
	end)
	
	Viewmodel.Parent = workspace.ViewmodelFolder
	Viewmodel.Name = "Viewmodel_" .. WorkPlr.Name

	-- This is the part where i tried to make my own lean/tilt mechanic but failed
	UIS.InputBegan:Connect(function(key,GPS)
		if key.KeyCode == Enum.KeyCode.E and not GPS then
			Camera.CFrame = Camera.CFrame:Lerp(CFrame.Angles(Camera.CFrame.X,Camera.CFrame.Y,math.rad(35)),1)
		end
	end)
	-- ends here
	
end)

Tool.Unequipped:Connect(function()
	Dequip:Play()
	viewmodel = workspace.ViewmodelFolder:FindFirstChild("Viewmodel_" .. WorkPlr.Name)
	viewmodel.Parent = game.ReplicatedStorage.Player_Viewmodels
	render:Disconnect()
end)

Tool.Activated:Connect(function()
	if debounce then
		debounce = false
		Camera.CFrame = LastCameraCF * CFrame.Angles(math.rad(3),0,0)
		local Damaga_Boolen = math.random(15,25)
		for _,flash in Flash:GetChildren() do
			flash.Enabled = true
			wait(0.03)
			flash.Enabled = false
		end
		FireEvent:FireServer(LastCameraCF, ShootSound, Mouse.Hit.Position, Tool.Barrel, Damaga_Boolen) 
		wait(0.3)
		debounce = true
	end
end)

I’m bad about CFrames but as I know you can’t make it with lerp. I recommend you try use this spring module for your problem: Physics Based Spring Module V2.0
It’s easy to use and no need to change camera type.

Here is example:

local SpringModule = Workspace.Spring
local NewSpring = SpringModule.new(20, 10, 200, 0, 0, 0.5)
local Camera = Workspace.CurrentCamera

if input.Keycode== Enum.KeyCode.E then
      Spring:Reset() 
end

RunService.RenderStepped:Connect(function()
    Camera.CFrame = CurrentCFrame * CFrame.Angles(0, 0, Spring.Offset)
end)
1 Like

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