- Home /
RPG Action Progress Bar always same speed
Hello, I make an RPG with turn based combat where the turn ordere is calculated based on the character speed. A Coroutine checks if a character is ready. If no character is ready it increases all characters Action Progress bar by a value ( different for each char) and starts the coroutine again as long as no character is ready for action. My Problem is now that i wanted to test it and every time i start the play mode, the action bars recharge at a different speed. I think its because of framerate and other stuff going on. but this will be a problem with different devices as some people will wait longer than other. Tried to solve this with Time.deltaTime but its not exactly working. Can someone have a look on my Code?
IEnumerator WaitForCharactersReady()
{
float tickTime = 0.02f;
if (readyCharacters.Count == 0)
{
for (int i = 0; i < playerTeamControllers.Length; i++)
{
playerTeamControllers[i].characterStats.IncreaseActionProgressByOneTick();
playerSpotUis[i].SetActionBar((int)playerTeamControllers[i].characterStats.actionProgress);
if (playerTeamControllers[i].characterStats.isReadyForAction())
{
readyCharacters.Add(enemyTeamControllers[i]);
}
}
for (int i = 0; i < enemyTeamControllers.Length; i++)
{
enemyTeamControllers[i].characterStats.IncreaseActionProgressByOneTick();
enemySpotUis[i].SetActionBar((int)enemyTeamControllers[i].characterStats.actionProgress);
if (enemyTeamControllers[i].characterStats.isReadyForAction())
{
readyCharacters.Add(enemyTeamControllers[i]);
}
}
while(tickTime >= 0.0f)
{
tickTime -= Time.deltaTime;
yield return null;
}
StartCoroutine(WaitForCharactersReady());
}
}
Answer by s4shrish · Jan 04 at 06:09 PM
Hi, I don't understand your in-game logic, but I can help with the coroutine stuff.
You can try using "yield return new WaitForSeconds()" to wait a specific amount of time.
But alternatively I would recommend you using "yield return new WaitForFixedUpdate();". It will run alongside FixedUpdate for 50 times a second. That should solve your issue of different characters incrementing by variable amount.
That said, regardless of the way the coroutine is running, it should increase all character ticktime evenly, so my guess is that IncreaseActionProgressByOneTick(); has some issues as well.
If I would have to do something similar, I would do it somewhat like below
IEnumerator ActionCoro() { while (!gameOver)//Or just true { if(readyCharacters.Count <= 0) { for () { //go through all the player characters } for () { //go through all the enemy characters } } yield return new WaitForFixedUpdate(); } }