Detecting the vehicle
Hello. I'm just wondering if there is any optimal way to get the closest vehicle in front of player in the distance that equals to some specified amount, without any collision detecting plugin. What do you think guys?
Yes, there is.
Код:
```GetClosestVehicle(playerid)
{
new Float:pX, Float:pY, Float:pZ, Float:cX, Float:cY, Float:cZ, Float:distance = 99999.0, Float:distance2, result = -1;
for(new i = 0; i < MAX_VEHICLES; i++)
{
if (GetPlayerVehicleID(playerid) != i)
{
GetPlayerPos(playerid, pX, pY, pZ);
GetVehiclePos(i, cX, cY, cZ);
distance = floatsqroot(floatpower(floatabs(floatsub(cX, pX)), 2) + floatpower(floatabs(floatsub(cY, pY)), 2) + floatpower(floatabs(floatsub(cZ, pZ)), 2));
distance2 = floatround(distance);
if(distance2 < distance)
{
distance = distance2;
result = i;
}
}
}
return result;
}```
If you want to return the closest vehicle-id do:
Код:
`return result;`
If you want to return the distance do:
Код:
`return distance2;`
GetPlayerCameraTargetVehicle will return the vehicle the player is looking at (but always returns the closest even if it's at the side). About the distance thing I'm not so sure, depending on what you want to do. Getting the distance between vehicle and player and if it is not somewhat equal to that specified amount, return that there's no vehicle or detect it with other way (looping and such is not very efficient unless you have an custom iterator with all the streamed vehicles for each player).
Quote:
 Originally Posted by Konstantinos GetPlayerCameraTargetVehicle will return the vehicle the player is looking at (but always returns the closest even if it's at the side). About the distance thing I'm not so sure, depending on what you want to do. Getting the distance between vehicle and player and if it is not somewhat equal to that specified amount, return that there's no vehicle or detect it with other way (looping and such is not very efficient unless you have an custom iterator with all the streamed vehicles for each player).
There are a bunch tricks to getting closest vehicle, a couple mentioned here, but I'll add this to what he said: there is a IsVehicleStreamedIn(vehicleid, forplayerid) which can be combined with a foreach loop if all the other ways fails (checkin if player is in a vehicle or the first function.) but custom iterators would bypass calling this native which would make it very fast.
I've done it using the camera target. Thank you all!

Forum Jump:

Users browsing this thread: 1 Guest(s)