How do I add a secondary melee attack to a weapon using lua?
5 replies, posted
Hello,
I have managed to make a model of this weapon: [url]http://world.guns.ru/assault/type95-1.jpg[/url]
and it fires, reloads etc... I have also added a a melee animation. ( The Weapon stabs the enemy with a bayonet on the front. )
what would the lua code be, so that I can keep the firing animations & sound and make the "alt-fire" key melee attack.
Any help is appreciated :D
I think this would be it:
[lua]function SWEP:PrimaryAttack()
if self.Owner:KeyDown( KEY_LALT ) then
self.Weapon:SetNextPrimaryFire(CurTime() + .4)
local trace = self.Owner:GetEyeTrace()
if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 75 then
self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)
bullet = {}
bullet.Num = 1
bullet.Src = self.Owner:GetShootPos()
bullet.Dir = self.Owner:GetAimVector()
bullet.Spread = Vector(0, 0, 0)
bullet.Tracer = 0
bullet.Force = 1
bullet.Damage = 25
self.Owner:FireBullets(bullet)
self.Weapon:EmitSound("")
else
self.Weapon:EmitSound("" .. math.random( 1, 4 ) .. ".wav")
self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
elseif not self.Owner:KeyDown( Key_LALT ) then
// Your shooting code here
end
end
end [/lua]
Untested, but It should work, I think.
*Next Day*
Well, did it work?
If you pasted a code, thanks but I cant see it :C
[QUOTE=jedinight54;21188155]If you pasted a code, thanks but I cant see it :C[/QUOTE]
You are probably using Internet Explorer, which breaks the Lua tags. Switching to another internet browser, such as Firefox, will fix this. In the meantum, here is the code in code tags:
[code]function SWEP:PrimaryAttack()
if self.Owner:KeyDown( KEY_LALT ) then
self.Weapon:SetNextPrimaryFire(CurTime() + .4)
local trace = self.Owner:GetEyeTrace()
if trace.HitPos:Distance(self.Owner:GetShootPos()) <= 75 then
self.Weapon:SendWeaponAnim(ACT_VM_HITCENTER)
bullet = {}
bullet.Num = 1
bullet.Src = self.Owner:GetShootPos()
bullet.Dir = self.Owner:GetAimVector()
bullet.Spread = Vector(0, 0, 0)
bullet.Tracer = 0
bullet.Force = 1
bullet.Damage = 25
self.Owner:FireBullets(bullet)
self.Weapon:EmitSound("")
else
self.Weapon:EmitSound("" .. math.random( 1, 4 ) .. ".wav")
self.Weapon:SendWeaponAnim(ACT_VM_MISSCENTER)
elseif not self.Owner:KeyDown( Key_LALT ) then
// Your shooting code here
end
end
end [/code]
[QUOTE=jedinight54;21188155]If you pasted a code, thanks but I cant see it :C[/QUOTE]
Yea, like MakeR said, it breaks the lua tags, which sucks. But; you can simply hit "Reply", then you can see what I wrote, in the reply box :)
Nice One Guys :D
Sorry, you need to Log In to post a reply to this thread.