Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using UnityEngine.InputSystem;
- public class Input: MonoBehaviour
- {
- private Vector2 _inputHorizontal;
- private Vector2 _inputVertical;
- public float Horizontal { get; private set; }
- public float Vertical { get; private set; }
- private void Update() => GetAxis();
- private void GetAxis()
- {
- // Convert the bool of keyboard press to a float number, store in a vector2
- // this is known as a conditional statement, its like a scrunched up if statement.
- _inputHorizontal.x = Keyboard.current.aKey.isPressed ? -1f : 0f;
- _inputHorizontal.y = Keyboard.current.dKey.isPressed ? 1f : 0f;
- _inputVertical.x = Keyboard.current.wKey.isPressed ? 1f : 0f;
- _inputVertical.y = Keyboard.current.sKey.isPressed ? -1f : 0f;
- // Convert the Vector 2 to a float which contains the absolute positive or negative number of input.
- Horizontal = _inputHorizontal.y + _inputHorizontal.x;
- Vertical = _inputVertical.y + _inputVertical.x;
- }
- private void GetAxisShort()
- {
- /*
- Short hand - could also run the above code as a compressed bool to float function
- Also added an Else if to check for Gamepad Input: Here you will need an if statement to check whether a Gamepad is Connected
- */
- if (Keyboard.current != null)
- {
- Horizontal = (Keyboard.current.sKey.isPressed ? -1f : 0f) + (Keyboard.current.wKey.isPressed ? 1f : 0f);
- }
- else if (Gamepad.current != null)
- {
- Horizontal = (Gamepad.current.dpad.left.isPressed ? -1f : 0f) + (Gamepad.current.dpad.right.isPressed ? 1f : 0f);
- }
- }
- public Vector2 GetAxisVector2()
- {
- // Convert the bool of keyboard press to a float number, store in a vector2
- // this is known as a conditional statement, its like a scrunched up if statement.
- _inputHorizontal.x = Keyboard.current.aKey.isPressed ? -1f : 0f;
- _inputHorizontal.y = Keyboard.current.dKey.isPressed ? 1f : 0f;
- _inputVertical.x = Keyboard.current.wKey.isPressed ? 1f : 0f;
- _inputVertical.y = Keyboard.current.sKey.isPressed ? -1f : 0f;
- // Convert the Vector 2 to a float which contains the absolute positive or negative number of input.
- Horizontal = _inputHorizontal.y + _inputHorizontal.x;
- Vertical = _inputVertical.y + _inputVertical.x;
- return new Vector2(Horizontal, Vertical);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement