Weapon Sway interrupting Animations

Hello all, I have a weapon sway script that interrupts my animations on my gun due to it constantly setting the position in the LateUpdate function. I was wondering if there happened to be any work arounds.

Here is my script:

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

public class WeaponBob : MonoBehaviour
{

    //Courtesy of Redditor ChromakeyDreamcoat
    [SerializeField]
    private float bobSpeed = 1f;
    [SerializeField]
    private float bobDistance = 1f;

    private float horizontal, vertical, timer, waveSlice;
    private Vector3 midPoint;

    void Start()
    {
        midPoint = transform.localPosition;
    }

    void LateUpdate()
    {
        if (GetComponent<WeaponScript>().walking)
        {
            horizontal = Input.GetAxis("Horizontal");
            vertical = Input.GetAxis("Vertical");

            Vector3 localPosition = transform.localPosition;

            if (Mathf.Abs(horizontal) == 0 && Mathf.Abs(vertical) == 0)
            {
                timer = 0.0f;
            }
            else
            {
                waveSlice = Mathf.Sin(timer);
                timer = timer + bobSpeed;
                if (timer > Mathf.PI * 2)
                {
                    timer = timer - (Mathf.PI * 2);
                }
            }
            if (waveSlice != 0)
            {
                float translateChange = waveSlice * bobDistance;
                float totalAxes = Mathf.Abs(horizontal) + Mathf.Abs(vertical);
                totalAxes = Mathf.Clamp(totalAxes, 0.0f, 1.0f);
                translateChange = totalAxes * translateChange;
                localPosition.y = midPoint.y + translateChange;
                localPosition.x = midPoint.x + translateChange * 2;
            }
            else
            {
                localPosition.y = midPoint.y;
                localPosition.x = midPoint.x;
            }

            transform.localPosition = localPosition;
        }
    }
}

Edit: I’ve realised I’ve forgotten to mention the walking Boolean in my weapon script. That simply checks if “w” is pressed

Quick Edit: I know my name is stupid, I made my Unity account a few years ago when I was a big Minecraft nerd.

I’ve found a solution for this problem, simply make an empty game object, child the weapon to that object, have that object handle all the animations, and put the weapon bob script on the actual gun object. Credit to ktyl.dev on the Taira Games discord (now Game Dev 101) Discord