Gradual zoom effect on SWEP

I’m trying to create the gradual zoom effect that you can find on the crossbow for a custom swep. This is the code I have so far that controls it;



zoomed = false
defZoom = 0

function SWEP:Deploy()
	defZoom = self.Owner:GetFOV()
end


function SWEP:Think()
	if zoomed then
		self.Owner:SetFOV(math.max(self.Owner:GetFOV() - 2, 20), 0)
	else
		self.Owner:SetFOV(math.min(self.Owner:GetFOV() + 2, defZoom), 0)
	end
end

function SWEP:SecondaryAttack()
	zoomed = not zoomed
	return false
end


However, the zooming in and out is really buggy and dysfunctional. Is there a better way of doing it?

How does this work? It seems to be a function hook, not a call. How would I use it for my means?

[editline]14th March 2015[/editline]

I now have this code in my weapon script;



zoomed = false

function SWEP:TranslateFOV(current)
	if zoomed then
		return 20
	else
		return current
	end
end

function SWEP:SecondaryAttack()
	zoomed = not zoomed
	return false
end


Not only does it not work half the time, but when it does it’s instantaneous instead of gradual.

  1. Use self.zoomed instead of zoomed. There’s also no need to initialize it, it defaults to nil which is in 99% of cases the same as false.
  2. SecondaryAttack is predicted, use IsFirstTimePredicted for changing variables based on themselves
  3. You are the one responsible for making the zoom gradual

That’s not very helpful, considering I made the post specifically because I need help making the zoom gradual.

Use

Global.Lerp or

math.Approach, make sure to use

Global.RealTime or

Global.FrameTime in some way so as to not make it framerate-dependent.

Sorry for the dry replies, I’ve seen some of your posts and I think you’re smart enough to figure the code/logic stuff out on your own :slight_smile:

If you look at my initial post, I put code that uses SWEP:Think, and is supposed to gradually add to the weapon’s zoom value. But it just ends up looking glitchy and not working properly.

Think was your problem. Have you tried the same approach in TranslateFOV?

I just realized what the second argument for SetFOV is for.

This is the code I have now;



function SWEP:SecondaryAttack()
	if IsFirstTimePredicted() then
		self.zoomed = not self.zoomed
		if self.zoomed then
			self.Owner:SetFOV(20, 1)
		else
			self.Owner:SetFOV(80, 1)
		end
	end
	return false
end


However, the player’s view trembles while unzoomed, and while zooming in is somewhat gradual, zooming back out is instantaneous.

Stop using SetFOV! Use the TranslateFOV hook like you did a few posts ago and just make it gradual using math!

Man, SetFOV’s second argument is literally the transition time.

He shouldn’t use SetFOV in a SWEP, especially since there’s a hook designed specifically for that reason.

Well, there’s no argument for transition time in the TranslateFOV hook. So I don’t see how I could use it.

[lua]
function SWEP:TranslateFOV(old)
if self.zoomed then
self.currentfov = math.Approach(self.currentfov, 20, FrameTime()*0.5) – tweak the 0.5
return self.currentfov
else
self.currentfov = math.Approach(self.currentfov, old, FrameTime()*0.5) – tweak the 0.5
return self.currentfov
end
end
[/lua]

untested but this is basically how you should do it.

Thanks, it works pretty well. This is the code I ended up using;



function SWEP:TranslateFOV(old)
	self.currentfov = self.currentfov or 80
	if self.zoomed then
		self.currentfov = math.Approach(self.currentfov, 20, 0.5)
	else
		self.currentfov = math.Approach(self.currentfov, old, 0.2)
	end
	return self.currentfov
end