Why the camera reset ?

Hello everyone, I’m a beginner, I’m developing a first person horror game, PT style with a loop between two doors

I must have spent more than 15 hours on my script, and it still bugs
I won’t be able to describe here all the modifications I made.

so I’m posting here one of the first versions of my script, which I really like, just one flaw, when the player enters the scene or after an animation when the script is read the first time after an activation, it resets the player’s sight, this breaks the immersion and forces the player to give several mouse strokes after a teleport for example to regain his sight

I tried to save the position, put *=, and many other modifications, but it always caused other problems, like blockages in the walls, crazy sensitivity, the camera which rotated by itself, the camera who teleported, the camera jerked, the camera which deviated from the trajectory in relation to the player, and so on

So here I am asking for your help, I’m really out of ideas, a big thank you to you for helping me dear coders

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class FirstPersonLook : MonoBehaviour
{
    [SerializeField]
    Transform character;
    public SettingsManager settingsManager;

    private float maxVerticalAngle = 40f;
    private float minVerticalAngle = -60f;

    Vector2 velocity;
    Vector2 frameVelocity;

    public GameObject menuPanel;
    public GameObject panelAnalyse;

    private float maxCameraOffset = 0.02f; // L'amplitude maximale du mouvement de la caméra
    private float cameraMotionSpeed = 1.33f; // La vitesse de mouvement de la caméra
    private Vector3 originalCameraPosition; // Position d'origine de la caméra
    private float currentCameraOffset = 0.0f; // L'amplitude actuelle du mouvement de la caméra

    public bool controlsActive = true;
    void Start()
    {
        Cursor.lockState = CursorLockMode.Locked; // Lock the mouse cursor to the game screen.

        originalCameraPosition = transform.localPosition; // Enregistrez la position d'origine de la caméra
    }

    void Update()
    {
        if (controlsActive && !menuPanel.activeSelf && !panelAnalyse.activeSelf)
        {
            float sensitivity = settingsManager.gameSettings.sensitivity; // Utilisez la sensibilité depuis le SettingsManager

            // Obtenir une vélocité fluide
            Vector2 mouseDelta = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y"));
            frameVelocity = Vector2.Lerp(frameVelocity, mouseDelta, 1); // Plus de lissage
            velocity += frameVelocity;

            // Rotation de la caméra vers le haut et le bas et du contrôleur vers la gauche et la droite en fonction de la vélocité
            transform.localRotation = Quaternion.AngleAxis(-velocity.y, Vector3.right);
            character.localRotation = Quaternion.AngleAxis(velocity.x, Vector3.up);

            // Limiter la vélocité verticale pour restreindre la plage de vision
            velocity.y = Mathf.Clamp(velocity.y, minVerticalAngle, maxVerticalAngle);

            // Ajouter un effet de respiration à la caméra
            currentCameraOffset = Mathf.Sin(Time.time * cameraMotionSpeed) * maxCameraOffset;
            Vector3 newCameraPosition = originalCameraPosition + Vector3.up * currentCameraOffset;
            transform.localPosition = newCameraPosition;
        }
    }
    public void DisableControls()
    {
        controlsActive = false;
    }

    public void EnableControls()
    {
        controlsActive = true;
    }
}