Identify player hit by physics object (or anything)

I’ve made a swep that launches stunsticks and I’d like for the player/npc/object to get knocked back a bit without taking major damage. (of course, being able to just type in “SetMass(2000)” this isn’t really a problem for the object part, I’m more curious about knocking back players and npcs without ravaging them.) I’d like to be able to make the “bolt” identify what it’s hitting, and if it’s a player, to knock them back and up by changing their velocity and angles(?)

Here’s what I’ve got thus far

[lua]function SWEP:PrimaryAttack()

self:SendWeaponAnim( ACT_VM_PRIMARYATTACK )
self:SetNextPrimaryFire( CurTime() + 0.6 )

local forward = self.Owner:EyeAngles():Forward()
local bolt = ents.Create( "prop_physics" )
	bolt:SetModel( "models/weapons/w_stunbaton.mdl")
	bolt:SetPos( self.Owner:GetShootPos() + forward * 10 )
	bolt:SetAngles( self.Owner:EyeAngles() )
	bolt:Spawn()
	bolt:GetPhysicsObject():SetMass(1)
	bolt:GetPhysicsObject():SetVelocity( forward * 6000 )
	bolt:SetPhysicsAttacker( self.Owner )
	bolt:Fire("kill",0,2)

end[/lua]

Use Player.GetEyeTrace.

Example:



local trace = self.Owner:GetEyeTrace()

if trace.Entity:IsPlayer() then
    local ply = trace.Entity;
    ply:PrintMessage( HUD_PRINTTALK, "You just got shot!" );
end


You apply the velocity to the ply object, calculate from where self.Owners position is so you don’t get knocked towards the target.

Took some rearranging of text and some brain power, but I got to work, thanks to you :slight_smile: