Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020 LTS & Unity 2021.1 have been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Problem finding time from normalized time

Discussion in 'Animation' started by goksu_yamac, Apr 6, 2021.

  1. goksu_yamac

    goksu_yamac

    Joined:
    Feb 12, 2017
    Posts:
    2
    Hi folks!

    I'm developing a scene for the purpose of running a perception experiment that requires very precise timing of some animations - down to a precision of milliseconds. I've been scrutinizing the animation system to figure out the most convenient way and I've encountered a problem that I couldn't understand. I'm hoping that maybe someone here could have an answer or a suggestion. I'd appreciate any kind of help.

    I've got a 30fps fbx human animation of 51 frames length. I've got the fps of the application set to 120fps. I'm trying to do some kind of a sanity check by converting normalized time into time and printing it per frame. Simultaneously, I keep track of the time passed in the animation clip by adding up deltaTime per frame. My expectation was to find that these quantities are the same. However, that was not the case. The two quantities are off by 0.15ms per frame and the difference accumulates. Below is the code snippet that I'm using.

    Is there a problem with my method? Is it wrong to assume that normalized time multiplied by clip size in frames and divided by clip frame rate would give the time?

    Code (CSharp):
    1.     int anim_size = 51; // animation size in frames
    2.     int anim_counter = 0;
    3.     float time_passed = 0;
    4.     bool once = true;
    5.  
    6.     void TimeConversion(float nt) {
    7.        // converts normalized time to time
    8.        
    9.         float normal_time = nt * anim_size / 30;  // time in secs
    10.         float normal_frame = nt * anim_size * 4;    // time in frames (120 fps);
    11.  
    12.         Debug.Log("Animation frame(120fps) is " + normal_frame + ", animation time is " + normal_time + ", time passed is " + time_passed);
    13.         anim_counter = anim_counter + 1;
    14.     }
    15.  
    16.     private void Awake()
    17.     {
    18.         QualitySettings.vSyncCount = 0;
    19.         Application.targetFrameRate = 120;
    20.         m_Animator = this.GetComponent<Animator>();
    21.     }
    22.  
    23.     void Update() {
    24.  
    25.         if (Input.GetKeyDown(KeyCode.P))
    26.         {
    27.             m_Animator.Play("throw0_2");
    28.             anim_bool = true;
    29.         }
    30.  
    31.         m_CurrentClipInfo = m_Animator.GetCurrentAnimatorClipInfo(0);
    32.  
    33.         if (m_CurrentClipInfo.Length != 0 && once == true)
    34.         {
    35.             curr_clip_name = m_CurrentClipInfo[0].clip.name;
    36.             time_passed = 0;
    37.             once = false;
    38.         }
    39.  
    40.         if (curr_clip_name == "throw0_2") {
    41.             anim_state_info = m_Animator.GetCurrentAnimatorStateInfo(0);
    42.             TimeConversion(anim_state_info.normalizedTime);
    43.         }
    44.  
    45.         time_passed += Time.deltaTime;
    46.     }


    Thanks for any help,
    Goksu
     
unityunity