07.06.2014, 16:33
When a player is driving a vehicle, how can I get the ID of the vehicle in front of them within a distance of 50m (if there is a vehicle within 50m).
Thanks
Thanks
stock FindClosestVehicle(playerid)
{
new
Float:x, Float:y, Float:z,
closest = -1;
for(new a = 0; a < MAX_VEHICLES; a++)
{
GetVehiclePos(a, x, y, z);
if(a != YOUR_VEHICLEID_OR_ARRAY/VARIBLE && IsPlayerInRangeOfPoint(playerid, 10.0, x, y, z))
{
closest = a;
}
}
if(closest != -1) return closest;
return INVALID_VEHICLE_ID;
}
Except that will check vehicles in all directions. The OP specifically asks for the front (speed radar?). The way I'd do it would be with the streamer plugin. You need to calculate these 3 points (A, B and C) with trigonometry as the input for CreateDynamicPolygon. Then use a similar loop as demonstrated above, while filtering out any vehicles not in the zone.
[i m g]http://puu.sh/9jovP/fa64a6d157.png[/img] |
new Float:x, y, a;
new vid = GetPlayerVehicleID(playerid);
GetVehiclePos(vid, x, y, a);
GetVehicleZAngle(vid, a);
Point C:
x += (50.0 * floatsin(-a, degrees));
y += (50.0 * floatcos(-a, degrees));
Point B:
x += (50.0 * floatsin(-(a - 15), degrees));
y += (50.0 * floatcos(-(a - 15), degrees));
Point D:
x += (50.0 * floatsin(-(a + 15), degrees));
y += (50.0 * floatcos(-(a + 15), degrees));