Camera rotation follows entity that is attached to

Hi everyone,

Is there a way to attach the camera so that it follows the position and rotation of the entity that it is attached to ?
I found this native but it doesn’t make the camera follow the rotation of the entity :

-- https://docs.fivem.net/natives/?_0xFEDB7D269E8C60E3
-- ATTACH_CAM_TO_ENTITY
AttachCamToEntity(
	cam --[[ Cam ]], 
	entity --[[ Entity ]], 
	xOffset --[[ number ]], 
	yOffset --[[ number ]], 
	zOffset --[[ number ]], 
	isRelative --[[ boolean ]]
)

A workaround I found was to do this code in a loop but it’s not ideal, because I’d want to apply an offset to the rotation (having the camera 10° above the xy plan for instance) and euler angles create singularities :

obj_rot = GetEntityRotation(obj,2)
SetCamRot(cam, obj_rot, 2)

I think this native would be a better fit but it’s only for vehicle :

-- https://docs.fivem.net/natives/?_0x8DB3F12A02CAEF72
-- _ATTACH_CAM_TO_VEHICLE_BONE
AttachCamToVehicleBone(
	cam --[[ Cam ]], 
	vehicle --[[ Vehicle ]], 
	boneIndex --[[ integer ]], 
	relativeRotation --[[ boolean ]], 
	rotX --[[ number ]], 
	rotY --[[ number ]], 
	rotZ --[[ number ]], 
	offX --[[ number ]], 
	offY --[[ number ]], 
	offZ --[[ number ]], 
	fixedDirection --[[ boolean ]]
)

Any help would be appreciated ! Thank you

This sounds like what you’re after.

Hey, thank you for the answer but my camera is gonna be attached to an entity, not a ped so I’m not sure it’ll work

And I don’t think this native fixs the rotation between the ped and the camera

A ped is an entity. It’s no different.

1 Like

Yes but it’s not what I’m looking for anyway

I’m trying to get a camera to have fixed position and rotation on an object, sort of like a dashcam for instance

But that would be on an object instead of a vehicle

An object is also an entity lol.

If you’re attaching to a vehicle then why can’t you use _ATTACH_CAM_TO_VEHICLE_BONE as you mentioned yourself before?

Can you describe exactly what you’re trying to do, and preferably show the code you have so far as well. That will make our lives easier trying to help you.

1 Like

Thank you for helping but please read carefully…

I never said I wanted to attach the camera to a vehicle nor did I say that an object is not an entity

I said that I wanted to attach a camera to an object so that the camera is attached to the object (that part is already done) and I also want the camera to have a rotation relative to the rotation of the object (that part I’m struggling)

I’ll post some code tonight

Here’s the code I currently have

local cam, obj

Citizen.CreateThread(function()
  local pos = GetEntityCoords(PlayerPedId()) + vector3(0,0,2)
  local hash = GetHashKey("prop_pap_camera_01")

  RequestModel(hash)
  while not HasModelLoaded(hash) do
    Citizen.Wait(100)
  end

  obj = CreateObject(hash, pos, true, true, true)
  Citizen.CreateThread(function()
    while true do
      Citizen.Wait(1)
      SetEntityCoords(obj, pos)
    end
  end)

  cam = CreateCam("DEFAULT_SCRIPTED_CAMERA", 1)
  SetCamCoord(cam, spawn)

  -- Solution 1 ? Doesn't follow rotation of object :
  -- AttachCamToEntity(cam, obj, vector3(0,0,0), true)

  -- Solution 2 ? Doesn't work as obj is not a vehicle :
  -- local bone = GetEntityBoneIndexByName(obj, "chassis")
  -- AttachCamToVehicleBone(cam, obj, bone, true, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, true)

  -- Solution 3 ? But can't apply offset to rotation :
  AttachCamToEntity(cam, obj, vector3(0,0,0), true)
  Citizen.CreateThread(function()
    while true do
      Citizen.Wait(1)
      local obj_rot = GetEntityRotation(obj, 2)
      obj_rot = obj_rot + vector3(20.0, 0.0, 0.0) -- create some issues (works fine without it)
      SetCamRot(cam, obj_rot, 2)
    end
  end)

  SetEntityAngularVelocity(obj, 0.5, 0.0, 0.0)
  RenderScriptCams(1, 0, 0, 1, 1)
end)

-- Delete everything when script stops
AddEventHandler("onResourceStop", function(resource)
  if (GetCurrentResourceName() ~= resource) then
    return
  end
  DeleteObject(obj)
  DestroyCam(cam, 0)
  RenderScriptCams(0, 0, 1, 1, 1)
end)

No need to get your knickers in a twist, that’ll just discourage people from replying altogether.

A few things:

  • Firstly you set pos to 2 units above the ped’s head once at the start of the script, but then never update that value, which results in nothing moving, I can’t tell if that’s what you intended to do or not, but I removed it since it didn’t make much sense to me.
  • Secondly, you said you want the script camera to follow the rotation of the prop camera, but the prop camera does not move, so I was a bit lost on how you were saying solutions were not working. For the purposes of the code below, I made it so the camera prop followed the ped rotation, and then the script camera follows the prop camera rotation. If you have a different source rotation for the camera prop you can remove that line, I labeled it for you.

The code below does the following:

  1. Create a camera prop above the ped
  2. Updates the camera prop’s position and rotation to match that of the ped (+2 units above)
  3. Creates a script camera
  4. Attaches the script camera to the camera prop
  5. Updates the script camera’s rotation to match that of the ped
local cam, obj

Citizen.CreateThread(function()
    local hash = GetHashKey("prop_pap_camera_01")

    RequestModel(hash)
    while not HasModelLoaded(hash) do
        Citizen.Wait(100)
    end

    -- Spawn below the player then set in place
    obj = CreateObject(hash, GetEntityCoords(PlayerPedId()) - vector3(0, 0, -50), true, true, true)

    Citizen.CreateThread(function()
        while true do
            local ped = PlayerPedId()

            -- You could also attach the prop to the ped, if that is your end goal
            SetEntityCoords(obj, GetEntityCoords(ped) + vector3(0,0,2))
            -- You can remove this if you do not want the prop to follow the player rotation
            SetEntityRotation(obj, GetEntityRotation(ped, 2) + vector3(0, 0, 180), 2, true)
            -- Uncomment this if you want the camera to move with the mouse
            --SetEntityRotation(obj, GetGameplayCamRot(2), 2, true)

            Citizen.Wait(1)
        end
    end)

    cam = CreateCam("DEFAULT_SCRIPTED_CAMERA", 1)

    AttachCamToEntity(cam, obj, vector3(0,0,0), true)

    Citizen.CreateThread(function()
        while true do
            local obj_rot = GetEntityRotation(obj, 2)
            -- This points the camera upwards, unclear if this is intended or not
            obj_rot = obj_rot + vector3(20.0, 0.0, 180)
            SetCamRot(cam, obj_rot, 2)

            Citizen.Wait(1)
        end
    end)

    RenderScriptCams(1, 0, 0, 1, 1)
end)

-- Delete everything when script stops
AddEventHandler("onResourceStop", function(resource)
    if (GetCurrentResourceName() ~= resource) then
        return
    end

    DeleteObject(obj)
    DestroyCam(cam, 0)
    RenderScriptCams(0, 0, 1, 1, 1)
end)

Removed a few things:

  • local pos, for the reasons I explained above.
  • SetCamCoord(cam, spawn), spawn was not defined, and if you attach the camera to the prop then you don’t need to update the position, only the rotation.
  • SetEntityAngularVelocity, no idea what you were doing with this.

Hopefully that’s what you’re after, or at least points you in the direction of what you’re after, because even after “reading carefully” and looking at the code you posted, I’m still not clear on what you’re trying to do.

Hi, this was just a sample code to demonstrate the problem I’m facing, no need to correct it

The problem is with the rotation, therefor the prop doesn’t have to move to understand the issue

The prop was already rotating thanks to the native SetAngularVelocity, look it up or execute my code and you’ll understand what I’m talking about

Back where we started, the issue is, as the camera is 20° above the pitch of the prop, there’s some problems when the pitch of the prop is in the [70°; 90°] range (because the camera pitch is in the [90°; 110°] range and the SetCamRot does some conversions that I do not understand in this case)

This problem appears with the three axis of rotation when I apply an offset

:man_facepalming:

The “sample code” you provided did very little except spawn a static prop (i.e. did not move at all) above the player, and attach a camera to said static prop, which by extension also did not move.

Since you seem unable or unwilling to provide what I’ve asked for, and you continue to use that lovely tone in your messages, I’m going to stop replying now. Best of luck with your problem, good day.

I’ll too stop responding to you but you should execute the provided code just to understand what I wanted to do. You’ll see that the prop isn’t static

nice code brother