An alternative way of doing both of these together is through some vector calculations. You want to essentially do step 3 of the image below:
![[Image: angle_vectors.png]](https://www.analyzemath.com/stepbystep_mathworksheets/3D_vectors/angle_vectors.png)
Some rough code (that I didn't test for accuracy) would look like:
![[Image: angle_vectors.png]](https://www.analyzemath.com/stepbystep_mathworksheets/3D_vectors/angle_vectors.png)
Some rough code (that I didn't test for accuracy) would look like:
PHP Code:
new Float:xn, Float:yn, Float:zn;
FCNPC_GetPosition(npcid, xn, yn, zn);
// Get player position
new Float:xp, Float:yp, Float:zp;
if(!IsPlayerNPC(playerid)) {
GetPlayerPos(playerid, xp, yp, zp);
} else {
FCNPC_GetPosition(playerid, xp, yp, zp);
}
// First the vector from the NPC to the player
Float:vector_diff[3] = {xp-xn, yp-yn, zp-zn};
Float:facing_angle = FCNPC_GetAngle(npcid) + 90; // add 90 degrees to align with unit circle
// Then a unit vector for the angle the NPC is facing - nb. norm of this vector does not matter, but for unit vectors it's 1
Float:vector_facing[3] = {floatcos(facing_angle), floatsin(facing_angle), 0.0f};
// Calculate the dot product, to get the numerator:
Float:dot_product = vector_diff[0] * vector_facing[0] +
vector_diff[1] * vector_facing[1] +
vector_diff[2] * vector_facing[2];
// Calculate the denominator
Float:normcalc = VectorSize(vector_diff[0], vector_diff[1], vector_diff[2]); // VectorSize(vector_facing) = 1, so multiplication is not needed in this special case
//Then this is the angle between the facing angle and the player
Float:angle = acos(dot_product/normcalc);
return angle <= max_viewing_angle; // Insert your own variable here


