Problems with Coding Frame-Rate and Button Inputs

Hello,

This is my first time working with Unity and I’m using it (and C#) to make a sprite-based 2D Fighter.

However, the problem I’m having with is the Frame Rate. I have been unable to make the game framerate-independent and I have been unable to maintain a frame rate of 60FPS.

My second problem is with the input. I am using the Street Fighter style of play and in order to perform a special you would have to input Down, Down-Right, Right, Punch. I have been testing that concept by writing a code that would accept any key as the right input. The problem with this is that it doesn’t accept any input after the first unless I hold the key down.

Can you guys help me find a solution to these problems?

Here’s the code for that particular script:

using UnityEngine;
using System.Collections;

public class SpecialMoveTest : MonoBehaviour {

	public float inputTime = 0f;
	public float inputLimit = 10f;
	
	// Use this for initialization
	void Start () {
	
	}
	
	// Update is called once per frame
	void Update () {
		StartCoroutine(InputCheck());
	}
	
	IEnumerator InputCheck() {
		if (Input.anyKeyDown) {
			Debug.Log("Down");
			while (inputTime < inputLimit) {
				yield return new WaitForSeconds(1);
				if (Input.anyKeyDown) {
					Debug.Log("Down-Right");
					while (inputTime < inputLimit) {
						yield return new WaitForSeconds(1);
						inputTime+= Time.deltaTime;
					}
				}
				yield return new WaitForSeconds(1);
				inputTime+= Time.deltaTime;
				}
		}
		if ( inputTime >= inputLimit) {
			Debug.Log("Input Finished");
			inputTime = 0;
		}
	}
}

By the way, I only have two inputs in the above because I was testing the concept and if it succeeded I would have added more inputs.

I am not for sure that a coroutine does not linger after the Update, If it does it would run into the process time of the engine update. If that happened logging would destroy your framerate.

Try moving the inputs into the Update function.

Starting a coroutine every frame in Update won’t work. Update happens every frame and can’t be interrupted, so it’s starting another instance of the coroutine every frame, so you have many running simultaneously. Just use coroutines and leave Update out.

–Eric

I’ll be releasing an Input plug-in for unity soon (within a week) that you’ll be able to do all this kind of stuff with.

Ah that makes sense, I figured that it would get held up in the while loop and update wouldn’t check/run the coroutine again, which in hindsight doesn’t make much sense. I’ll try that out and see if it works.

Sounds cool, I’ll be sure to keep an eye out for it.