Search Unity

Simple Timer resetting

Discussion in 'Scripting' started by yahadi, Aug 31, 2009.

  1. yahadi

    yahadi

    Joined:
    Jun 28, 2009
    Posts:
    36
    I am calculating the time the player is spending falling from a high location.

    I used:

    Code (csharp):
    1.  
    2. function Update ()
    3. {
    4.     if (!playerController.IsGrounded()  Time.time>airtimer)
    5.     {
    6.        
    7.        
    8.         airtimer=Time.time+1;
    9.         print (airtimer);
    10.     }
    11. }
    12.  
    And when the player collides with the ground I reset airtimer=0.

    The problem is that when I fall the second time airtimer variable doesn't start from 0 like it should but form Time.time which is the amount of time passed since the game started. Is there an alternative way of doing this?

    I tried using Time.deltaTime instead but the variable stopped incrementing beyond 1. [/code]
     
  2. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    you need 2 more variables


    Code (csharp):
    1.  
    2. function Start ()
    3. {
    4. var startTime;
    5. var jumpstart=false; //make it false in Start ()
    6. }
    7.  
    8. functioin Update
    9. {
    10. //at first jumpstart is false but when starting to fall jumpstart will become true
    11. //and then the value of Time.Time will be stored in starttime and when
    12. //you collide with the groun again jumpstart will become false and then startTime will be subtrackted from current value of Time.Time
    13. if (!IsGrounded())
    14. {
    15. if (jumpstart != true) //first frame that you are falling
    16. {
    17. jumpstart = true;
    18. startTime = Time.Time;
    19. }
    20. }
    21. else
    22. {
    23. if (jumpstart == true) //you collided with ground
    24. {
    25. jumpstart = fales; //should be false to determine next falling state's start time
    26. print (Time.Time-starttime); //this is what you want.
    27. }
    28. }
    29. }
     
  3. yahadi

    yahadi

    Joined:
    Jun 28, 2009
    Posts:
    36
    Ok, I am getting strange values like .

    First of all, although I am always falling from the same point each time I am getting a different value.

    Second, the values I am getting are strange like 1093669 and 083598 etc..

    I need them to be consistent and in seconds, like the player spent 5 seconds in the air before he collided with the ground therefore make him die from the fall.
     
  4. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    the value is a float number and you can use
    Mathf.Round () function to make it better for your nneds but if you get different numbers myabe you put the code in an inappropreate place. put the code in FixedUpdate or Update. FixedUpdate is more precise
     
  5. Ashkan_gc

    Ashkan_gc

    Joined:
    Aug 12, 2009
    Posts:
    1,124
    oh i forgot to say maybe javascript don't make the startTime a float value (i don't know javascript well and i use C#) maybe you should declare it as
    var startTime:float;

    i am not sure but i think it should be a float automatically when you store Time.Time in it.