Rotate my camera towards a ped

Hi! I am writing a character customization script, and my camera glitches way too often. I mean it should be looking straight towards the ped I am customizing, but sometimes it looks completely in a different direction, sometimes it’s looking towards the ground, and etc. I dont know how can I create a camera and set it towards the player, so I made my player’s camera first person and set the player right in front of the ped I am customizing, but bad rotations happen. Can someone please help me?

My script:

on("showCharacterCustomizer", (id, charName, playerID) =>
{
    FreezePedCameraRotation(PlayerPedId());
    DoScreenFadeIn(4000);
    DisplayRadar(false);
    SetFollowPedCamViewMode(4);

    var model = GetHashKey("mp_f_freemode_01");
    SetEntityVisible(PlayerPedId(), false);

    RequestModel(model);
    RequestCollisionAtCoord(pedX, pedY, pedZ);
    RequestCollisionAtCoord(pX, pY, pZ);

    var tick = setTick(() =>
    {
        if (HasModelLoaded(model))
        {
            ped = CreatePed(
                2,
                GetHashKey("mp_f_freemode_01"),
                pedX, pedY, pedZ,
                -160,
                false,
                true
            );

            SetPedHeadBlendData(
                ped,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                0,
                false
            );

            FreezeEntityPosition(ped, true);
            FreezeEntityPosition(PlayerPedId(), true);

            SetEntityCoords(PlayerPedId(), pX, pY, pZ);

            //Set the player facing the ped, but sometimes it's looking in a different
            //direction.
            SetEntityHeading(PlayerPedId(), 40);
                
            SetNuiFocus(true, true);
            SendNuiMessage(JSON.stringify({
                type: "character_creator", visible: true, accountID: id,
                charName: charName, playerID: playerID
            }));
            clearTick(tick);
        }
    })
});
1 Like

Here is the code I’m using to create a camera and make it point at the head of the player. It’s LUA but I’m sure it’ll help you.

function CreationCamHead()
	cam = CreateCam('DEFAULT_SCRIPTED_CAMERA')

	local coordsCam = GetOffsetFromEntityInWorldCoords(PlayerPedId(), 0.0, 0.5, 0.65)
	local coordsPly = GetEntityCoords(PlayerPedId())
	SetCamCoord(cam, coordsCam)
	PointCamAtCoord(cam, coordsPly['x'], coordsPly['y'], coordsPly['z']+0.65)

	SetCamActive(cam, true)
	RenderScriptCams(true, true, 500, true, true)
end
3 Likes

Thank you! :slight_smile: My error in the script was I had RenderScriptCams on false. :smiley: