• Handling player collisions with one another
    5 replies, posted
I am quite new to Lua in Gmod and am simply trying to find the right function/hook to handle collision amongst players. As soon as they collide I would like to execute certain commands, seems simple enough? The ShouldCollide hook doesn't seem to handle collision like this since it only determines whether players should collide, and not what should happen as soon as they do (it senses collision long before any collision actually occurs). The StartTouch hook would be exactly what I'm looking for but it only works when used on an entity and not on player objects, unless I am not using it properly. The only examples of collision groups I can find is to disable player collision, which is not what I want. Any help would be appreciated.
[lua] hook.Add("ShouldCollide","DoThings",function(p1,p2) if p1:IsPlayer() && p2:IsPlayer() then -- Do stuff -- return true or false end end)[/lua] Why not just use ShouldCollide? It gets called at time of collision, and you just tell the code whether or not to collide them.
I have tried that, but the problem is it seems to detect collision whenever you are even remotely near another player even if still many feet apart. If you output a Msg you can see how often it actually occurs, versus only triggering when the players are actually in contact.
You could do it kind of hackily and just check the distance between players.
You can try using the ShouldCollide hook and use [lua]if ent1:NearestPoint(ent2:GetPos):Distance(ent2:NearestPoint(ent1:GetPos()) < 1 then[/lua] It should actually return when it collides then, but this is untested so i am not sure if it would work.
Stop thinking the ShouldCollide hook has anything to do with collisions about to happen, just check the distance between the players in a Think hook.
Sorry, you need to Log In to post a reply to this thread.