Get nearest player
#1

Hey guys, I need a little help. I have a police radar command that is used as follows: "/radar [playerid]". It works, but instead of entering a player's id, I want it to find the velocity of the nearest player's vehicle. I know this is possible, and I have ******d it, but I've found nothing, so don't say "GO ****** IT NOOB." Can someone show me how to do it or point me in the right direction to a tutorial?
Reply
#2

This will start you off.

pawn Code:
forward GetClosestPlayerToPlayer(playerid);
public GetClosestPlayerToPlayer(playerid)
{
    new Float:dist = 1000.0;
    new targetid = INVALID_PLAYER_ID;
    new Float:x1,Float:y1,Float:z1;
    new Float:x2,Float:y2,Float:z2;
    new Float:tmpdis;
    GetPlayerPos(playerid,x1,y1,z1);
    for(new i=0;i<MAX_PLAYERS;i++)
    {
        if(i == playerid) continue;
        GetPlayerPos(i,x2,y2,z2);
        tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
        if(tmpdis < dist)
        {
            dist = tmpdis;
            targetid = i;
        }
    }
    return targetid;
}
Reply
#3

Thanks. It worked fine.
Reply


Forum Jump:


Users browsing this thread: 2 Guest(s)