How do I change a viewmodel's position in a SWEP?

Hello. Just so you know, the viewmodel I’m using is models/weapons/c_shotgun.mdl, and the FOV is 54. Should their be any extra info you need, let me know in the comments.

NOTE: I just found out about GM:CalcViewModelView. However, I’m confused as to how to use it; just so you know the weapon file’s name is shotgun.lua.

Ex


if ( CLIENT ) then
   function SWEP:CalcViewModelView( ent, oldPos, oldAng, pos, ang )
	pos = pos + ang:Right() * 7 + ang:Forward() * 17 + ang:Up() * -4.5
	
	ang:RotateAroundAxis( ang:Up(), 170 )
	ang:RotateAroundAxis( ang:Right(), -70 )
	
	ent:SetPos( pos )
	ent:SetAngles( ang )
   end
end

Any position or angle value you modify will change the viewmodel’s position and angle.

That code confuses me a bit, well, the position I want to set the viewmodel to is Vector(-2.161, 0, 0.519)

Then do pos = Vector( -2.161, 0, 0.519 ) inside that function.

That doesn’t work… Here:


if ( CLIENT ) then
   function SWEP:CalcViewModelView( ent, oldPos, oldAng, pos, ang )
	pos = Vector(-2.161, 0, 0.519)
	
	ang:RotateAroundAxis( ang:Up(), 170 )
	ang:RotateAroundAxis( ang:Right(), -70 )
	
	ent:SetPos( pos )
	ent:SetAngles( ang )
   end
end


Should I get rid of if CLIENT then, or should I maybe call this somewhere else, like in SWEP:Initialize or SWEP:Think?

[editline]5th May 2015[/editline]

Okay, I read up on the Gmod wiki that you need to specify the name of the weapon you’re changing the viewmodel position of, so I did this:


if ( CLIENT ) then
   function SWEP:CalcViewModelView( shotgun, ent, oldPos, oldAng, pos, ang )
	pos = pos + ang:Right() * 7 + ang:Forward() * 17 + ang:Up() * -4.5
	
	ang:RotateAroundAxis( ang:Up(), 170 )
	ang:RotateAroundAxis( ang:Right(), -70 )
	
	ent:SetPos( pos )
	ent:SetAngles( ang )
   end
end

But, I get this error:


[ERROR] lua/weapons/shotgun.lua:70: attempt to index local 'ang' (a nil value)
  1. func - lua/weapons/shotgun.lua:70
   2. unknown - gamemodes/base/gamemode/cl_init.lua:562


I didn’t mean include the other functions, they were an example. Use this:


if ( CLIENT ) then
   function SWEP:CalcViewModelView( ent, oldPos, oldAng, pos, ang )
	pos = Vector(-2.161, 0, 0.519)
   end
end

This will only work if the gamemode you’re using hasn’t overrided CalcViewModelView.

I found out that if you want to change the viewmodel position from the SWEP code, you’ll need to use GetViewModelPosition(or something like that), not GM:CalcViewModelView. I’ll still try this though.