Hello all.
When zoomed in, moving and changing direction of view using mouse will result in the previous direction continuing before it changes. It is somewhat like a lag. For example if while moving the mouse right and i suddenly move up, the character continues to turn his view right a while before moving up.
I am not sure exactly what is wrong. It breaks the whole experience. Do kindly assist.
local Players = game:GetService("Players")
local ContextActionService = game:GetService("ContextActionService")
local UserInputService = game:GetService("UserInputService")
local TweenService = game:GetService("TweenService")
local camera = workspace.CurrentCamera
local player = Players.LocalPlayer
local tweenInfo = TweenInfo.new(0.5, Enum.EasingStyle.Quint, Enum.EasingDirection.Out)
local initialFieldOfView = camera.FieldOfView
local initialCameraPosition = camera.CFrame
local isZoomed = false
local inTween = false
local target
local humanoid = character:WaitForChild("Humanoid")
local rootPart = character:WaitForChild("HumanoidRootPart")
local initialWalkSpeed = humanoid.WalkSpeed
local initialJumpPower = humanoid.JumpPower
local cameraAngleX = 0
local cameraAngleY = 0
local function moveInput(actionName, inputState, inputObject)
if not isZoomed then return end
if inputState == Enum.UserInputState.Change then
cameraAngleX = math.clamp(cameraAngleX-inputObject.Delta.X*0.4, -90, 90)
cameraAngleY = math.clamp(cameraAngleY-inputObject.Delta.Y*0.4, -75, 75)
local camPosition = rootPart.CFrame:ToWorldSpace(CFrame.new(0, 2, -2))
local camRotation = camPosition * CFrame.Angles(0, math.rad(cameraAngleX), 0) * CFrame.Angles(math.rad(cameraAngleY), 0, 0)
target = camRotation:ToWorldSpace(CFrame.new(0, 0, -20))
camera.CFrame = CFrame.new(camRotation.Position, target.Position)
camera.Focus = CFrame.new(target.Position)
end
end
local function scopeInOut(actionName, inputState, inputObject)
if inTween == true then return end
if (inputObject.UserInputType == Enum.UserInputType.MouseButton3 and (inputState == Enum.UserInputState.Begin or inputState == Enum.UserInputState.End))
or (inputObject.UserInputType == Enum.UserInputType.Touch and inputState == Enum.UserInputState.End) then
inTween = true
-- Zoom in
if isZoomed == false then
initialCameraPosition = camera.CFrame
camera.CameraType = Enum.CameraType.Scriptable
UserInputService.MouseBehavior = Enum.MouseBehavior.LockCenter
UserInputService.MouseIconEnabled = false
UserInputService.MouseDeltaSensitivity = 0.1
-- Rotate root part CFrame according to camera direction
local faceToward = camera.CFrame:Lerp(rootPart.CFrame, 1.1)
rootPart.CFrame = CFrame.new(rootPart.CFrame.Position, Vector3.new(faceToward.Position.X, rootPart.Position.Y, faceToward.Position.Z))
target = rootPart.CFrame:ToWorldSpace(CFrame.new(0, 2, -20))
cameraAngleX, cameraAngleY = 0, 0
humanoid.WalkSpeed = 0
humanoid.JumpPower = 0
wait(0.05)
local camPosition = rootPart.CFrame:ToWorldSpace(CFrame.new(0, 2, -2))
camera.CFrame = CFrame.new(camera.CFrame.Position, target.Position)
local tween = TweenService:Create(camera, tweenInfo, {CFrame=camPosition, FieldOfView=50})
tween.Completed:Connect(function()
ContextActionService:BindAction("MoveInput", moveInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
ContextActionService:SetTitle("ScopeInOut", "–")
camera.Focus = CFrame.new(target.Position)
isZoomed = true
inTween = false
end)
tween:Play()
-- Zoom out
elseif isZoomed == true then
ContextActionService:UnbindAction("MoveInput", moveInput, false, Enum.UserInputType.MouseMovement, Enum.UserInputType.Touch)
UserInputService.MouseBehavior = Enum.MouseBehavior.Default
UserInputService.MouseIconEnabled = true
local tween = TweenService:Create(camera, tweenInfo, {CFrame=initialCameraPosition, FieldOfView=initialFieldOfView})
tween.Completed:Connect(function()
ContextActionService:SetTitle("ScopeInOut", "+")
camera.CameraType = Enum.CameraType.Custom
humanoid.WalkSpeed = initialWalkSpeed
humanoid.JumpPower = initialJumpPower
isZoomed = false
inTween = false
end)
tween:Play()
end
end
end
ContextActionService:BindAction("ScopeInOut", scopeInOut, true, Enum.UserInputType.MouseButton3)
ContextActionService:SetPosition("ScopeInOut", UDim2.new(0.65, 0, 0.1, 0))
ContextActionService:SetTitle("ScopeInOut", "+")