SA-MP Forums Archive
Closest vehicle to player - 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: Closest vehicle to player (/showthread.php?tid=338681)



Closest vehicle to player - Sphex - 30.04.2012

Hi, how can I find the closest vehicle near player
Thanks.


Re: Closest vehicle to player - MP2 - 30.04.2012

pawn Код:
new Float:dist = FLOAT_INFINITY, Float:tempdist, closest = -1, Float:x, Float:y, Float:z;
        for(new i=0; i<MAX_VEHICLES; i++)
        {
            if(IsVehicleStreamedIn(i, playerid))
            {
                GetVehiclePos(i, x, y, z);
                tempdist = GetVehicleDistanceFromPoint(GetPlayerVehicleID(playerid), x, y, z);
                if(tempdist > dist) continue;
                closest = i;
                dist = GetVehicleDistanceFromPoint(GetPlayerVehicleID(playerid), x, y, z);
            }
        }
        if(closest == -1) return SendErrorMessage(playerid, "No vehicles nearby to attach. Use /TOW [VEHICLE ID] to attach a specific trailer to your vehicle.");
This is a part of my script, it is NOT a copy and paste job. Look at it, edit it and learn. You may need to define FLOAT_INFINITY.


Re: Closest vehicle to player - Sphex - 30.04.2012

Thanks, I think I got it.
The INFINITY Is like the maximum distance?


Re: Closest vehicle to player - Joe Staff - 30.04.2012

Yes, also you could reduce some redundancy by setting
pawn Код:
dist=tempdist



Re: Closest vehicle to player - MP2 - 30.04.2012

Of course, my mistake.

#define FLOAT_INFINITY (Float:0x7F800000)