Advertisement
Xsaxin

Unity Input Buffer Fighting Game Sample

Oct 23rd, 2023
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.04 KB | Gaming | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using TMPro;
  4. using UnityEngine;
  5.  
  6. [CreateAssetMenu(fileName ="ComboData",menuName="Custom/Combodata")]
  7. public class ComboData : ScriptableObject
  8. {
  9.     public string comboKey;
  10.     public string Action;
  11. }
  12.  
  13. public class ComboSystem : MonoBehaviour
  14. {
  15.     //public string[] comboSequences;
  16.     public ComboData[] comboData;
  17.  
  18.     public float comboInputWindow = 0.5f;
  19.  
  20.     private string currentInput = "";
  21.  
  22.     private bool comboExecuted = false;
  23.     private float comboExecutedTime = 0f;
  24.  
  25.     //Text
  26.     public TextMeshProUGUI keyInput;
  27.     public TextMeshProUGUI ExecuteInput;
  28.     void Update()
  29.     {
  30.         if (currentInput.Length > 0)
  31.         {
  32.             comboExecutedTime += Time.deltaTime;
  33.         }
  34.  
  35.         foreach (ComboData combo in comboData)
  36.         {
  37.             if(currentInput.EndsWith(combo.comboKey))
  38.             {
  39.                 comboExecuted = true;
  40.                 ExecuteCombo(combo);
  41.                 currentInput = "";
  42.                 break;
  43.             }
  44.         }
  45.  
  46.         if (comboExecuted && (Time.time - comboExecutedTime) > comboInputWindow)
  47.         {
  48.             comboExecuted = false;
  49.         }
  50.  
  51.         if(comboExecutedTime >= comboInputWindow && currentInput.Length > 0)
  52.         {
  53.             //Debug.Log("Timed");
  54.             currentInput = "";
  55.             comboExecutedTime = 0;
  56.             ExecuteInput.text = "";
  57.             keyInput.text = "";
  58.         }
  59.        
  60.  
  61.         if(Input.GetKeyDown(KeyCode.P))
  62.         {
  63.             currentInput += "P";
  64.             comboExecutedTime = 0;
  65.             keyInput.text = currentInput;
  66.         }
  67.  
  68.         if(Input.GetKeyDown(KeyCode.K))
  69.         {
  70.             currentInput += "K";
  71.             comboExecutedTime = 0;
  72.             keyInput.text = currentInput;
  73.         }
  74.     }
  75.  
  76.     void ExecuteCombo(ComboData combo)
  77.     {
  78.         Debug.Log("Combo Executed: " + combo);
  79.         ExecuteInput.text = combo.Action + " : " + combo.comboKey;
  80.         keyInput.text = "";
  81.     }
  82. }
  83.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement