Getting Closest vehicleid to player
#1

How do I get the vehicleid of the closest vehicle to a player? It's going to be for my RP GM's /scanlicenseplate command. So any ideas how to do this?

Thanks in advance!
Reply
#2

pawn Код:
stock GetNearestVehicle(playerid, Float:dis)
{
    new Float:L, Float:O, Float:II;
    if(GetPlayerPos(playerid, L, O, II))
    {
        new vehicleid = INVALID_VEHICLE_ID;
        for(new v, Float:temp, Float:VL, Float:VO, Float:VII; v != MAX_VEHICLES; v++)
        {
            if(GetVehiclePos(v, VL, VO, VII) && v != GetPlayerVehicleID(playerid))
            {
                VL -= L, VO -= O, VII -= II;
                temp = VL * VL + VO * VO + VII * VII;
                if(temp < dis) dis = temp, vehicleid = v;
            }
        }
        dis = floatpower(dis, 1.5);
        return vehicleid;
    }
    return INVALID_VEHICLE_ID;
}
Reply
#3

EDIT: OH this is a stock. So when I'm using under the " if(strcmp(cmd, "/scanlicenseplate", true) == 0) " how would I add it?
Reply
#4

I'll show u a example:
pawn Код:
CMD:repaircar(playerid, params[])
{
    new nearest = GetNearestVehicle(playerid, 25.0); // It searches the nearest vehicle in radius of 25 metters
    RepairVehicle(nearest); // Repairs Vehicle
    return true;
}
Reply
#5

Thanks! I'll try it now!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)