Simply put, I want to know how to count every time I hit a key and have that number available for later use. I can’t seem to find a strait answer anywhere.
What I am trying to do is to have the player mash two buttons back and forth in order to charge an attack, with the damage being how many times the two buttons were pressed. I need the damage number to be available for use in UI later as well.
This is probably super simple, but I’m just learning how to code.
private System.Collections.Generic.Dictionary<KeyCode,int> keyPressesCount = new System.Collections.Generic.Dictionary<KeyCode,int>();
void Update()
{
// Change `Space` by the desired key
if( GetKeyDown( KeyCode.Space ) )
{
// Charge your attack
}
}
bool GetKeyDown( KeyCode key )
{
if( Input.GetKeyDown( key ) )
{
if( !keyPressesCount.Contains( key ) )
keyPressesCount[key] = 0;
keyPressesCount[key]++;
return true;
}
return false;
}
Please, next time, provide what you have tried.