Hi
I’m trying to make a first-person shooter, and I am using a script to make the arms face the camera direction when a weapon is equipped. The problem is that in first-person the arms look too close or too far away from the camera when looking up or down.
Video:
Script Responsible For “Arms To Cam”:
- Inside Each Weapon (Local Script)
-- Raise Hand --
run.RenderStepped:Connect(function()
if equipped then
local char = plr.Character or plr.CharacterAdded:Wait()
local params = RaycastParams.new()
params.FilterType = Enum.RaycastFilterType.Blacklist
params.FilterDescendantsInstances = {char, workspace:WaitForChild("FX")}
params.IgnoreWater = true
local VIEWPORT_SIZE = cam.ViewportSize -- current size of the screen -- if you're raycasting multiple times, this will have to be redefined!
local CAST_LENGTH = 100 -- distance to raycast
local unitRay = cam:ScreenPointToRay(VIEWPORT_SIZE.X / 2, VIEWPORT_SIZE.Y / 2 - game:GetService("GuiService"):GetGuiInset().Y) -- account for the 36px TopBar inset
local raycastResult = workspace:Raycast(unitRay.Origin, unitRay.Direction * CAST_LENGTH, params)
local pos
local posTwo = cam.CFrame.Position
if raycastResult and raycastResult.Position then -- .Position is the Vector3 (the Hit.Position) of the center of the screen
pos = raycastResult.Position
else
pos = unitRay.Origin + unitRay.Direction * CAST_LENGTH
end
local rightX, rightY, rightZ = char.Torso["Right Shoulder"].C0:ToEulerAnglesYXZ()
char.Torso["Right Shoulder"].C0 = (char.Torso["Right Shoulder"].C0 * CFrame.Angles(0, 0, -rightZ)) * CFrame.Angles(0, 0, math.asin((pos - posTwo).unit.y))
if reloading then
local leftX, leftY, leftZ = char.Torso["Left Shoulder"].C0:ToEulerAnglesYXZ()
char.Torso["Left Shoulder"].C0 = (char.Torso["Left Shoulder"].C0 * CFrame.Angles(0, 0, -leftZ)) * CFrame.Angles(0, 0, math.asin((-pos - -posTwo).unit.y))
end
end
end)
Can someone help me fix it?