How To Keep My Weapon With My Camera?

Hello,

I’ve just run into a problem. My gun does not stay with my camera when it begins to recoil, and after the methods being to run, it “Lerps” back to where it is supposed to. Here is what issue I am experiencing:

Here are the methods, and I have properly assigned my weapon transform object to my weapon as well.

 IEnumerator MoveToPosition(Vector3 newPosition, float time)
    {
        float elapsedTime = 0;
        Vector3 startingPos = weapon.transform.position;

        while (elapsedTime < time)
        {
            weapon.transform.position = Vector3.Lerp(startingPos, newPosition, (elapsedTime / time));
            elapsedTime += Time.deltaTime;
            yield return null;
        }
    }

    void recoilBack()
    {
        Vector3 zoomOutOffset = new Vector3(-0.01f, 0, 0);

        if (zoomOutOffset.x >= 0.05f)
        {
            zoomOutOffset.x = 0.05f;
        }

        var zoomOutWorldPosition = weapon.transform.TransformDirection(zoomOutOffset);

        StartCoroutine(MoveToPosition(weapon.transform.position + zoomOutWorldPosition, recoilSpeed));

        
    }

    void recoilForward()
    {
        Vector3 zoomInOffset = new Vector3(0.01f, 0, 0);


        if (zoomInOffset.x >= 0.05f)
        {
            zoomInOffset.x = 0.05f;
        }

        var zoomInWorldPosition = weapon.transform.TransformDirection(zoomInOffset);

        StartCoroutine(MoveToPosition(weapon.transform.position + zoomInWorldPosition, recoilSpeed));

    }

Make sure the weapon is child of the camera. Also if you have animated the weapon via Unity it probably uses the global positions so it’s most likely it will return to the position your firstly animated. I would recommend animating the weapon in a the software you made the weapon so it is attached to the character and play it no matter where you are in the world.

No luck yet, finally fixed some of my sounds in the meantime… Any suggestions as to how I can get the gun to continue to recoil and move along with my player/camera? The methods I supplied are called when the IEnumerator Coroutines are run. Thanks for your help guys, please let me know if you have a solution!