Advertisement
gameDevTeacher

Custom Input Axis

Feb 13th, 2021
552
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.75 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.InputSystem;
  3.  
  4. public class Input: MonoBehaviour
  5. {
  6.         private Vector2 _inputHorizontal;
  7.         private Vector2 _inputVertical;
  8.  
  9.         public float Horizontal { get; private set; }
  10.         public float Vertical { get; private set; }
  11.  
  12.         private void Update() => GetAxis();
  13.  
  14.         private void GetAxis()
  15.         {
  16.             // Convert the bool of keyboard press to a float number, store in a vector2
  17.             // this is known as a conditional statement, its like a scrunched up if statement.
  18.              
  19.             _inputHorizontal.x = Keyboard.current.aKey.isPressed ? -1f : 0f;
  20.             _inputHorizontal.y = Keyboard.current.dKey.isPressed ? 1f : 0f;
  21.            
  22.             _inputVertical.x = Keyboard.current.wKey.isPressed ? 1f : 0f;
  23.             _inputVertical.y = Keyboard.current.sKey.isPressed ? -1f : 0f;
  24.            
  25.             // Convert the Vector 2 to a float which contains the absolute positive or negative number of input.
  26.             Horizontal = _inputHorizontal.y + _inputHorizontal.x;
  27.             Vertical = _inputVertical.y + _inputVertical.x;
  28.         }
  29.        
  30.         private void GetAxisShort()
  31.         {
  32.             /*
  33.                Short hand - could also run the above code as a compressed bool to float function
  34.                Also added an Else if to check for Gamepad Input: Here you will need an if statement to check whether a Gamepad is Connected
  35.            */
  36.            
  37.             if (Keyboard.current != null)
  38.             {
  39.                 Horizontal = (Keyboard.current.sKey.isPressed ? -1f : 0f) + (Keyboard.current.wKey.isPressed ? 1f : 0f);
  40.             }
  41.             else if (Gamepad.current != null)
  42.             {
  43.                 Horizontal = (Gamepad.current.dpad.left.isPressed ? -1f : 0f) + (Gamepad.current.dpad.right.isPressed ? 1f : 0f);
  44.             }
  45.         }
  46.         public Vector2 GetAxisVector2()
  47.         {
  48.             // Convert the bool of keyboard press to a float number, store in a vector2
  49.             // this is known as a conditional statement, its like a scrunched up if statement.
  50.              
  51.             _inputHorizontal.x = Keyboard.current.aKey.isPressed ? -1f : 0f;
  52.             _inputHorizontal.y = Keyboard.current.dKey.isPressed ? 1f : 0f;
  53.            
  54.             _inputVertical.x = Keyboard.current.wKey.isPressed ? 1f : 0f;
  55.             _inputVertical.y = Keyboard.current.sKey.isPressed ? -1f : 0f;
  56.            
  57.             // Convert the Vector 2 to a float which contains the absolute positive or negative number of input.
  58.             Horizontal = _inputHorizontal.y + _inputHorizontal.x;
  59.             Vertical = _inputVertical.y + _inputVertical.x;
  60.  
  61.             return new Vector2(Horizontal, Vertical);
  62.         }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement