SA-MP Forums Archive
GetPlayerTargetPlayer Sniper? - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+--- Thread: GetPlayerTargetPlayer Sniper? (/showthread.php?tid=665335)



GetPlayerTargetPlayer Sniper? - MicroKyrr - 31.03.2019

Quote:

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.

Since GetPlayerTargetPlayer does not work for sniper rifle, is there any other ways to do this? I tried OnPlayerWeaponShot but the target id will know that he is getting shot by bullets because the bullet hit animation is being played.


Re: GetPlayerTargetPlayer Sniper? - Nyzenic - 31.03.2019

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


Re: GetPlayerTargetPlayer Sniper? - raydx - 31.03.2019

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;
}
Usage:

Code:
if(GetPlayerCameraMode(playerid) == 7) 
		{
			new targetid;
			GetPlayerCameraTarget(playerid, targetid, 0.5);  
			if(targetid != INVALID_PLAYER_ID && targetid != playerid)
			{
				your code here
			}
		}
I was using camera mode 46 for camera weapon, but according to wiki sniper rifle is using 7.


Re: GetPlayerTargetPlayer Sniper? - MicroKyrr - 31.03.2019

Quote:
Originally Posted by zerruv
View Post
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
Quote:
Originally Posted by raydx
View Post
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;
}
Usage:

Code:
if(GetPlayerCameraMode(playerid) == 7) 
		{
			new targetid;
			GetPlayerCameraTarget(playerid, targetid, 0.5);  
			if(targetid != INVALID_PLAYER_ID && targetid != playerid)
			{
				your code here
			}
		}
I was using camera mode 46 for camera weapon, but according to wiki sniper rifle is using 7.
I borrowed Logofero's GetPlayerCameraTargetID function, which is the same as your code provided. I decided to use OnPlayerWeaponshot instead since it's way more convenient for me, thanks for all your help.