Closest vehicle to player
#1

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

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.
Reply
#3

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

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

Of course, my mistake.

#define FLOAT_INFINITY (Float:0x7F800000)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)