Is there some way to check if the player is behind a certain NPC? For example, for a backstab script in a weapon.
Well I guess you could compare their angles. NPCs are like players and only rotate on one axis so that should be relatively simple.
Oh and you also need the distance between them, I think.
Here's something to get you started :
[lua]--assuming ply and npc are defined
-- And I think the angle to check is the yaw, but I could be wrong.
local AngleLimit = 15 -- Play around with this until it feels right
local PosLimit = 30 -- That too
local pPos, nPos = ply:GetPos(), npc:GetPos()
pPos = Vector(pPos.x,pPos.y,0)
nPos = Vector(nPos.x,nPos.y,0)
local AngleDiff = math.abs( ply:GetAngles().y - npc:GetAngles().y ) -- If they're facing exactly the same way this will be 0.
local PosDiff = pPos:Distance( nPos ) -- We're getting the difference between their x and y coordinates.
if (AngleDiff > AngleLimit) or (PosDiff > PosLimit) then return end
-- Perform backstab here[/lua]
The player has to be both within a certain distance of the NPC and looking in a similar direction. This could certainly be optimized. It's possible it would be better if the pos and angle limits were relative to one another but anyway, try playing around with the limits and see if it works well for you.
I get the angle part, but I'm not really understanding why the player must be within a certain distance of the NPC.
So you cannot backstab someone from 50 meters away?
For the backstab part :downs:
Aw, that makes sense.
[QUOTE=commander204;21059636]So you cannot backstab someone from 50 meters away?[/QUOTE]
Well the trace would take care of that part. I was thinking you could hit in a correct angle but from an incorrect position.. Not sure about it.
I noticed this might not work in one thing I wanted to do. While this might work with the backstab, it won't work with a script that makes the NPC ignore the player (ai_ignoreplayer) when he's behind him, since they won't have the same angle.
You could simply make your own SNPCs that can only detect the player if it enters their FOV. Or maybe existing NPCs have a keyvalue that would do the same thing.
Of course you could also get the direction of a trace between the 2 entities and then compare it to the direction the NPC is facing.
Sorry, you need to Log In to post a reply to this thread.