[HELP] Get vehicle door (hood, trunk, doors etc.) coords


#1

Hi.

I need to get location of one door from car that player was recently inside.

I want to make script that shows 3D text on position of (for example) trunk.
If player is nearby trunk (NOT CAR), 3D text shows up above, and player is able to open it.

I want to use GetDistanceBetweenCoords, but I need to know coords of specified doors in car.
Is it possible?


#2

Maybe GetWorldPositionOfEntityBone https://runtime.fivem.net/doc/natives/#_0x44A8FCB8ED227738 ?


#3

hmm, I can’t find bone name of trunk doors, but, I don’t really need trunk doors but something near.

So I tried something like this, but I do something wrong:

Citizen.CreateThread(function()
	while true do
		local ped = GetPlayerPed(-1)
		local bool, vehicle = GetEntityPlayerIsFreeAimingAt(ped)
		if bool then
			if IsEntityAVehicle(vehicle) then
				local trunkpos = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "taillight_l"))
				local distanceToTrunk = GetDistanceBetweenCoords(GetEntityCoords(ped), GetEntityCoords(trunkpos), 1)
				if distanceToTrunk <= trunkDistance then
					ShowAboveRadarMessage("DEBUG 1")
				end
			end
		end
		Citizen.Wait(0)
	end
end)

oh yeah, ,trunkDistance" is defined on the beginning of my script*
And “ShowAboveRadarMessage” is defined function to display GTA notifications.


#4

Not sure if this will help you or not.


#5

Not really.
Opening is not the problem.
The problem is to get coords of trunk or something nearby, to get a posibility to draw 3D text above it, and calculate distance between player and trunk player is looking at.

My scritpt above should be 80% done for that, but something doesn’t work.

Extrenal trunk scritpt uses car coords, what is not what I need.


#6
Citizen.CreateThread(function()
	while true do
		local bool, vehicle = GetEntityPlayerIsFreeAimingAt(PlayerId())
		if bool then
			if IsEntityAVehicle(vehicle) then
				local trunkpos = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "boot"))
				local distanceToVeh = GetDistanceBetweenCoords(GetEntityCoords(PlayerId()), GetEntityCoords(trunkpos), 1)
				if distanceToVeh < 6 then
                    print("It Worked!")
				end
			end
		end
		Citizen.Wait(0)
	end
end)

#7

Edited code to get trunk of nearest car, not the car that player is looking, because everytime I want to get it working, I need to click RMB…

Citizen.CreateThread(function()
	while true do
		local vehicle = getNearestVeh()
			if IsEntityAVehicle(vehicle) then
				local trunkpos = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "boot"))
				local distanceToTrunk = GetDistanceBetweenCoords(GetEntityCoords(PlayerId()), GetEntityCoords(trunkpos), 1)
				if distanceToTrunk < 1 then
					print('it works!')
			    end
			end
		Citizen.Wait(0)
	end
end)

function getNearestVeh()
	local pos = GetEntityCoords(GetPlayerPed(-1))
	local entityWorld = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 0.0, 20.0, 0.0)

	local rayHandle = CastRayPointToPoint(pos.x, pos.y, pos.z, entityWorld.x, entityWorld.y, entityWorld.z, 10, GetPlayerPed(-1), 0)
	local _, _, _, _, vehicleHandle = GetRaycastResult(rayHandle)
	return vehicleHandle
end

#8

Wasn’t sure what you were looking for exactly, I was going based off your code. :+1:


#9

Or maybe you have idea how to use GetEntityPlayerIsFreeAimingAt without clicking RMB everytime I am looking at car?


#10

The code you posted above should work.


#11

Area from trunk to player is too wide, I need to get it closer but I can’t or I am doing something wrong.
Any Idea?

Citizen.CreateThread(function()
	while true do
		local vehicle = getNearestVeh()
		if IsEntityAVehicle(vehicle) then
			local trunkpos = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "boot"))
			local distanceToTrunk = GetDistanceBetweenCoords(GetEntityCoords(PlayerId()), GetEntityCoords(trunkpos), 1)
			print(distanceToTrunk)
			if distanceToTrunk <= 0.0 then
				local textpos = vector3(trunkpos)
				DrawText3D(textpos.x, textpos.y, textpos.z, "PRESS TO OPEN")
			end
		end
		Citizen.Wait(0)
	end
end)


function getNearestVeh()
	local pos = GetEntityCoords(GetPlayerPed(-1))
	local entityWorld = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 0.0, 20.0, 0.0)

	local rayHandle = CastRayPointToPoint(pos.x, pos.y, pos.z, entityWorld.x, entityWorld.y, entityWorld.z, 10, GetPlayerPed(-1), 0)
	local _, _, _, _, vehicleHandle = GetRaycastResult(rayHandle)

	return vehicleHandle
end

#12

There’s a limit to the distance when it comes to Fivem. 1 being the minimum distance, still seems far but that’s as close at it will get.


#14

Okay, part of code was bad, now it works how it should.

Citizen.CreateThread(function()
	while true do
		local coordA = GetEntityCoords(GetPlayerPed(-1), 1)
		local coordB = GetOffsetFromEntityInWorldCoords(GetPlayerPed(-1), 0.0, 1.0, 0.0)
		local vehicle = getVehicleInDirection(coordA, coordB)
		if DoesEntityExist(vehicle) and IsEntityAVehicle(vehicle) then
			local trunkpos = GetWorldPositionOfEntityBone(vehicle, GetEntityBoneIndexByName(vehicle, "boot"))
			local playerpos = GetEntityCoords(GetPlayerPed(-1), 1)
			local distanceToTrunk = GetDistanceBetweenCoords(trunkpos, playerpos, 1)
			if distanceToTrunk < 1 then
                DrawText3D(trunkpos.x, trunkpos.y, trunkpos.z - 0.3, "PRESS TO OPEN")
			end
		end
		Citizen.Wait(0)
	end
end)


function getVehicleInDirection(coordFrom, coordTo)
	local rayHandle = CastRayPointToPoint(coordFrom.x, coordFrom.y, coordFrom.z, coordTo.x, coordTo.y, coordTo.z, 10, GetPlayerPed(-1), 0)
	local _, _, _, _, vehicle = GetRaycastResult(rayHandle)
	return vehicle
end