My script for a togglable toggle Aim down sight

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityStandardAssets.Characters.FirstPerson;

public class WeaponZoom : MonoBehaviour
{
    [SerializeField] Camera fpsCamera;

    [SerializeField] float zoomOutFOV = 60f;
    [SerializeField] float zoomInFOV = 35f;
    [SerializeField] float zoomOutSensitivity = 2f;
    [SerializeField] float zoomInSensitivity = 1f;
    [SerializeField] bool toggle = false;

    RigidbodyFirstPersonController fpsController;

    bool zoomedInToggle = false;

    void Start()
    {
        fpsController = GetComponent<RigidbodyFirstPersonController>();
    }

    void Update()
    {
        if (!toggle)
        {
            ChangeFov();
        } else
        {
            ChangeFovToggle();
        }
    }

    void ChangeFovToggle()
    {
        if (Input.GetMouseButtonDown(1))
        {
            if(!zoomedInToggle)
            {
                zoomedInToggle = true;
                ApplyFOV(zoomInFOV, zoomInSensitivity);
            }
            else
            {
                zoomedInToggle = false;
                ApplyFOV(zoomOutFOV, zoomOutSensitivity);
            }
        }

    } 
    
    void ChangeFov()
    {
        if (Input.GetMouseButton(1))
        {
            ApplyFOV(zoomInFOV, zoomInSensitivity);
        }
        else
        {
            ApplyFOV(zoomOutFOV, zoomOutSensitivity);
        }
    }

    void ApplyFOV(float zoomFov, float zoomSensitivity)
    {
        fpsCamera.fieldOfView = zoomFov;
        fpsController.mouseLook.XSensitivity = zoomSensitivity;
        fpsController.mouseLook.YSensitivity = zoomSensitivity;
    }
}

Privacy & Terms