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 