Hello, I have currently designed a very basic model to use on a gun.
It works flawlessly apart from when you spectate someone holding it, here is a screenshot;
[thumb]http://i.imgur.com/R7OWAPJ.png[/thumb]
I downloaded the P.E.B SWEP from the workshop and used that as my base code, here is the code I currently use;
[lua]
SWEP.ViewModel = "models/weapons/c_rpg.mdl"
SWEP.WorldModel = "models/weapons/w_357.mdl"
SWEP.Base = "weapon_tttbase"
SWEP.Primary.Sound = Sound("buttons/combine_button3.wav")
SWEP.Primary.Recoil = 0
SWEP.Primary.Damage = 5
SWEP.Primary.Force = 100
SWEP.Primary.NumShots = 1
SWEP.Primary.Delay = 0.2
SWEP.Primary.Ammo = "357"
SWEP.Primary.ClipSize = 9999999
SWEP.Primary.DefaultClip = 999999
SWEP.Primary.Automatic = true
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
SWEP.UseHands = true
SWEP.IronSightsPos = Vector(-5.613, -3.77, 2.559)
SWEP.IronSightsAng = Vector(0, 0, 0)
SWEP.RestoreSound = Sound( "buttons/combine_button3.wav" )
SWEP.LastPrimaryAttack = 0
function SWEP:Initialize()
self:SetWeaponHoldType(self.HoldType)
self.Weapon:SetNetworkedBool( "Ironsights", false )
if CLIENT then
// Create a new table for every weapon instance
self.VElements = table.FullCopy( self.VElements )
self.WElements = table.FullCopy( self.WElements )
self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods )
self:CreateModels(self.VElements) // create viewmodels
self:CreateModels(self.WElements) // create worldmodels
// init view model bone build function
if (IsValid(self.Owner)) then
local vm = self.Owner:GetViewModel()
if IsValid(vm) then
self:ResetBonePositions(vm)
// Init viewmodel visibility
if (self.ShowViewModel == nil or self.ShowViewModel) then
vm:SetColor(Color(255,255,255,255))
else
// we set the alpha to 1 instead of 0 because else ViewModelDrawn stops being called
vm:SetColor(Color(255,255,255,1))
// ^ stopped working in GMod 13 because you have to do Entity:SetRenderMode(1) for translucency to kick in
// however for some reason the view model resets to render mode 0 every frame so we just apply a debug material to prevent it from drawing
vm:SetMaterial("Debug/hsv")
end
end
end
self.MoveDis = 0.4
//self.VElements["switch"].orig_x = self.VElements["switch"].pos.x
//self.VElements["switchr"].orig_x = self.VElements["switchr"].pos.x
end
end
function SWEP:SetupDataTables()
self:DTVar( "Entity", 0, "laser" )
end
function SWEP:Think()
if (CLIENT and IsValid(self.Owner)) then
if (self.LastPrimaryAttack < CurTime() - 1 ) then
//local x1 = self.VElements["switch"].pos.x
//local x2 = self.VElements["switchr"].pos.x
//local t1 = self.VElements["switch"].orig_x
//local t2 = self.VElements["switchr"].orig_x
//self.VElements["switch"].pos.x = math.Approach(x1, t1, FrameTime()*self.MoveDis)
//self.VElements["switchr"].pos.x = math.Approach(x2, t2, FrameTime()*self.MoveDis)
end
local laser = self:GetLaser()
if (IsValid(laser) and laser.CorrectBeamStart) then
laser:CorrectBeamStart( self.LastBeamStart )
end
end
end
function SWEP:PrimaryAttack()
self.Weapon:SetNextPrimaryFire(CurTime() + self.Primary.Delay)
if not self:CanPrimaryAttack() then return end
self:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
self.Owner:SetAnimation(PLAYER_ATTACK1)
self:EmitSound( self.Primary.Sound )
self:TakePrimaryAmmo( 1 )
self:Recoil()
//self.LastPrimaryAttack = CurTime()
self:ReloadCheck()
if (SERVER) then
local laser = ents.Create( "rc_laser" )
if (IsValid(laser)) then
laser:SetPos(self.Owner:GetShootPos())
laser:Spawn()
laser:SetOwner(self.Owner)
laser:SetParent(self.Owner)
laser:SetWeapon( self )
laser:SetLaserProperties( "sprites/laserbeam", self.Primary.Damage, 8, Color(0,255,0) )
self.dt.laser = laser
end
self.Owner:SendLua( "LocalPlayer():GetActiveWeapon():ClientPrimary()" )
end
end
function SWEP:ClientPrimary()
self.LastPrimaryAttack = CurTime()
/*local vm = self.Owner:GetViewModel()
if (IsValid(vm)) then
local attachment = vm:GetAttachment( 1 )
self.LastBeamStart = attachment.Pos
end*/ // somehow totally fucked, while it worked in multiplayer
local angles = self.Owner:EyeAngles()
local pos = self.Owner:GetShootPos()
if (self:GetNWBool("Ironsights", false)) then
pos = pos + angles:Forward()*15
else
pos = pos + angles:Right()*3
pos = pos + angles:Up()*-1.8
pos = pos + angles:Forward()*15
end
self.LastBeamStart = pos
//self.VElements["switch"].pos.x = self.VElements["switch"].orig_x + self.MoveDis
//self.VElements["switchr"].pos.x = self.VElements["switchr"].orig_x - self.MoveDis
timer.Simple( 1, function()
if (IsValid(self)) then self:EmitSound( self.RestoreSound ) end
end)
// make the bullets disappear
timer.Simple(0.1, function()
if (!IsValid(self)) then return end
local pred = self:Clip1()
if (pred == 0) then
//self.VElements["bullet1"].hide = true
//self.VElements["glow"].hide = true
else
for i = 1, (6 - pred) do
//self.VElements["bullet1"..string.rep("+",i)].hide = true
//self.VElements["glow"..string.rep("+",i)].hide = true
end
end
end)
end
function SWEP:Recoil()
//local recoil = self.Primary.Recoil
//local recoilAng = Angle(math.Rand(-0.25,-0.2) * recoil, math.Rand(-0.1,0.1) * recoil, math.Rand(-0.07, 0.07) * recoil)
//self.Owner:ViewPunch(recoilAng)
end
function SWEP:Reload()
//self.Weapon:DefaultReload( ACT_VM_RELOAD )
self.Weapon:DefaultReload( ACT_VM_IDLE )
self:SetIronsights( false )
self:OnReloadStart()
if (!IsValid(self.Owner)) then return end
if (SERVER) then
self.Owner:SendLua( "Entity("..self:EntIndex().."):OnReloadStart()" )
end
local animTime = self.Owner:GetViewModel():SequenceDuration()
timer.Simple( animTime, function()
if (!IsValid(self)) then return end
self:SendWeaponAnim( ACT_VM_IDLE )
end)
end
function SWEP:OnReloadStart()
if CLIENT then
timer.Simple( 2, function()
if !IsValid(self) then return end
//self.VElements["bullet1"].hide = false
//self.VElements["glow"].hide = false
for i = 1, math.min(5, self.Owner:GetAmmoCount(self:GetPrimaryAmmoType())-1) do
//self.VElements["bullet1"..string.rep("+",5-(i-1))].hide = false
//self.VElements["glow"..string.rep("+",5-(i-1))].hide = false
end
end)
end
end
function SWEP:GetLaser()
local laser = self.dt.laser
if (!IsValid(laser)) then return nil
else return laser end
end
function SWEP:ReloadCheck()
timer.Destroy("reloadcheck"..self:EntIndex())
timer.Create("reloadcheck"..self:EntIndex(),0.5,1,function()
if (IsValid(self) and IsValid(self.Owner) and self:Clip1() == 0) then
self:SetIronsight
Sorry, you need to Log In to post a reply to this thread.