Zooming in camera system

I am attempting to create a camera zooming system using the coroutine.wrap() system but I have ran into a problem. Whenever the zoom function is activated it will zoom correctly, and will zoom out correctly, though if you are halfway between the zoom and unzoom or spam then your camera will look clumsy.

Example gif:
https://gyazo.com/ec86859c98dd6ad12227151fdd2b22fe

Code:

function zoom()
	for fov = camera.FieldOfView, 50, -1 do
		camera.FieldOfView = fov
		wait()
	end
end


function unzoom()
	for fov = camera.FieldOfView, 70, 1 do
		camera.FieldOfView = fov
		wait()
	end
end

Mouse.Button2Down:Connect(function(mouse)
   print("zoom")
   local zoom = coroutine.wrap(zoom)
   zoom()
end)

Mouse.Button2Up:Connect(function(mouse)
   print("unzoom")
   local unzoom = coroutine.wrap(unzoom)
   unzoom()
end)

I am not sure how to fix this apart from timing both so people cannot spam it, though I would like it if people were able to zoom in and out quickly without delay. Could anyone help me with this?

Have you considered using TweenService to change the FOV? It would let you change it smoothly. You can also pause the “movement” with a single function in case the player starts zooming out before zooming in is complete.

1 Like

add a debounce


I will give this a good try, sounds very promising.