x


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?

more ▼

asked Jun 23, 2011 at 08:09 PM

zachypin gravatar image

zachypin
121 98 75 75

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)Menu. 2) Main 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.

Jun 23, 2011 at 08:20 PM zachypin
(comments are locked)
10|3000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Jun 27, 2011 at 03:46 PM

zachypin gravatar image

zachypin
121 98 75 75

worked :)

Jun 27, 2011 at 05:05 PM zachypin
(comments are locked)
10|3000 characters needed characters left

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.

more ▼

answered Jun 24, 2011 at 01:30 AM

Molix gravatar image

Molix
5.4k 41 52 101

Yes, that is what it sounds like. Good luck.

Jun 27, 2011 at 03:55 PM Molix
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x6946
x3695
x2280
x1604

asked: Jun 23, 2011 at 08:09 PM

Seen: 1461 times

Last Updated: Jun 27, 2011 at 05:05 PM