Set animation position proprieties with code

I have an axe chop tree animation with 4 keys based on position and rotation(axe the child of the player). When the player isn’t near a tree the position proprieties are constant(relative to player). When the player is near a tree, the animation doesn’t stops until the axe reaches the position proprieties and that causes a glitch and the axe is moving through tree!
I tried to check if player is near tree like this:

private void OnTriggerEnter(Collider other)
    {
        if(other.transform.gameObject.tag == "Tree")
        {
            nearTree = true;
            Tree = other.transform.gameObject;
        }
    }
    private void OnTriggerExit(Collider other)
    {
        if (other.transform.gameObject.tag == "Tree")
        {
            nearTree = false;
        }
    }
void Update(){

if(nearTree==false){
animation.play(hatchet); //the animation starts with constant values
}
if(nearTree==true){
//I want to change the animation position at an exact frame and replace the x, y, z values with Tree.transform.position.x, Tree.transform.position.y, Tree.transform.position.z, so the animation should stop when the axe reaches tree position
}
}

It checks correctly if the player is near tree!
I didn’t find how to change animation keys on the web!

You can’t change animation keys in runtime, only in Editor scripts. What you want is called Inverse Kinematics (IK). I don’t think unity embedded IK solution is capable of this nor I don’t know if your player has special bone for the axe. So first check out embedded solution and then look into assets store for commercial/free ones.

Also it may be you want to add collider to your axe and employ another clip when the axe touches the three and maybe put in some particles.

[QUOTE="Also it may be you want to add collider to your axe and employ another clip when the axe touches the three and maybe put in some particles.[/QUOTE]

That means to check if the hatchet is colliding with a tree and if it is true to stop the animation? I tried this but I don’t know how to handle this because I have 3 animations:
axeidle that’s play anytime, axeidleneartree that’s play when the player is near the tree but doesn’t hit the tree and axehit! And I created a code to verify if the player spams the click button, if the interval between clicks is more than 0.5sec, the choptree paramters is deactivated and the transition between axehit and axeidleneartree is played:

void Update () {
        if (nearTree == true)
        {
            if (Input.GetButtonDown("Fire1"))
            {
            
             
            
                animator.Play("hatchet");
               
                numberOfClicksInARow++;
                timeLastClicked = Time.time;
            }
        
            if (Time.time > timeLastClicked + maxTimeInterval) animator.SetBool("ChopTree", false);
            if (Time.time < timeLastClicked + maxTimeInterval) animator.SetBool("ChopTree", true);
         
        }


    }

And my chopping animation has 4 keyframes:
initial, the final axe position, a pause and back to initial

And if the axe hits the tree the animation should be skipped the “back to initial” keyframe!
But I can’t figure out all these thing, can you give me an example?

This is not correct. When hit, add particle effect to hide from players eye minor mesh intersections and add another after-hit animation clip where your character pulls axe back

ok but if the player stays in an worng position the axe glitches totally! look at this screen capture:


I don’t think that some particles can hide this because the axe is on the other side of tree!