Search Unity

Simple animation issue

Discussion in 'Animation' started by j0hn_, Dec 5, 2013.

  1. j0hn_

    j0hn_

    Joined:
    May 23, 2013
    Posts:
    5
    So I have an airplane, which is a child of an empty object. Click a button, airplane flies forward and stops. Click button again, the airplane flashes back to it's starting point to begin the animation again. I want the plane to keep flying along from it's current position,
    so I need to sync the parent's transform with the plane's current location at the end of the animation, right?

    I tried:

    parent.transform.position = this.transform.position;

    Seems to put the parent in correct location, but then the child maintains it's relative position and jumps forward.
    Someone suggested...

    transform.localPosition = Vector3.zero;

    ...Immediately after, but that didn't help either.

    I've done a lot of searching but couldn't find an answer, so any hints would be appreciated.
     
  2. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,859
    No, I wouldn't think so. Let's back up. What's causing the plane to fly forward? What code runs when you click the button?
     
  3. j0hn_

    j0hn_

    Joined:
    May 23, 2013
    Posts:
    5
    Clicking the button just calls the animation.Play function for the plane.
    The animation itself is just a simple animation I created in the animation widow.
     
  4. j0hn_

    j0hn_

    Joined:
    May 23, 2013
    Posts:
    5
    So with some help I got the position correctly updating.
    Code (csharp):
    1.    
    2.  void Start()
    3.     {
    4.        relRot = this.transform.rotation * Quaternion.Inverse(parent.transform.rotation);
    5.     }
    6.  public void updateParent()
    7.  {  
    8.         parent.transform.position = this.transform.position;
    9.         transform.localPosition = Vector3.zero;
    10.         parent.transform.rotation = relRot * this.transform.rotation;
    11.         animation.Play("idle");
    12. }
    13.  
    Got it working with the above.
     
    Last edited: Dec 6, 2013