Can anyone give me ideas as to how I’d create a melee weapon for a SWEP? I checked weapon_fists.lua and still I was confused as to how to make one. I’m good at making SWEPs that shoot bullets and whatnot, but I need help with a melee weapon.
The main difference is:
You will have local sound effects via ent:EmitSound()
self.Owner():LagCompensation() is useful for making for your weapon hits. [Remember to turn it off after done using it!]
util.TraceHull() / util.TraceLine() is useful for getting the target (entity) of your melee weapon.
Note: Make sure to use mask = MASK_SHOT_HULL and that endpos = self.Owner():GetShootPos() + self.Owner():GetAimVector() * [distance of melee attack]
When you have a target entity check if it is an entity via an if statement:
if ( IsValid( ent ) && ent:IsPlayer() && .... ) then ent:SetHealth( ... ) -- edit this to what you want the melee weapon to do ent:EmitSound( ... ) --- insert some animations here with SetAnimation() and SendWeaponAnim() self:SetNextPrimaryFire( CurTime() + delay until next attack ) end
Check out this for a reference: https://github.com/garrynewman/garrysmod/blob/master/garrysmod/gamemodes/terrortown/entities/weapons/weapon_ttt_knife.lua