Advertisement
gameDevTeacher

Custom Input

Jul 5th, 2021
926
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.21 KB | None | 0 0
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.InputSystem;
  4.  
  5. public class Input : MonoBehaviour
  6. {
  7.     public enum InputType
  8.     {
  9.         Keyboard,
  10.         Gamepad
  11.     }
  12.    
  13.     public InputType inputType;
  14.    
  15.     [HideInInspector] public Vector2 MoveVector => _moveVector;
  16.     private Vector2 _moveVector;
  17.    
  18.     private void Update()
  19.     {
  20.         switch (inputType)
  21.         {
  22.             case InputType.Keyboard:
  23.                 _moveVector.x = (Keyboard.current.aKey.isPressed ? -1f : 0f) + (Keyboard.current.dKey.isPressed ? 1f : 0f);
  24.                 _moveVector.y = (Keyboard.current.sKey.isPressed ? -1f : 0f) + (Keyboard.current.wKey.isPressed ? 1f : 0f);
  25.                 break;
  26.             case InputType.Gamepad when Gamepad.current == null:
  27.                 return;
  28.             case InputType.Gamepad:
  29.                 _moveVector.x = (Gamepad.current.dpad.left.isPressed ? -1f : 0f) + (Gamepad.current.dpad.right.isPressed ? 1f : 0f);
  30.                 _moveVector.y = (Gamepad.current.dpad.down.isPressed ? -1f : 0f) + (Gamepad.current.dpad.up.isPressed ? 1f : 0f);
  31.                 break;
  32.             default:
  33.                 throw new ArgumentOutOfRangeException();
  34.         }
  35.     }
  36. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement