Camera freaks out when i try to aim my gun

My camera freaks out when i want to aim??

Epilepsy warning
btw it wont let me format the text

local RunService = game:GetService(“RunService”)
local Players = game:GetService(“Players”)

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local aimPart = tool:WaitForChild(“AimPart”)
local aimOffset = Vector3.new(0, 0.2, -0.5)
local aimLerpSpeed = 0.15
RunService.RenderStepped:Connect(function()
if aiming and player.Character and aimPart then
local targetPos = aimPart.Position + aimOffset
camera.CFrame = CFrame.new(targetPos, targetPos + camera.CFrame.LookVector)
end
end)

Edit 1: Its not going to the aim part either
If you can send code that fixes the issue it will be helpful :smiley:

Edit 2: Also i need to know how viewmodels work LOL if anyone could teach me that it would be cool

1 Like

Hello!

Firstly to format text, just use three hyphons or whatever they may be called as follows: ```

opening and closing text with this produces this effect:

local RunService = game:GetService(“RunService”)
local Players = game:GetService(“Players”)

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local aimPart = tool:WaitForChild(“AimPart”)
local aimOffset = Vector3.new(0, 0.2, -0.5)
local aimLerpSpeed = 0.15

RunService.RenderStepped:Connect(function()

  if aiming and player.Character and aimPart then
  
    local targetPos = aimPart.Position + aimOffset
    camera.CFrame = CFrame.new(targetPos, targetPos + camera.CFrame.LookVector)
  
  end
end)

CFrames are always tricky and I think what is happening here is that the aim offset is supposed to be relative to the aim part but is actually relative to world. I believe this can be fixed by saying:

aimOffset = Vector3.new(0,0.2,-0.5):ToObjectSpace(aimPart.CFrame)

This however also means that aim offset has to be defined every frame to assure that when the player moves the offset changes to continue to be relative to aimparts new position and orientation.

If you are extremely worried about performance then you can also choose to check when the player is moving their camera so you aren’t constantly doing that vector math but I don’t recommend that entirely to be honest.

Hopefully this helps! Let me know if it works or not :smile:

Oh yeah also you asked for a code example and this one should be easy to provide so Ill just do it here:

local RunService = game:GetService(“RunService”)
local Players = game:GetService(“Players”)

local player = Players.LocalPlayer
local camera = workspace.CurrentCamera
local aimPart = tool:WaitForChild(“AimPart”)
local aimOffset = Vector3.new(0, 0.2, -0.5)
local aimLerpSpeed = 0.15

RunService.RenderStepped:Connect(function()

  if aiming and player.Character and aimPart then
  
    aimOffset = Vector3.new(0,0.2,-0.5):ToObjectSpace(aimPart.CFrame)

    local targetPos = aimPart.Position + aimOffset
    camera.CFrame = CFrame.new(targetPos, targetPos + camera.CFrame.LookVector)
  
  end
end)

Thank you!!! but the camera only does it when im in first person
which i dont know how to fix because if i were to aim in third person it wouldnt let me move my camera

Hello,

Is it still not working? I might have to take a closer look if that is the case.

Edit:

Sorry, I just realized there are many more issues than just how you have the vector math setup here. The primary issue is that you are changing the Camera CFrame which is causing the crazy effect you see when you aim in. To get a gun to aim, you’ll want to avoid changing the camera itself too much and just figure out how to get the gun into the proper position for aiming first.

The reason you are having the issue you see here is because you are setting the camera CFrame to a part attached to a gun attached to the player. In 3rd person, you can’t move because you are setting the CFrame to a set spatial value.

First person however, will move the camera to the spatial value you set, which turns the player, which turns the gun, which turns the camera, and you get this loop of insanity.

Basically you will have to do more research, and figure out how to move the gun (not the camera) and maybe change the camera FOV to give a zoom in effect.

That is all just off the top of my head, sorry I could not help more, I am currently really busy with school so I can’t just write something up for you. Goodluck and best wishes going forward!