Time.time * Float JS -> C#

Hi, i’m missing something here

float framesPerSecond = 2.0;

JS:
var index : int = Time.time * framesPerSecond; // Working fine, the time goes 2x faster

C#
int index = (int)Time.time * (int)framesPerSecond // Messed up, the time is multiplied by 2 resulting 2,4,6,8,10…

Any thoughts?

Hi ,

Dont’ know about the JS Code , but IMHO the C# code should look like :

float index = Time.deltaTime  * framesPerSecond ;

If I am understanding correctly what you are trying to achieve.


oxl

Yeah, don’t cast your values to integers; it’ll clamp the values. If you need to clamp “index” to an integer, you can either:
int index = (int)(Time.time * framesPerSecond);
or
int index = (int)Math.Round(Time.time * framesPerSecond)

The difference is that the first example will always floor the value (e.g., 0.99 will turn into 0) whereas the second will properly round it (0.99 will turn into 1)

If you want to keep it as a decimal number, use oxl’s solution.

Thanks, now it’s working

void Update () {
    [B][SIZE="3"]int index = (int)Mathf.Round(Time.time * framesPerSecond);[/SIZE] 	[/B]
    index = index % (uvAnimationTileX * uvAnimationTileY); 		
    Vector2 size = new Vector2(1.0f / uvAnimationTileX, 1.0f / uvAnimationTileY); 

    ...
}