SA-MP Forums Archive
Closest Vehicle - 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)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: Closest Vehicle (/showthread.php?tid=244073)



Closest Vehicle - austin070 - 26.03.2011

How could I target the closest vehicle to me? Example: I type /plate and it gets the closest vehicle's plate.


Re: Closest Vehicle - THE_KNOWN - 26.03.2011

i do it like this:

Код:
	if(newkeys == KEY_FIRE)
	{
	    if(!IsPlayerInAnyVehicle(playerid)) return 1;
		new v,Float:d=10.0,vehicleid;
		vehicleid=GetPlayerVehicleID(playerid);
		if(GetVehicleModel(vehicleid) != 525) return 1;
		if(IsTrailerAttachedToVehicle(vehicleid)) return DetachTrailerFromVehicle(vehicleid);
	    for(new i=0;i<MAX_VEHICLES;i++)
	    {
	    new Float:t;
		t=GetDistanceBetweenVehicles(vehicleid,i);
		if(t<d && i != vehicleid)
		    {
		    d=t;
		    v=i;
		    }
		}
		AttachTrailerToVehicle(v,GetPlayerVehicleID(playerid));
	}


stock GetDistanceBetweenVehicles(vehicleid,vehicleid2)
{
    new Float:x1,Float:y1,Float:z1,Float:x2,Float:y2,Float:z2;
    new Float:tmpdis;
    GetVehiclePos(vehicleid,x1,y1,z1);
    GetVehiclePos(vehicleid2,x2,y2,z2);
    tmpdis = floatsqroot(floatpower(floatabs(floatsub(x2,x1)),2)+floatpower(floatabs(floatsub(y2,y1)),2)+floatpower(floatabs(floatsub(z2,z1)),2));
    return floatround(tmpdis);
}
well i use it for towing but you can just get an idea with that. GL