Jump to content

[HELP]Getting nearest door of car


Recommended Posts

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)

 

Link to comment

Here's example (based of XricsmondX  code):

local distances = {}
local currentVeh = nil

local startTick = nil
local animTime = 1000  -- in miliseconds

local toRotation = nil
local fromRotation = nil

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


local function interpolate()
  local progress = (getTickCount() - startTick) / animTime
  local rotZ = interpolateBetween(fromRotation, 0, 0, toRotation, 0, 0, progress, "Linear")
  setVehicleComponentRotation(currentVeh, distances[1][2], 0, 0, rotZ)
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])
    
    currentVeh = nVeh
    fromRotation = 0 --here's start rotation of door 
    toRotation = -45 --here's end rotation 
    startTick = getTickCount()
    addEventHandler("onClientRender", getRootElement(), interpolate)
	--setVehicleComponentRotation(nVeh,distances[1][2],0,0,-45)
end)

Or you also can use deltaTime(timeSlice) from onClientPreRender. But in my opinion interpolateBetween is better idea.

I forgot - you also should check if progress == 1 and use removeEventHandler in interpolate function. 

 

Edited by Verius
tip
Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...