Hi. I’m not really sure how to word this, sorry! I’m making a cycling game and being able to measure the speed at which the player is “pedaling” is very important. I’ve made a start, but it just doesn’t look right and leads to very weird in-game behaviour.
The player presses the arrow keys left and right to pedal. Each keystroke is a half revolution of the pedals. The cadence is the average speed of full revolutions of the pedals, if that makes sense. Here’s what I have so far.
void Start ()
{
lastPedal = "none";
halfRev = 0;
rpm = 0;
//Count rpm
StartCoroutine(startCadenceCalc());
}
void Update ()
{
if(Input.GetKeyDown(KeyCode.LeftArrow) && lastPedal != "left")
{
halfRev += 1;
lastPedal = "left";
}
if (Input.GetKeyDown(KeyCode.RightArrow) && lastPedal != "right")
{
halfRev += 1;
lastPedal = "right";
}
}
IEnumerator startCadenceCalc()
{
StartCoroutine(cadenceCalc1());
yield return new WaitForSeconds(1);
StartCoroutine(cadenceCalc2());
yield return new WaitForSeconds(1);
StartCoroutine(cadenceCalc3());
yield return new WaitForSeconds(1);
StartCoroutine(cadenceCalc4());
yield return new WaitForSeconds(1);
StartCoroutine(cadenceCalc5());
yield return new WaitForSeconds(1);
StartCoroutine(cadenceCalc6());
yield return new WaitForSeconds(1);
}
IEnumerator cadenceCalc1()
{
int currentRev1 = halfRev;
yield return new WaitForSeconds(6);
rpm = (halfRev - currentRev1) * 2;
StartCoroutine(cadenceCalc1());
}
IEnumerator cadenceCalc2()
{
int currentRev2 = halfRev;
yield return new WaitForSeconds(6);
rpm = (halfRev - currentRev2) * 2;
StartCoroutine(cadenceCalc2());
}
IEnumerator cadenceCalc3()
{
int currentRev3 = halfRev;
yield return new WaitForSeconds(6);
rpm = (halfRev - currentRev3) * 2;
StartCoroutine(cadenceCalc3());
}
IEnumerator cadenceCalc4()
{
int currentRev4 = halfRev;
yield return new WaitForSeconds(6);
rpm = (halfRev - currentRev4) * 2;
StartCoroutine(cadenceCalc4());
}
IEnumerator cadenceCalc5()
{
int currentRev5 = halfRev;
yield return new WaitForSeconds(6);
rpm = (halfRev - currentRev5) * 2;
StartCoroutine(cadenceCalc5());
}
IEnumerator cadenceCalc6()
{
int currentRev6 = halfRev;
yield return new WaitForSeconds(6);
rpm = (halfRev - currentRev6) * 2;
StartCoroutine(cadenceCalc6());
}
I have multiple calculations to update the average calculation every second rather than every 6 seconds.
I know the code is very bad, but I really don’t know how to start on improving it and I’m not really sure how to word it for a Google search.
Any help on how to do this or where to look?
Thanks.