Applying bullet force to entities

Hello,

I’ve been trying to apply bullet force to entities (Props only really) but for some reason my code will not actually do anything. I can be sure that all of my if statements are going through, but obviously at some point it just decides not to work. Any suggestions?

I have my function hooked on to GM:EntityTakeDamage


function PropPhysics(ent, inflictor, attacker, amount, dmginfo)
	if ent:IsValid() && !ent:IsPlayer() && !ent:IsNPC() then --Check that the entity is of the type we are looking for
		if inflictor:IsPlayer() then
			--Calculate the force multiplier to apply
			local weapMult = GAMEMODE.Multipliers.Weapons[weap]
			weapMult = weapMult && weapMult || 1.0
			local forceMultiplier = GAMEMODE.CVars.PropKnockback:GetFloat() * weapMult
			--Store the velocity of the hit and the velocity of the object before hit
			local hitVel = dmginfo:GetDamageForce()
			local objectVelOld = ent:GetVelocity()
			--Calculate the force to apply to the object
			local objectVel = objectVelOld + (forceMultiplier * hitVel)
			--Apply the force to the object
			ent:SetLocalVelocity(objectVel)
		end
	end
end

If you need any more information don’t hesitate to ask.

From what I’m reading you should use Entity:SetVelocity(vector) rather than LocalVelocity.

Notes

*If the movetype of the entity is MOVETYPE_VPHYSICS (aka, if the entity uses physics simulations) you need to use :SetVelocity on the PHYSICS object of the entity.

*The function actually adds the vector specified to the current velocity, rather than replacing the current velocity.

*Entity:SetLocalVelocity(Vector(x,x,x)) actually sets the Velocity of an entity instead of adding to the entity’s current velocity.

I could be wrong, just try it out!

YOU GOD!

(although what you said didn’t actually work)
But what you posted made me realise what I was doing wrong

So I tested it and it seemed that I should be applying it to the physics object of the entity. Then I realised that it would be more awesome to use ApplyForceOffset because then I could have it rotate and all.

And now it all works! Yipee! (sorry I’m so happy, I only started learning lua a couple of days ago - although I cheated, I know other languages.)

Anyways, my final code:


function PropPhysics(ent, inflictor, attacker, amount, dmginfo)
	if ent:IsValid() && !ent:IsPlayer() && !ent:IsNPC() then --Check that the entity is of the type we are looking for
		if inflictor:IsPlayer() then
			--Calculate the force multiplier to apply
			local weapMult = GAMEMODE.Multipliers.Weapons[weap]
			weapMult = weapMult && weapMult || 1.0
			local forceMultiplier = GAMEMODE.CVars.PropKnockback:GetFloat() * weapMult
			--Store the velocity of the hit and the velocity of the object before hit
			local hitVel = dmginfo:GetDamageForce()
			local hitLoc = dmginfo:GetDamagePosition()
			local objectVelOld = ent:GetVelocity()
			--Calculate the force to apply to the object
			--local objectVel = objectVelOld + (forceMultiplier * hitVel)
			local objectVel = forceMultiplier * hitVel
			--Apply the force to the physics object of the entity
			local physobj = ent:GetPhysicsObject()
			physobj:ApplyForceOffset(objectVel, hitLoc)
		end
	end
end

Haha, goodjob!