[Help] i need Calc " GetGameplayCamRelativePitch() " Get angle between two 3D Vectors

i use this code i tested in another engine

I want take angle this RED line and add on Camera / SetGameplayCamRelativePitch()

Get angle between two 3D Vectors like GetGameplayCamRelativePitch()

local camcoords=GetGameplayCamRot(0)
local targetCoords = GetEntityRotation(targetped, 2)
ansAgain = math.acos(myDot(coord, targetCoords) / (myMag(coord) * myMag(targetCoords)))
SetFocusArea(targetCoords.x, targetCoords.y, targetCoords.z, 0.0, 0.0, 0.0)
SetFocusEntity(object1)
--SetCamCoord(cam, targetCoords.x, targetCoords.y, targetCoords.z)    
angle = math.atan2 ( camcoords.y - targetCoords.y, targetCoords.x - camcoords.x)  
angle2 = -math.atan2 ( targetCoords.y - camcoords.y, targetCoords.x - camcoords.x )

SetGameplayCamRawPitch(angle)
angulo = GetGameplayCamRelativePitch()
print(angulo,angle,angle2 ,math.deg(ansAgain))

i used source: math - New to Lua - Get angle between two 3D Vectors - Stack Overflow

What exactly are you trying to achieve?
Just from looking at your code I’m not sure what you want to do. There might be an easy solution if you explain it :slight_smile:

i want set defalt camera look to target ped or entry

SetGameplayCamRawPitch and SetGameplayCamRelativeHeading

function makeEntityFaceEntity( entity1, entity2 )
local p1 = GetEntityCoords(entity1, true)
local p2 = GetEntityCoords(entity2, true)

	local dx = p2.x - p1.x
	local dy = p2.y - p1.y

	local heading = GetHeadingFromVector_2d(dx, dy)
	SetEntityHeading( entity1, heading )
end

I made that script for fun, you will need to add the ability to move the camera around if u want so,
but the main logic is here, basically allow u to focus the camera on the entity target :

local selectedTarget = nil
local isFocus = false

local function RotationToDirection(rot)
	local z = math.rad(rot.z)
	local x = math.rad(rot.x)
	local num = math.abs(math.cos(x))
	return vector3(-math.sin(z) * num, math.cos(z) * num, math.sin(x))
end

Citizen.CreateThread(function()
	while true do
		Wait(0)

		local camCoords = GetGameplayCamCoord()
		local direction = RotationToDirection(GetGameplayCamRot(2))
		local rayTo = camCoords + direction * 100.0

		local rayHandle = StartShapeTestRay(
			camCoords.x, camCoords.y, camCoords.z,
			rayTo.x, rayTo.y, rayTo.z,
			-1, PlayerPedId(), 0
		)

		local _, hit, endCoords, _, entityHit = GetShapeTestResult(rayHandle)

		if hit and DoesEntityExist(entityHit) then
			local entityType = GetEntityType(entityHit)

			local isPed = (entityType == 1 and not IsPedAPlayer(entityHit))
			local isObject = (entityType == 3)
			local isVehicle = (entityType == 2 and IsEntityAVehicle(entityHit))

			if isPed or isObject or isVehicle then
				DrawMarker(0, endCoords.x, endCoords.y, endCoords.z + 1.0, 0, 0, 0, 0, 0, 0,
					0.3, 0.3, 0.3, 0, 255, 0, 100, false, true, 2, nil, nil, false)

				if IsControlJustPressed(0, 38) then -->  E
					if isFocus then
						selectedTarget = entityHit
						print("Entity selected: " .. tostring(selectedTarget))
					end
					isFocus = not isFocus
				end
			end
		end
	end
end)



RegisterCommand("cam", function()
	if not selectedTarget or not DoesEntityExist(selectedTarget) then
		return
	end

	local targetCoords = GetEntityCoords(selectedTarget)

	local cam = CreateCam("DEFAULT_SCRIPTED_CAMERA", true)
	local camOffset = vector3(0.0, -8.0, 2.0) --> offset
	local camPos = targetCoords + camOffset  --> cam pos relative

	SetCamCoord(cam, camPos.x, camPos.y, camPos.z)
	PointCamAtEntity(cam, selectedTarget, 0.0, 0.0, 0.0)
	SetCamActive(cam, true)
	RenderScriptCams(true, false, 0, true, true)
	SetFocusEntity(selectedTarget)

	-- this thread is here to allow the camera to follow the entity target while focus :
	Citizen.CreateThread(function()
		while DoesEntityExist(selectedTarget) do
			local targetCoords = GetEntityCoords(selectedTarget)

			local camPos = targetCoords + camOffset
			SetCamCoord(cam, camPos.x, camPos.y, camPos.z)
			PointCamAtEntity(cam, selectedTarget, 0.0, 0.0, 0.0)

			Citizen.Wait(0)
		end
	end)

	-- Destroy the camera after 10 seconds
	Citizen.Wait(10000) 
	if DoesCamExist(cam) then
		DestroyCam(cam, false)
		RenderScriptCams(false, false, 0, true, true)
	end
end)

have fun with it :upside_down_face:

I’m not sure if this is what you were trying to achieve or if it helps you achieve your goal at all but I had a bit of fun I suppose.

-- fxmanifest requires lua_54 yes
local glm_sincos = require 'glm'.sincos
local glm_rad = require 'glm'.rad
local math_abs = math.abs
RegisterCommand("LookAtClosestEntity", function()
  local closestPlayer, closestDistance = ESX.Game.GetClosestPlayer()
  if closestPlayer == -1 or closestDistance > 300.0 then
    ESX.ShowNotification('Nobody nearby to face', false, false, 0)
  else
    local target = GetPlayerPed(closestPlayer)
    TaskTurnPedToFaceEntity(PlayerPedId(), target, 2000)
    Wait(2500)
    local distTo = #(GetGameplayCamCoord()-GetEntityCoords(target))
    print(distTo)
    local offsetHeading = math.max(0, 5-distTo)
    SetGameplayCamRelativeHeading(offsetHeading*1.0)
    print(offsetHeading)
    local pitchTo = false
    for i = -60,60 do
      print(pitchTo)
      if pitchTo then break end
      print(i)
      SetGameplayCamRelativePitch(i*1.0, 1.0)
      local camCoords = GetGameplayCamCoord()
      local sin, cos = glm_sincos(glm_rad(GetGameplayCamRot(2)))
      local destination = camCoords + (vec3(-sin.z * math_abs(cos.x), cos.z * math_abs(cos.x), sin.x) * 100.0)
      local handle = StartShapeTestLosProbe(camCoords.x, camCoords.y, camCoords.z, destination.x, destination.y, destination.z, 511, PlayerPedId(), 4)
      while true do
        Wait(0)
        local retval, hit, endCoords, surfaceNormal, material, entityHit = GetShapeTestResultIncludingMaterial(handle)
        if retval ~= 1 then
          DrawMarker(2, endCoords.x, endCoords.y, endCoords.z, 0.0, 0.0, 0.0, 0.0, 180.0, 0.0, 0.8, 0.8, 0.8, 255, 128, 0, 50, false, true, 2, nil, nil, false)
          pitchTo = entityHit==target
          break
        end
      end
    end
    if pitchTo then print("Looking At Target With Gameplay Cam") end
  end
end)