I am calculating the time the player is spending falling from a high location. I used: Code (csharp): function Update () { if (!playerController.IsGrounded() Time.time>airtimer) { airtimer=Time.time+1; print (airtimer); } } 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]
you need 2 more variables Code (csharp): function Start () { var startTime; var jumpstart=false; //make it false in Start () } functioin Update { //at first jumpstart is false but when starting to fall jumpstart will become true //and then the value of Time.Time will be stored in starttime and when //you collide with the groun again jumpstart will become false and then startTime will be subtrackted from current value of Time.Time if (!IsGrounded()) { if (jumpstart != true) //first frame that you are falling { jumpstart = true; startTime = Time.Time; } } else { if (jumpstart == true) //you collided with ground { jumpstart = fales; //should be false to determine next falling state's start time print (Time.Time-starttime); //this is what you want. } } }
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.
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
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.