Does not work for the sniper rifle, as it doesn't lock on to anything and as such can't and won't return a player. |
stock GetPlayerCameraTarget(playerid, &targetid, Float:sphere_range= 1.0, Float:max_dist = 500.0) { new Float:p1[3], Float:p2[3], Float:sc[3]; new Float:cam_pos[3], Float:cam_fv[3]; const Float:fov = 5.0; GetPlayerCameraPos(playerid, cam_pos[0], cam_pos[1], cam_pos[2]); GetPlayerCameraFrontVector(playerid, cam_fv[0], cam_fv[1], cam_fv[2]); p1[0] = cam_pos[0] + floatmul(cam_fv[0], 1.0); p1[1] = cam_pos[1] + floatmul(cam_fv[1], 1.0); p1[2] = cam_pos[2] + floatmul(cam_fv[2], 1.0); p2[0] = cam_pos[0] + floatmul(cam_fv[0], fov); p2[1] = cam_pos[1] + floatmul(cam_fv[1], fov); p2[2] = cam_pos[2] + floatmul(cam_fv[2], fov); new Float:dist=0.0, Float:dist2=0.0; targetid = INVALID_PLAYER_ID; foreach(Player, i) { GetPlayerPos(i, sc[0], sc[1], sc[2]); if(IsVectorInSphere(p1, p2, sc, sphere_range, max_dist)) { dist2 = VectorSize(p1[0]-sc[0], p1[1]-sc[1], p1[2]-sc[2]); if(dist == 0.0 || dist2 < dist) { targetid = i; dist = dist2; } } } if (targetid == INVALID_PLAYER_ID) return 0; return 1; } stock IsVectorInShere(Float:p1[3], Float:p2[3], Float:sc[3], Float:sphere_range, Float:max_range) { if(VectorSize(p1[0]-sc[0], p1[1]-sc[1], p1[2]-sc[2]) <= max_range) { new Float:a, Float:b, Float:c, Float:bb4ac, Float:dp[3]; dp[0] = p2[0] - p1[0]; dp[1] = p2[1] - p1[1]; dp[2] = p2[2] - p1[2]; a = dp[0] * dp[0] + dp[1] * dp[1] + dp[2] * dp[2]; b = 2 * (dp[0] * (p1[0] - sc[0]) + dp[1] * (p1[1] - sc[1]) + dp[2] * (p1[2] - sc[2])); c = sc[0] * sc[0] + sc[1] * sc[1] + sc[2] * sc[2]; c += p1[0] * p1[0] + p1[1] * p1[1] + p1[2] * p1[2]; c -= 2 * (sc[0] * p1[0] + sc[1] * p1[1] + sc[2] * p1[2]); c -= sphere_range * sphere_range; bb4ac = b * b - 4 * a * c; if(bb4ac < 0) return 0; return 1; } return 0; }
if(GetPlayerCameraMode(playerid) == 7) { new targetid; GetPlayerCameraTarget(playerid, targetid, 0.5); if(targetid != INVALID_PLAYER_ID && targetid != playerid) { your code here } }
I guess you could try GetPlayerCameraFrontVector and check if the coordinates are in range of the target player.
https://sampwiki.blast.hk/wiki/GetPlayerCameraFrontVector or just simply https://sampwiki.blast.hk/wiki/GetPlayerCameraTargetPlayer |
Functions:
Code:
stock GetPlayerCameraTarget(playerid, &targetid, Float:sphere_range= 1.0, Float:max_dist = 500.0) { new Float:p1[3], Float:p2[3], Float:sc[3]; new Float:cam_pos[3], Float:cam_fv[3]; const Float:fov = 5.0; GetPlayerCameraPos(playerid, cam_pos[0], cam_pos[1], cam_pos[2]); GetPlayerCameraFrontVector(playerid, cam_fv[0], cam_fv[1], cam_fv[2]); p1[0] = cam_pos[0] + floatmul(cam_fv[0], 1.0); p1[1] = cam_pos[1] + floatmul(cam_fv[1], 1.0); p1[2] = cam_pos[2] + floatmul(cam_fv[2], 1.0); p2[0] = cam_pos[0] + floatmul(cam_fv[0], fov); p2[1] = cam_pos[1] + floatmul(cam_fv[1], fov); p2[2] = cam_pos[2] + floatmul(cam_fv[2], fov); new Float:dist=0.0, Float:dist2=0.0; targetid = INVALID_PLAYER_ID; foreach(Player, i) { GetPlayerPos(i, sc[0], sc[1], sc[2]); if(IsVectorInSphere(p1, p2, sc, sphere_range, max_dist)) { dist2 = VectorSize(p1[0]-sc[0], p1[1]-sc[1], p1[2]-sc[2]); if(dist == 0.0 || dist2 < dist) { targetid = i; dist = dist2; } } } if (targetid == INVALID_PLAYER_ID) return 0; return 1; } stock IsVectorInShere(Float:p1[3], Float:p2[3], Float:sc[3], Float:sphere_range, Float:max_range) { if(VectorSize(p1[0]-sc[0], p1[1]-sc[1], p1[2]-sc[2]) <= max_range) { new Float:a, Float:b, Float:c, Float:bb4ac, Float:dp[3]; dp[0] = p2[0] - p1[0]; dp[1] = p2[1] - p1[1]; dp[2] = p2[2] - p1[2]; a = dp[0] * dp[0] + dp[1] * dp[1] + dp[2] * dp[2]; b = 2 * (dp[0] * (p1[0] - sc[0]) + dp[1] * (p1[1] - sc[1]) + dp[2] * (p1[2] - sc[2])); c = sc[0] * sc[0] + sc[1] * sc[1] + sc[2] * sc[2]; c += p1[0] * p1[0] + p1[1] * p1[1] + p1[2] * p1[2]; c -= 2 * (sc[0] * p1[0] + sc[1] * p1[1] + sc[2] * p1[2]); c -= sphere_range * sphere_range; bb4ac = b * b - 4 * a * c; if(bb4ac < 0) return 0; return 1; } return 0; } Code:
if(GetPlayerCameraMode(playerid) == 7) { new targetid; GetPlayerCameraTarget(playerid, targetid, 0.5); if(targetid != INVALID_PLAYER_ID && targetid != playerid) { your code here } } |