mathf.movetowards not returning right values

maybe the title doesnt make sense, idk, I’m pretty bad at this stuff

so in my game, I’m trying to add recoil to my guns by moving the camera up and to the side. To do this, I have some function called AddRecoil in my shooting script that calls to my camera script. it looks kinda like this:
public void AddRecoil(Quaternion recoil) { rotX += Mathf.MoveTowards(0, recoil.x, recoilSpeed * Time.deltaTime); rotY += Mathf.MoveTowards(0, recoil.y, recoilSpeed * Time.deltaTime); }

however, when this actually happens, the camera doesn’t go up the amount recoil.x or recoil.y are. I think this is because the function is only called once, and thus only one value is returned before the function stops, but I’m not sure. How would I fix this?

1) The Mathf.MoveTowards() function must be called every frame to obtain smooth movement (if this function is called once, then only one value will be received).

|

2) The first parameter of the Mathf.MoveTowards() function cannot be a constant, otherwise each time the count will shift to this number and, as a result, the function will return almost the same value.

|

3) For more convenient work with Quaternion, you can use q.eulerAngles.x (.y or .z) (where q is the Quaternion) to get the angle values ​​in degrees, and for the assignment:

Quaternion q = Quaternion.Euler (xRot, yRot, zRot);

|

4) Most of the types created by Unity have their own functions that allow you to smoothly change one value of a variable to another. In your case, it is better to use Quaternion.Lerp() instead of Mathf.MoveTowards().
_
To summarize all of the above, I propose the following script (on the weapon’s GameObject):

using UnityEngine;

public class YourScript: MonoBehaviour
{
    bool isShooting;
    float recoilSpeed = 25;
    Quaternion shootingRotation, startRotation;

    // assign your rotation when shooting in the inspector
    // this value means how far the weapon can deviate when firing
    [SerializeField] Vector3 shootingRotationInEulerAngles = new Vector3(15, 10);

    void Start()
    {
        shootingRotation = Quaternion.Euler(shootingRotationInEulerAngles);
        startRotation = transform.rotation;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            isShooting = true;
        }

        Quaternion rot = transform.rotation;

        if (rot == startRotation * shootingRotation)
        {
            isShooting = false;
        }

        if (isShooting)
        {
            //Here it is necessary not to add, but to assign
            transform.rotation = Quaternion.Lerp(rot, startRotation * shootingRotation, Time.deltaTime * recoilSpeed);
        }
        else
        {
            //Here it is necessary not to add, but to assign
            transform.rotation = Quaternion.Lerp(rot, startRotation, Time.deltaTime * recoilSpeed / 5f);
            //I used "/ 5f" because returning the weapon to its place
            //should be slower than recoil (which is logical for me).
        }
    }
}