Viewmodel zoom is different

Chello!
I encounter some problems with my viewmodel script. Currently, the client do the calculation of arms and head based on mouse and camera. Then, the server will do the Sanity check.

However, the viewmodel position/zoom is kinda different. If you look down, the zoom is lower. If you look up, the zoom is higher.

Here the script :

local runService = game:GetService("RunService")

local player = game.Players.LocalPlayer
local mouse = player:GetMouse()
local character = player.Character or player.CharacterAdded:Wait()

local camera = game.Workspace.CurrentCamera

local head = character:WaitForChild("Head")
local torso = character:WaitForChild("Torso")
local humanoidRootPart = character:WaitForChild("HumanoidRootPart")
local humanoid = character:WaitForChild("Humanoid")

local rightArm = character:WaitForChild("Right Arm")
local leftArm = character:WaitForChild("Left Arm")

local neck = torso:WaitForChild("Neck")
local rightShoulder = torso:WaitForChild("Right Shoulder")
local leftShoulder = torso:WaitForChild("Left Shoulder")

local orientationEvent = game.ReplicatedStorage:WaitForChild("CharacterEvents"):WaitForChild("Orientation")
local headRotationEvent = game.ReplicatedStorage:WaitForChild("CharacterEvents"):WaitForChild("HeadRotation")

local calculateRS = nil
local calculateLS = nil
local camDirection = nil
local camDistance
local actualRotation = nil

local function onMouseMoved()
	if character:FindFirstChildWhichIsA("Tool") then
		calculateRS = CFrame.new(1, 0.5, 0) * CFrame.Angles(math.asin((mouse.Hit.Position - mouse.Origin.Position).unit.y), 1.55, 0)
		calculateLS = CFrame.new(-1, 0.5, 0) * CFrame.Angles(math.asin((mouse.Hit.Position - mouse.Origin.Position).unit.y), -1.55, 0)

		orientationEvent:FireServer(calculateRS, calculateLS)
	end
end

local function aRemovedInstace(child)
	if child:IsA("Tool") then
		calculateRS = CFrame.new(0, 0, 0) * CFrame.Angles(0, 0, 0)
		calculateLS = CFrame.new(0, 0, 0) * CFrame.Angles(0, 0, 0)

		orientationEvent:FireServer(calculateRS, calculateLS)
	end
end

local function rotateHeadAbsolute(deltaTime)
	if humanoid.Health ~= 0 and character ~= nil then
		camDirection = humanoidRootPart.CFrame:ToObjectSpace(camera.CFrame).lookVector
		
		actualRotation = CFrame.new(0, 1, 0) * CFrame.Angles(-math.asin(-camDirection.Y)+5, 0, math.asin(-camDirection.X)+84.75)
		
		headRotationEvent:FireServer(actualRotation)
	end
end

local function calculateDistance(deltaTime)
	if humanoid.Health ~= 0 and character ~= nil and character:FindFirstChildWhichIsA("Tool") then
		camDistance = (camera.CFrame.Position - head.Position).Magnitude
		
		if camDistance < 1.2 then
			rightArm.LocalTransparencyModifier = 0
			leftArm.LocalTransparencyModifier = 0
		end
	end
end

mouse.Move:Connect(onMouseMoved)

character.ChildRemoved:Connect(aRemovedInstace)

runService.RenderStepped:Connect(rotateHeadAbsolute)
runService.RenderStepped:Connect(calculateDistance)

Its because the viewmodel is rotating lower than the camera, with the neck.

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