Camera rotating fast and uncontrollable

when i press RMB to aim(while in third person) it works just fine, but when I go in first person and try it, the camera starts rotating uncontrollably and the CFrame isn’t even set properly
code:

local uis = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
local mosin = plr.Character["Right Arm"].MosinNagant

local camcfr = cam.CFrame
print(mosin)
local aimcam = mosin.aimcam
print(aimcam)

local isaiming = false

print(cam.CFrame)

uis.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        print('aiming')
        isaiming = true
        if isaiming then
            while task.wait() do
                cam.CameraType = Enum.CameraType.Scriptable
                cam.CFrame = aimcam.CFrame
            end
        end
    end
end)```

As i see the issue with your camera rotation in first person mode is due to the way the camera’s CFrame is being set in a loop, which can cause it to behave unpredictably :grin:. I’ll try to tweak your code a bit:

  1. Avoid Infinite Loops!: Instead of using a while loop inside the InputBegan event, you can use RenderStepped to update the camera’s CFrame smoothly.

  2. Properly Handle Camera Rotation: Ensure that the camera’s rotation is handled correctly by using CFrame methods that account for the player’s orientation.


Here’s an updated version of your script:

local uis = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
local mosin = plr.Character["Right Arm"].MosinNagant

local aimcam = mosin.aimcam
local isaiming = false

uis.InputBegan:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        isaiming = true
        cam.CameraType = Enum.CameraType.Scriptable
    end
end)

uis.InputEnded:Connect(function(input)
    if input.UserInputType == Enum.UserInputType.MouseButton2 then
        isaiming = false
        cam.CameraType = Enum.CameraType.Custom
    end
end)

game:GetService("RunService").RenderStepped:Connect(function()
    if isaiming then
        cam.CFrame = aimcam.CFrame
    end
end)

I will be waiting for your feedback! I hope I’ve fixed all the bugs.

1 Like

so now the camera goes back to normal, but when aiming it still rotates and the Camera is offset.
i didn’t even consider using runserv since its easier to just use task.wait but I can also see instead of placing it to change the frame in the inputbegan you put it in the runserv function, and idk why that works but thx

Can you send me your video because it works for me

sure

The camera might be rotating because it’s not locked to the character’s orientation. We can adjust the camera’s CFrame to align with the character’s HumanoidRootPart.

Here’s an updated version of the script:

local uis = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer -- I dont know what is plr for you but ill keep this line of code just in case
local character = plr.Character or plr.CharacterAdded:Wait()
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local mosin = character:WaitForChild("Right Arm"):WaitForChild("test")

local aimcam = mosin:WaitForChild("aimcam")
local isaiming = false

uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isaiming = true
		cam.CameraType = Enum.CameraType.Scriptable
	end
end)

uis.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isaiming = false
		cam.CameraType = Enum.CameraType.Custom
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if isaiming then
		local aimCFrame = humanoidRootPart.CFrame * aimcam.CFrame
		cam.CFrame = aimCFrame
	end
end)

Have you tried locking your mouse? and set the camera’s CFrame based on aimcam but edit it to player’s head orientation:

local uis = game:GetService("UserInputService")
local cam = workspace.CurrentCamera
local plr = game.Players.LocalPlayer
local mosin = plr.Character["Right Arm"].MosinNagant

local aimcam = mosin.aimcam
local isaiming = false

uis.InputBegan:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isaiming = true
		uis.MouseBehavior = Enum.MouseBehavior.LockCenter
		cam.CameraType = Enum.CameraType.Scriptable
	end
end)

uis.InputEnded:Connect(function(input)
	if input.UserInputType == Enum.UserInputType.MouseButton2 then
		isaiming = false
		uis.MouseBehavior = Enum.MouseBehavior.Default
		cam.CameraType = Enum.CameraType.Custom
	end
end)

game:GetService("RunService").RenderStepped:Connect(function()
	if isaiming then
		cam.CFrame = aimcam.CFrame * plr.Character.Head.CFrame:toObjectSpace(plr.Character.Head.CFrame)
	end
end)

Yes, I’ve already fixed it in my code and sent it to him. It is also better to use HumanoidRootPart for more flexibility.

You aim with your head not your HumanoidRootPart.

somehow its even worse, now not only the camera is rotating, but its also very high up in the air

Yes, however, the humanoidrootpart is the primary component in the player model and one never knows what may occur to other components of the body. Consequently, it may be more prudent to add an additional 5 units to the Y-axis CFrame, as this would help to avoid potential issues.

i should just let yall know that it works perfectly when I aim while I’m on third person

Try my script and let me know if you found any issues.

the camera is still rotating uncontrollably

That’s it, I found a bug and was able to repeat it at my studio, now I’ll try to fix it

It doesn’t matter to who it was parented ;/

In short, when you set the first-person view and change the camera position, it overlaps and as a result, you get an endless overlay of the camera position on yourself. Have you installed a 1-person view somewhere?

idk what you mean by installed a 1person view but I did make a ViewModel using the head and torso

He means if you have a first-person script that collides with your current script.
if you do send it here

so each script that ive done are the one where you aim the gun, one that makes the body visible in firstperson and the one for the ViewModel(it just makes the arms rotate to where you’re facing)