0

Why does my loop not work at all in unity? If the tempo is too high (120), it freaks out, but I need the tempo that high for a rhythm game.

    void Update()
    {
        BeatTime += Time.deltaTime;
        if(BeatTime > (60 / Tempo))
        {
        Beat();
        BeatTime -= (60 / Tempo);
        }
    }

What it is supposed to do is play the function Beat every beat according to the tempo. I've tried changing the way I added and removed the value on BeatTime, and changed the if condition, but it doesn't work. I want to get a consistent beat but it doesn't work. Unity C#.

  • 1
    Does this answer your question? [Why does integer division in C# return an integer and not a float?](https://stackoverflow.com/questions/10851273/why-does-integer-division-in-c-sharp-return-an-integer-and-not-a-float) – shingo Jun 03 '23 at 04:45
  • @shingo Without seeing more of their code, that would only come into play if Tempo is an int variable instead of a float or double. – B.O.B. Jun 03 '23 at 06:12
  • You will get `float` conversion as long as a floating value is involved in the particular calculation. For example if you put ‘60f’ or multiply a number by ‘1f’ it will get converted to `float` and the result will be a floating value. – Phillip Zoghbi Jun 03 '23 at 08:44
  • replace (60 / Tempo) by (60f / (float)Tempo) – Yann Massard Jun 03 '23 at 11:49

0 Answers0