Change object Hierarchy in Animation

Hello, I was wondering if there is a way to change the hierarchy of an object in animation or if there is a special plugin or script required? I’m basically trying to attach my character’s rifle to the other hand when playing an animation. At the moment I have it set to hide the gun in the right hand and unhide the left one where her hand is holding the body of the rifle and the other pulls the bolt back and forth. Unfortunately the method I’m currently using is buggy in a way that my character will appear dual-wielding sometimes(I have both rifles unhidden for visual aid). This is for VRchat

I’m a bit of a novice at the animation engine myself - possibly there’s a way to do it via the animation engine, unfortunately I don’t know. What you could do is run a script which would handle the transition, and call that script as an event at the appropriate keyframe?

So, some quick-and-dirty code might be:

using UnityEngine;

public class GunSwitch : MonoBehaviour {

    GameObject rightHand, leftHand; 

    void Awake()
    {
        rightHand = GameObject.Find("rightHand");
        leftHand = GameObject.Find("leftHand"); 
    }
	
    public void SwitchToLeftHand()
    {
        gameObject.transform.parent = leftHand.transform; 
    }

    public void SwitchToRightHand()
    {
        gameObject.transform.parent = rightHand.transform; 
    }

}

And then just call the SwitchTo_______ methods at the appropriate points in your animations? You might have to do some additional logic to smooth the transition visually, or tune your animation so that the transition works smoothly, but I’d think something like this would work?