I salvaged some code from an old and buggy hands swep i found at
to make my own and learn something.
My biggest problem right now is that the prop one is holding is not colliding with anything. If you pick it up, you can drop it out of world easily or into other props.
I reckon it’s because i just set the position and angles in a no-compromise way thus forcing it to stay at the position even if it collides with something, i just cant figure out another way to do it.
The second, minor issue is, that I need a filter for what one can pickup, usually i would do this by
if (tr.Entity:GetClass()=="prop_physics)" then
--pick it up
end
For some reason that just doesnt work. I have to exclude every single type of prop or entity to deny pickup.
if (tr.Entity:GetClass()=="func_door_rotating" || tr.Entity:GetClass()=="func_door" || tr.Entity:GetClass()=="prop_dynamic" || tr.Entity:GetClass()=="prop_static")" then
--pick it up
end
Here’s the full code:
--Hands
if (SERVER) then
AddCSLuaFile( "shared.lua" )
SWEP.Weight = 5
SWEP.AutoSwitchTo = false
SWEP.AutoSwitchFrom = false
end
if ( CLIENT ) then
SWEP.Author = "ProudOne"
SWEP.Contact = ""
SWEP.Purpose = ""
SWEP.Instructions = "Primary picks things up, puts them down, or punches people in their faces, secondary throws things away."
end
SWEP.Spawnable = true
SWEP.AdminSpawnable = true
SWEP.ViewModel = ""
SWEP.WorldModel = ""
SWEP.Primary.ClipSize = -1
SWEP.Primary.DefaultClip = -1
SWEP.Primary.Automatic = false
SWEP.Primary.Ammo = "none"
SWEP.Secondary.ClipSize = -1
SWEP.Secondary.DefaultClip = -1
SWEP.Secondary.Automatic = false
SWEP.Secondary.Ammo = "none"
/*---------------------------------------------------------
Drop all on respawn
---------------------------------------------------------*/
function SWEP:Deploy()
HeldEnt = nil
end
/*---------------------------------------------------------
Reload does nothing
---------------------------------------------------------*/
function SWEP:Reload()
end
/*---------------------------------------------------------
Think holds the stuff in place
---------------------------------------------------------*/
function SWEP:Think()
if ( !HeldEnt ) then return end
local ent = HeldEnt
local phys_ent = HeldEnt:GetPhysicsObject( )
if (!ent:IsValid() || !ent:GetClass()=="prop_physics" ) then
HeldEnt = nil
end
local pos = self.Owner:GetShootPos()
local ang = self.Owner:GetAimVector()
ent:SetPos(pos+(ang*80))
ent:SetAngles(self.Owner:GetAngles())
end
/*---------------------------------------------------------
PrimaryAttack
---------------------------------------------------------*/
function SWEP:PrimaryAttack()
if ( HeldEnt ) then
local phys_ent = HeldEnt:GetPhysicsObject( )
if ( HeldEnt:IsValid() ) then
phys_ent:EnableMotion(true)
phys_ent:ApplyForceCenter(self.Owner:GetForward()*100)
HeldEnt:SetOwner()
HeldEnt = nil
end
else
local pos = self.Owner:GetShootPos()
local ang = self.Owner:GetAimVector()
local armlength = {}
armlength.start = pos
armlength.endpos = pos+(ang*75)
armlength.filter = self.Owner
local tr = util.TraceLine(armlength)
if ( tr.HitWorld || !tr.Entity || !tr.Entity:IsValid() || tr.Entity:GetClass()=="func_door_rotating" || tr.Entity:GetClass()=="func_door" || tr.Entity:GetClass()=="prop_dynamic" || tr.Entity:GetClass()=="prop_static") then
return
end
if ( tr.Entity:IsPlayer() || tr.Entity:IsNPC() ) then
util.BlastDamage(self.Owner, self.Owner, tr.Entity:GetPos(), 1, 3)
return
end
local ent = tr.Entity
local player = self.Owner
local phys_ent = ent:GetPhysicsObject( )
phys_ent:EnableMotion(true)
if ( phys_ent:GetMass() > 50 ) then
phys_ent:ApplyForceCenter( (Vector( self.Owner:GetAimVector() ) * 75) + self.Owner:GetShootPos() * 29 )
end
HeldEnt = ent
HeldEnt:SetOwner( player )
end
end
/*---------------------------------------------------------
SecondaryAttack
---------------------------------------------------------*/
function SWEP:SecondaryAttack()
if ( HeldEnt ) then
local phys_ent = HeldEnt:GetPhysicsObject( )
if ( HeldEnt:IsValid() ) then
phys_ent:EnableMotion(true)
phys_ent:ApplyForceCenter(self.Owner:GetForward()*10000)
HeldEnt:SetOwner()
HeldEnt = nil
end
end
end
/*---------------------------------------------------------
Name: ShouldDropOnDie
Desc: Should this weapon be dropped when its owner dies?
---------------------------------------------------------*/
function SWEP:ShouldDropOnDie()
return false
end