This has been bugging me too. I've crafted this function to detect when a player is looking backward with their camera:
PHP код:
stock IsAimingBehind(playerid) {
static
Float:cPosX, Float:cPosY, Float:cPosZ,
Float:pPosX, Float:pPosY, Float:pPosZ,
Float:cFacAng, Float:pFacAng;
GetPlayerPos(playerid, pPosX, pPosY, pPosZ);
GetPlayerFacingAngle(playerid, pFacAng);
GetPlayerCameraPos(playerid, cPosX, cPosY, cPosZ);
cFacAng = 90 + (atan2((cPosY - pPosY), (cPosX - pPosX)));
static
Float:cOppFacAng = ((pFacAng < 180) ? (pFacAng + 180) : (pFacAng - 180));
return ((cFacAng - 90) < cOppFacAng < (cFacAng + 90)) || ((cFacAng + 90) < cOppFacAng < (cFacAng - 90));
}
The name of the function is a bit ambiguous. It doesn't check if the player is aiming, but that's something that you can easily change.
This function does the following:
- Calculates the facing angle of the player's camera (Not the player's character)
- Inverses the calculated vector relative to the facing angle of the player (the character)
- Checks if that falls within the 180° behind the player
You can use this in a looping timer to keep checking it when the player is holding KEY_HANDBRAKE / KEY_AIM (if KEY_AIM is defined in your script).
This function still needs some tweaking and improvement, but it works and can be used for now. A current improvement that I can think of is the use of GetPlayerCameraFrontVector
(?). Also while testing this, I noticed that the aim angle is larger than 90° on each side, so this can be improved too.
(?): At least I think GetPlayerCameraFrontVector replaces the calculations that I've done and if so, probably does them more efficiently.
I would appreciate if someone with more insight into this stuff could and wants to help me improve this function (and also explain the process, as I'm interested in learning too)!