Search Unity

Translating object for fixed distance within part of animation

Discussion in 'Animation' started by BMYU, Feb 22, 2017.

  1. BMYU

    BMYU

    Joined:
    Jun 17, 2014
    Posts:
    11
    What I want to do
    1.JPG
    There's an animation clip which has 50 frames.
    while playing, especially from 10 frame to 30 frame, I want to translate Z position of its object for fixed distance.

    My Solution
    Use Animation event
    Let's assume onBackDashStart() and onBackDashEnd() will be called at each 10 frame and 30 frame.
    It should be like this.
    Code (CSharp):
    1.  
    2.  
    3.     IEnumerator onBackDashStart()
    4.     {
    5.         this.animating = true;
    6.         const float DIST = -3.0f;
    7.         const float FRAMES = 20;
    8.         while (this.animating)
    9.         {
    10.             Debug.Log("HELLO");
    11.             float trans = DIST / FRAMES;
    12.             this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, this.transform.position.z + trans);
    13.             yield return new WaitForFixedUpdate();
    14.         }
    15.     }
    16.  
    17.     void onBackDashEnd()
    18.     {
    19.         this.animating = false;
    20.     }
    Problem
    The event may not be fired on exact target frame(10 frame and 30 frame) since some frames can be skipped by a lag. (like Update() method)

    Question
    How can I always translate its position for fixed distance?
    Can it be implemented with Rigidbody.velocity ?