• Check for collsion between NPC and player
    6 replies, posted
I was wondering if it is possible to hook a collision StartTouch between an npc(not SNPC) and a player.
Totally untested : [lua]Meta = FindMetaTable("Player") function Meta:StartTouch(ent) if ( ent:IsValid() and ent:IsNPC() ) then -- Do Stuff end end[/lua]
[lua]local Meta = FindMetaTable("player") local OldTouchFuncs = Meta.StartTouch function Meta:StartTouch(ent) OldTouchFuncs() if ( ent:IsValid() and ent:IsNPC() ) then -- Do Stuff end end[/lua] Slight modification of the above, should prevent overwriting of other functions there.
The Player metatable doesn't have a "StartTouch" hook like SENTs AFAIK...
Maybe do some simple box collision detection using the OBB's of both?
[QUOTE=ralle105;16988550]Maybe do some simple box collision detection using the OBB's of both?[/QUOTE] Actually, since player collision doesn't move off their AABB, it would probably be better to use that. I don't know if they even have a rotating OBB.
[QUOTE=Skyhawk;16988678]Actually, since player collision doesn't move off their AABB, it would probably be better to use that. I don't know if they even have a rotating OBB.[/QUOTE] [lua] function isColliding(ply, NPC) local plyRad = ply:OBBMaxs().x local npcRad = NPC:OBBMaxs().x local plyPos = ply:GetPos() local npcPos = NPC:GetPos() local plyZ = plyPos.z plyPos.z = npcPos.z if (plyPos:Distance(npcPos) < plyRad+npcRad) then local plyH = ply:OBBMaxs().z local npcH = NPC:OBBMaxs().z if (plyZ > npcPos.z) then if (npcPos.z+npcH > plyZ) then return true end else if (plyZ+plyH > npcPos.z) then return true end end end end [/lua] Uses two "cylinders" to work out if they're colliding. Untested.
Sorry, you need to Log In to post a reply to this thread.