Changing the Way a Model is Viewed

Is there a code snippet that allows me to change how the v_model is displayed? I want, specifically, to make the ironsight coordinates the way the SWEP is viewed when drawn.

Thanks for your time, and I’ll be happy to answer any more questions if the above request isn’t very clear.

This might help:

No, what I want to do is make the ironsights what the player looks at when they draw the weapon. Specifically, I want to make an FN-SCAR that’s closer to the player like it is in L4D2.

[lua]function SWEP:GetViewModelPosition(pos, ang)
pos = pos + ang:Forward()*-64
return pos, ang

This will move the view model 64 units back.

Thanks. I’ll give this a try.

Well you could create a function that brings the ironsights in and do
function SWEP:Deploy()
//you should know this shtuff
Or something.

So you just want to shift the viewmodel position a bit on a SWEP?

Basically. I’m going to try out the code Null gave me, and see if the results are satisfactory.

You might be looking for SWEP.GetViewModelPosition. Lets you alter the angle and position of the view model.


You might be looking for SWEP.GetViewModelPosition. Lets you alter the angle and position of the view model.

Rated late.

Ok, the code you gave me doesn’t seem to do anything to the model. Are you sure it’s correct?

I’m not sure, try this instead:
[lua]function SWEP:GetViewModelPosition(pos, ang)
local pos = pos + ang:Forward()*-64
return pos, ang


If that doesn’t work, you may have overridden CalcView.

OK, whatever CalcView is, it must be overridden. How do I enable it?

That might not be it, though, can’t help to add some nice debug lines.
Try something like this, hold down space and see what happens:

function SWEP:GetViewModelPosition(pos, ang)
if LocalPlayer():KeyDown(IN_JUMP) then
pos = pos + ang:Forward()*-64
return pos, ang

I’ll give it a try. Thank you.

I’ve tried that… the model goes away, I can’t see it anymore when I press the jump button.
In less words: It works.

If the units of the view model are the same as the world units I’m not surprised - you’re essentially moving it 64 units behind where it normally is.

Just edit the FOV in the sharedl.lua

That doesn’t work. I’ve tried different values, and it doesn’t seem to do anything.

Why not using / reading a SWEP base that currently has iron sights (madcows or realistic cs, not sure) and extracting the code out of it?


To spare you some work, here is the exact code used for iron sights in realistic swep
base( ).

local IRONSIGHT_TIME = 0.35
function SWEP:GetViewModelPosition(pos, ang)

if not self.IronSightsPos then return pos, ang end

local bIron = self.Weapon:GetNetworkedBool("Ironsights")
if bIron ~= self.bLastIron then -- Are we toggling ironsights?

	self.bLastIron = bIron 
	self.fIronTime = CurTime()
	if bIron then 
		self.SwayScale 	= 0.3
		self.BobScale 	= 0.1
		self.SwayScale 	= 1.0
		self.BobScale 	= 1.0


local fIronTime = self.fIronTime or 0

if not bIron and (fIronTime < CurTime() - IRONSIGHT_TIME) then 
	return pos, ang 

local Mul = 1.0 -- we scale the model pos by this value so we can interpolate between ironsight/normal view

if fIronTime > CurTime() - IRONSIGHT_TIME then

	Mul = math.Clamp((CurTime() - fIronTime) / IRONSIGHT_TIME, 0, 1)
	if not bIron then Mul = 1 - Mul end


local Offset	= self.IronSightsPos

if self.IronSightsAng then

	ang = ang*1
	ang:RotateAroundAxis(ang:Right(), 		self.IronSightsAng.x * Mul)
	ang:RotateAroundAxis(ang:Up(), 			self.IronSightsAng.y * Mul)
	ang:RotateAroundAxis(ang:Forward(), 	self.IronSightsAng.z * Mul)


local Right 	= ang:Right()
local Up 		= ang:Up()
local Forward 	= ang:Forward()

pos = pos + Offset.x * Right * Mul
pos = pos + Offset.y * Forward * Mul
pos = pos + Offset.z * Up * Mul

return pos, ang

Hope you can get something out of it =)