Unity 5 FPS crouch camera is pushed upwards by the controller[SOLVED!]

Hello everyone. For some reason, my script that smoothly resizes the fps controller height and lerps the camera position does not work as intended. Every time my player stands back up, the camera is gradually pushed upwards giving a very glitchy motion. I looked through scripts and forums, tried a multitude of combinations on my own, but I am missing something. Why is the camera pushed upwards bit by bit? If anyone knows that the problem might be, I’d appreciate an answer. I am clearly missing something very easy but I still can’t find it and it’s extremely frustrating as crouching is an integral part of my game.Here is my script:

public class Crouch : MonoBehaviour
{
    public CharacterController MyChar;
    public Camera MyCam;

    public float CrouchWalkSpeed;
    public float CrouchHeight = 1.0f;

    public float SmoothTime;

    private float DefCamY;

    private void Start()
    {
        DefCamY = MyCam.transform.position.y;
    }

    private void Update()
    {
        if (Input.GetKey(KeyCode.LeftControl))
        {
            MyChar.height = Mathf.Lerp(MyChar.height, CrouchHeight, Time.deltaTime * SmoothTime);
            var posY = MyCam.transform.position.y;
            posY = Mathf.Lerp(DefCamY, CrouchHeight, Time.deltaTime * SmoothTime);
        }
        else
        {
            MyChar.height = Mathf.Lerp(MyChar.height, 1.8f, Time.deltaTime * SmoothTime);
            var posY = MyCam.transform.position.y;
            posY = Mathf.Lerp(MyCam.transform.position.y, DefCamY, Time.deltaTime * SmoothTime);

        }
    }


}

EDIT: After a few hours of frustration, looking through the way Unity’s fps controller works, I have come up with a new script. This one Does not move the camera, as it conflicts with the camera position implemented in the FPS controller. The fps controller script always tries to keep the camera at head level, which is about height(in our case 1.8) divided by 2 ===> 1.8/2=0.9 . What my new script does is as follows:

  1. Lerp the char controller height
  2. Lerp the char controller center
  3. If the player is not crouching, set the Y position of the game object to height/2.

results and problems: A smooth crouch and stand up, however the head bob(if used) can be glitchy(will need further implementation or skipping it’s usage which I will do anyway:)
Jumping is not working unless we implement the position to not be forced to the ground(which is quite easy tbh:) )

Here is my new script, anyone feel free to use it if you might need it. I will follow up with an improved crouch system soon and post it for everyone once I have worked it out.

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

public class Crouch : MonoBehaviour
{
    public GameObject PlayerObject;

    public CharacterController MyChar;
    public Camera MyCam;

    public float CrouchWalkSpeed;
    public float CrouchHeight = 1.0f;

    public float SmoothTime;

    private float DefCamY;

    private float centY;
    public bool isCrouching = false;

    private void Start()
    {
        DefCamY = MyCam.transform.position.y;
    }

    private void Update()
    {
         var centY= MyChar.center.y; //Always update centY so it corresponds to the current Y coordonate of the character controller height


        if (Input.GetKey(KeyCode.LeftControl))
        {
            MyChar.height = Mathf.Lerp(MyChar.height, CrouchHeight, Time.deltaTime/ SmoothTime);

            centY = Mathf.Lerp(centY, CrouchHeight, Time.deltaTime/SmoothTime);
            MyChar.center = new Vector3(0, centY, 0);

            if (MyChar.height != 1.8f)
            {
                isCrouching = true;
            }
        }
        else
        {
            isCrouching = false;

        }
        if (!isCrouching)
        {
            MyChar.height = Mathf.Lerp(MyChar.height, 1.8f, Time.deltaTime/SmoothTime);
            centY = Mathf.Lerp(centY, 0f, Time.deltaTime * SmoothTime);
            MyChar.center = new Vector3(0, centY, 0);
            
                PlayerObject.transform.position = new Vector3(PlayerObject.transform.position.x, MyChar.height/2, PlayerObject.transform.position.z);
 
        }
    }


}

You’re missing shifting var posY outside both ifs.