- Home /
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?
Also. this code is setup on the parent because that is where the animation occurs. This is on a menu type gameObejct. So the hierarchy is like 1)$$anonymous$$enu. 2) $$anonymous$$ain 2)option1 2)option2 2)option3 2) buttons 3)downbutton 3)upbutton 1 is the parent. 2 is under menu. 3 is under buttons. Not sure if it matters that the animations being played are on the parent item.
Answer by zachypin · Jun 27, 2011 at 03:46 PM
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.
Answer by Molix · Jun 24, 2011 at 01:30 AM
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.
Your answer
Follow this Question
Related Questions
In animations, is it possible to only add transform values instead of setting it? 2 Answers
Getting imported animated object position 0 Answers
Play animation as the value changes from 0 to 1 with changing GameObject's position in unity 0 Answers
Parts of imported blender object are misplaced (when starting game) 0 Answers
transform.position error 1 Answer