Aimimg guns like in reciever

How do I aim my gun like in the game reciever. In receiver the gun when aimed moves independently of the camera inside a small deadzone when it reaches past that deadzone the camera rotates in the direction the gun is being moved.

I was able to get the gun to move within a deadzone but when it leaves the deadzone the gun does properly rotate with the camera. Instead the distance between the camera and gun is changed and the gun becomes unusable.

My code:

public class GunMouseLook : MonoBehaviour {

    public Transform gun,gunRest,gunAim;
    public float aimSpeed;
    public float XGunAimSensistivity = 3f;
    public float YGunAimSensistivity = 3F;

    public SimpleSmoothMouseLook ssml;

	public float xMaxDist;
    public float yMaxDist;
    public float zMaxDist;
    public float zMinDist;
    public float zInterval;
    public float maxDistanceFromCenter;
    public float maxDist;
    bool reachedTarget;

    // Update is called once per frame
    public void LateUpdate()

    public void NormalLook(){
        Cursor.lockState = CursorLockMode.Locked;
        gun.position = Vector3.Lerp(gun.position,gunRest.position,aimSpeed*Time.deltaTime);
        gun.rotation = Quaternion.Slerp(gun.rotation,gunRest.rotation,aimSpeed*Time.deltaTime);
        reachedTarget = false;

	public void GunLook(){

            } else{
                gun.position = Vector3.Lerp(gun.position,gunAim.position,aimSpeed*Time.deltaTime);
                gun.rotation = Quaternion.Slerp(gun.rotation,gunAim.rotation,aimSpeed*Time.deltaTime);
        Vector3 screenPointGun = Camera.main.WorldToScreenPoint(gun.position);
        float distanceFromCenter =  Vector3.Distance(screenPointGun,new Vector3(1/2,1/2,0));
        if(distanceFromCenter > maxDistanceFromCenter)
            ssml.enabled = true;
            float z = Mathf.Clamp(gun.position.z + zInterval * Input.mouseScrollDelta.y, zMinDist, zMaxDist);
            Vector2 mp = new Vector2(Input.GetAxisRaw("Mouse X"), Input.GetAxisRaw("Mouse Y")) * 0.1f;
            gun.position = (gun.position + new Vector3(mp.x, mp.y, 0));
            gun.rotation = transform.rotation;


example gif

The function below checks for a deadZone angle and rotates the transform if the angle is past:

public float deadZone = 30;
	void GunLookDeadZoneRotation() {
		// Get the angle
		float angleDiff = Vector3.Angle (transform.forward, gunAim.forward);
		float rotY = angleDiff.y;
		if (angleDiff < deadZone)
        transform.rotation = transform.localRotation * Quaternion.Euler(-rotY, 0f, 0f);