I writed a half script, you can see the method.
function getNearestVehicle(player,distance)
local tempTable = {}
local lastMinDis = distance-0.0001
local nearestVeh = false
local px,py,pz = getElementPosition(player)
local pint = getElementInterior(player)
local pdim = getElementDimension(player)
for _,v in pairs(getElementsByType("vehicle")) do
local vint,vdim = getElementInterior(v),getElementDimension(v)
if vint == pint and vdim == pdim then
local vx,vy,vz = getElementPosition(v)
local dis = getDistanceBetweenPoints3D(px,py,pz,vx,vy,vz)
if dis < distance then
if dis < lastMinDis then
lastMinDis = dis
nearestVeh = v
end
end
end
end
return nearestVeh
end
bindKey("mouse2","down",function()
nVeh = getNearestVehicle(localPlayer,5)
door_rf_dummyX,door_rf_dummyY,door_rf_dummyZ = getVehicleComponentPosition ( nVeh, "door_rf_dummy", "world" )
door_lf_dummyX,door_lf_dummyY,door_lf_dummyZ = getVehicleComponentPosition ( nVeh, "door_lf_dummy", "world" )
door_rb_dummyX,door_rb_dummyY,door_rb_dummyZ = getVehicleComponentPosition ( nVeh, "door_rb_dummy", "world" ) or 0,0,0
door_lb_dummyX,door_lb_dummyY,door_lb_dummyZ = getVehicleComponentPosition ( nVeh, "door_lb_dummy", "world" ) or 0,0,0
px,py,pz = getElementPosition(localPlayer)
distances = {
{getDistanceBetweenPoints3D(px,py,pz,door_rf_dummyX,door_rf_dummyY,door_rf_dummyZ),"door_rf_dummy"},
{getDistanceBetweenPoints3D(px,py,pz,door_lf_dummyX,door_lf_dummyY,door_lf_dummyZ),"door_lf_dummy"},
{getDistanceBetweenPoints3D(px,py,pz,door_rb_dummyX,door_rb_dummyY,door_rb_dummyZ),"door_rb_dummy"},
{getDistanceBetweenPoints3D(px,py,pz,door_lb_dummyX,door_lb_dummyY,door_lb_dummyZ),"door_lb_dummy"},
}
table.sort(distances,function(a, b) return a[1] < b[1] end)
outputChatBox(distances[1][2])
setVehicleComponentRotation(nVeh,distances[1][2],0,0,-45)
end)