Advertisement
Guest User

Perform one of several actions on input Unity3D

a guest
Apr 24th, 2019
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1.     IEnumerator PerformActionOnInput(Dictionary<string, Action> inputActions)
  2.     {
  3.         bool pressed = false;
  4.         while (!pressed)
  5.         {
  6.             foreach (string k in inputActions.Keys)
  7.             {
  8.                 if (Input.GetButtonDown(k))
  9.                 {
  10.                     inputActions[k].Invoke();
  11.                     pressed = true;
  12.                     break;
  13.                 }
  14.             }
  15.             yield return null;
  16.         }
  17.     }
  18.  
  19.     public void Move()
  20.     {
  21.         if(!selected)
  22.             return;
  23.  
  24.         var actions = new Dictionary<string, Action>()
  25.         {
  26.             {
  27.                 "Fire1", () =>
  28.                 {
  29.                     Debug.Log("Moving: " + selected.name);
  30.                     var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
  31.                     RaycastHit hit;
  32.                     if (Physics.Raycast(ray, out hit, float.MaxValue, moveMask))
  33.                     {
  34.                         selected.Move(hit.point);
  35.                     }
  36.                 }
  37.             },
  38.             {
  39.                 "Fire2", () => { return; }
  40.             }
  41.         };
  42.  
  43.         StartCoroutine(PerformActionOnInput(actions));
  44.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement