Search Unity

Inaccuracy for timed execution of code snippets - am I doing it wrong?

Discussion in 'Scripting' started by Zediron, Sep 29, 2012.

  1. Zediron

    Zediron

    Joined:
    Sep 27, 2012
    Posts:
    5
    Hello everyone!

    I am just starting to learn unity and gaming concepts. I am in the phase of trying to reimplement some of the tutorials' scripts, see how else I could do certain things and compare the output.

    Doing things independent of FPS, so to say timed, seems like one of the most basic requirement for a game. Eg: I would like to do something every second. In my case I would like my mash to rotate by 90 degrees every second:

    Code (csharp):
    1. void Update () {                              
    2.                 timer -= Time.deltaTime;
    3.                 if(timer <= 0.0f){
    4.                         timer = 1.0f;
    5.                         Debug.Log( transform.rotation.eulerAngles.z );
    6.                 }
    7.                 transform.Rotate(0,0, Time.deltaTime *  90, Space.Self);
    8.         }
    Problem: It is inaccurate quite a bit. I modified this script to see how many rotations my object does in 60 seconds and compare that to the expected result and the output was quite significantly different.

    Any input on how to make it more accurate are much appreciated.
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401