I’ve been trying to select a vehicle at a given coordinate. However, none of the functions seem to be working. I have tried GetClosestVehicle, GetRandomVehicleInSphere, and IsAnyVehicleNearPoint. I have tried many different parameters for each. I have also tried on players and NPCs. No luck.
@Tusticles Yes. That is what I have been referencing for the most part. In addition, I have read all the additional documentation found in the links of that description.
Here a small function that get the car in the direction you specify.
-- From point A to B -- Use GetOffsetFromEntityInWorldCoords for distance
function getVehicleInDirection(coordFrom, coordTo)
local rayHandle = CastRayPointToPoint(coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 10, GetPlayerPed(-1), 0)
local a, b, c, d, vehicle = GetRaycastResult(rayHandle)
return vehicle
end
Exemple:
if IsControlJustReleased(0, 51) then
local coordA = GetEntityCoords(GetPlayerPed(-1), 1)
local coordB = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 0.0, 20.0, 0.0)
local targetVehicle = getVehicleInDirection(coordA, coordB)
-- HOLY SHIT THE VEHICLE IS RIGHT HERE ---> targetVehicle
end
local pos = GetEntityCoords(GetPlayerPed(-1))
local vehicle = GetClosestVehicle(pos['x'], pos['y'], pos['z'], 5.001, 0, 70)
if DoesEntityExist(vehicle) then
-- do_something
end
works fine.
and you can use this flag list:
00000000000000000 = 0 - Only returns cars and motorbikes. Often returns vehicle already inside. Otherwise only return empty vehicles.
00000000000000010 = 2 - Only empty vehicles. Only returns cars and motorbikes.
00000000000000100 = 4 - Works like 70. During the testing, this one worked the best i.e. for cars and motorbikes
00000000000000110 = 6 - Works like 70.
00000000000000111 = 7 - Works like 70.
00000000000010111 = 23 - Only finds cars when not inside one.
00000000001000110 = 70 - (Not from scripts. Recommended by the native db). Only works with motorbikes and cars.
00000000001111111 = 127 - While inside cars or motorbikes, nothing can be found. On foot or inside heli cars can be found.
00000000100000100 = 260 - Works like 70.
00000100001100010 = 2146
00000100001111111 = 2175
00011000000000110 = 12294
00100000000000000 = 16384
00100000000000010 = 16386
00101000000010111 = 20503
01000000000000000 = 32768
10000100000000110 = 67590
10000100001111111 = 67711 - Finds cars when inside heli, not when inside cars.
11000000000000101 = 98309
11000100000000111 = 100359 - Works like 70 but returns the vehicle the player already is in quite often.
11111111111111111 = 131071 - Not from the scripts. Nothing seems to work when testing.
00100000000000000 = 16384 - returns planes only.
Hello, id to return some function GetClosestVehicle() correspond to what exactly? On an entity of type Player, or is PlayerPed, serverid or other?
How to transform it there playerPed please
i have tested GetPlayerServerId(), GetPlayerPed() and GetPlayerFromServerId()
for get player name (GetPlayerName()) dont work
I usually use the flag 12294 instead of 70 on get closest vehicle (cant remember why), though, with modded cars I can never use this method, only using cast rays which I have always reverted to using.