SA-MP Forums Archive
Detecting the 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)
+--- Thread: Detecting the vehicle (/showthread.php?tid=608186)



Detecting the vehicle - cdoubleoper - 28.05.2016

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?


Re: Detecting the vehicle - Darkwood17 - 28.05.2016

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;



Re: Detecting the vehicle - Konstantinos - 28.05.2016

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).


Re: Detecting the vehicle - PrO.GameR - 28.05.2016

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.


Re: Detecting the vehicle - cdoubleoper - 28.05.2016

I've done it using the camera target. Thank you all!