Help with roll effect

I have a custom first person camera script

I’m trying to get the camera to roll with the players head when I’m rolling instead of just bobbing down then up on the y-axis, which I did achieve but its buggy.

Can anyone help me make it go back to the center smoothly and not just have it snap back to the center instantly?

heres the code of how I achieved the roll effect:

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

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local Humanoid = Player.Character:WaitForChild("Humanoid")
local Character = Player.Character
local Head = Character:WaitForChild("Head")

RunService.RenderStepped:Connect(function(deltaTime)
	if  Character:GetAttribute("Rolling") then 

		Camera.CFrame = Head.CFrame
	end
end)

video of buggy mess:

1 Like

i think the reason your camera clips like that is because assuming that the character does not have the attribute “rolling” anymore, the .Renderstepped does not check and stops forcing the camera’s cframe as head’s cframe which is what happens when the player rolls, and the camera goes back to where the player last looked before doing the roll

apologies if i explained this poorly

Hello there! I might be able to help you get smoother rolling, the issue you said to be was the camera snapping to the center of the screen when the player gets the “Rolling” attribute. You could try to pre-rotate the camera to match the player’s Head while they are falling so the camera is already moved to the center when the rolling happens, and it won’t look so horrible. I tried using a tween and here’s the code I used (I also should’ve mentioned, the script I made here is meant to be a different script that assists the original rolling mechanic, you could add this as a different new script, not in place of the original script, I’m terribly sorry for the confusion)

local player = game.Players.LocalPlayer
local character = player.Character
local humanoid = character.Humanoid

local TweenService = game:GetService("TweenService")
local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")

local Camera = workspace.CurrentCamera



local tweenInfo = TweenInfo.new(1)



local function MoveCam()
local Head = character.Head

    local tween = TweenService:Create(Camera, tweenInfo, {CFrame = Head.CFrame})
	if humanoid:GetState() == Enum.HumanoidStateType.Freefall and player.CameraMode == Enum.CameraMode.LockFirstPerson then

		tween:Play()
	end	

    if humanoid:GetState() ~= Enum.HumanoidStateType.Freefall and player.CameraMode == Enum.CameraMode.LockFirstPerson then
	
		tween:Cancel()

	end
end

while true do
	wait(0.001)
	MoveCam()
end

Though I really dislike one thing about this script and that is how normally when I tested it, the camera locks in place for 1 second after the player stopped FreeFalling, I do think that probably you won’t have the same issue because of your rolling mechanic and extra camera movements might last longer than the 1 second.

Otherwise, I think making the camera match the player’s head while they are Free Falling every single frame by using RenderStepped, but that would still snap the camera to the center, the rolling could look smoother though.

If you choose to keep your current script, you can use TweenService to smoothly reset your view to where you were, or you could get the last Camera CFrame from the Roll and apply it. Which is what I’ve done below.

Also you forgot to set it to Scriptable. So your Camera was just going back to where your camera was or is supposed to be.

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

local Player = Players.LocalPlayer
local Mouse = Player:GetMouse()
local Camera = workspace.CurrentCamera
local Humanoid = Player.Character:WaitForChild("Humanoid")
local Character = Player.Character
local Head = Character:WaitForChild("Head")

local LastCFrame = CFrame.new()
local wasRolling = false

RunService.RenderStepped:Connect(function(deltaTime)
	if Character:GetAttribute("Rolling") then 
		Camera.CFrame = Head.CFrame
        LastCFrame = Head.CFrame
        WasRolling = true
        Camera.CameraType = Enum.CameraType.Scriptable
    elseif WasRolling and not Character:GetAttribute("Rolling") then
        Camera.CFrame = LastCFrame
    	WasRolling = false
    	Camera.CameraType = Enum.CameraType.Custom
	end
end)