Animation Clobbers Position

So I have an object in my world that has its location based on a tagged object in my world. code:

var objectToFollow;

var onM = true;
var on1 = false;
var on2 = false;
var on3 = false;

function Update () 
{
    if(gameObject.Find("localPlayer")==null)
    {}
    else
    {
       //animation code omitted 
       objectToFollow = GameObject.FindWithTag("Player").transform;
        transform.position = objectToFollow.TransformDirection(Vector3.forward * 4) + objectToFollow.position;
		transform.localPosition.y += 2;
        transform.rotation = objectToFollow.rotation * Quaternion.Euler(0,-90,90);
    }
}

This positions the object in front of my player. I have animations that play when I push the specified buttons. The problem is that when I do the animation the position of the object completely changes and doesn’t stay in front of the player. The code for the animations fall where the code above says it was omitted.

//code from animation

if(onM)
{
	if(Input.GetKeyDown("o"))
	{
		animation.Play("m-1");
		onM = false;
		on1 = true;
	}
	else if(Input.GetKeyDown("l"))
	{
		animation.Play("m-3");
		onM = false;
		on3 = true;
	}
}
else if(on1)
{
	if(Input.GetKeyDown("o"))
	{
		animation.Play("1-2");
		on1 = false;
		on2 = true;
	}
	else if(Input.GetKeyDown("l"))
	{
		animation.Play("1-m");
		on1 = false;
		onM = true;
	}
}
else if(on2)
{
	if(Input.GetKeyDown("o"))
	{
		animation.Play("2-3");
		on2 = false;
		on3 = true;
	}
	else if(Input.GetKeyDown("l"))
	{
		animation.Play("2-1");
		on2 = false;
		on1 = true;
	}
}
else if(on3)
{
	if(Input.GetKeyDown("o"))
	{
		animation.Play("3-m");
		on3 = false;
		onM = true;
	}
	else if(Input.GetKeyDown("l"))
	{
		animation.Play("3-2");
		on3 = false;
		on2 = true;
	}
}

When this object is parented to the player rather than transformed based on the tag the objects animation works correctly, however that’s not what I want to do due to other factors. Any ideas?

I think the problem is that I am attaching the object to the Player via the parent of the gameobject. At the same time the animation that is running is on the parent of the gameobject.

To solve this I am currently making a parent object that is only holding the attachment code with no animations. Then a child of that will hold all of the animations, and the child of that will hold the onclick functions that say when interact with object call parent and do animation. This will only work if my assumption is correct that transform.parent only calls the direct parent and not the overarching parent. I will let you know.

It is hard to say without seeing it, but it sounds as if the animation is moving the root of the object in question, probably to some location around (0,0,0). So when it is not attached to the player, it warps to near the origin; when it is attached, it is relative to the player. I’d suggest attaching the animating object in question to a new, dummy object, and then use your code above to position that.