shadowx320

InputHandler

Mar 19th, 2021 (edited)
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.22 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. namespace SG
  6. {
  7.     public class InputHandler : MonoBehaviour
  8.     {
  9.         public float horizontal;
  10.         public float vertical;
  11.         public float moveAmount;
  12.         public float mouseX;
  13.         public float mouseY;
  14.  
  15.         public bool b_input;
  16.  
  17.         public bool rollFlag;
  18.         public bool isInteracting;
  19.  
  20.         PlayerControls inputActions;
  21.         CameraHandler cameraHandler;
  22.  
  23.         Vector2 movementInput;
  24.         Vector2 cameraInput;
  25.  
  26.         private void Awake()
  27.         {
  28.             cameraHandler = CameraHandler.singleton;
  29.         }
  30.  
  31.         private void FixedUpdate()
  32.         {
  33.             float delta = Time.fixedDeltaTime;
  34.  
  35.             if (cameraHandler != null)
  36.             {
  37.                 cameraHandler.FollowTarget(delta);
  38.                 cameraHandler.HandleCameraRotation(delta, mouseX, mouseY);
  39.             }
  40.         }
  41.  
  42.         public void OnEnable()
  43.         {
  44.             if (inputActions == null)
  45.             {
  46.                 inputActions = new PlayerControls();
  47.                 inputActions.PlayerMovement.Movement.performed += inputActions => movementInput = inputActions.ReadValue<Vector2>();
  48.                 inputActions.PlayerMovement.Camera.performed += i => cameraInput = i.ReadValue<Vector2>();
  49.             }
  50.  
  51.             inputActions.Enable();
  52.         }
  53.  
  54.         private void OnDisable()
  55.         {
  56.             inputActions.Disable();
  57.         }
  58.  
  59.         public void TickInput(float delta)
  60.         {
  61.             MoveInput(delta);
  62.             HandleRollInput(delta);
  63.         }
  64.  
  65.         private void MoveInput(float delta)
  66.         {
  67.             horizontal = movementInput.x;
  68.             vertical = movementInput.y;
  69.             moveAmount = Mathf.Clamp01(Mathf.Abs(horizontal) + Mathf.Abs(vertical));
  70.             mouseX = cameraInput.x;
  71.             mouseY = cameraInput.y;
  72.         }
  73.  
  74.         private void HandleRollInput(float delta)
  75.         {
  76.             b_input = inputActions.PlayerActions.Roll.phase == UnityEngine.InputSystem.InputActionPhase.Started;
  77.  
  78.             if (b_input)
  79.             {
  80.                 rollFlag = true;
  81.             }
  82.         }
  83.     }
  84. }
  85.  
  86.  
  87.  
Add Comment
Please, Sign In to add comment