Advertisement
BinaryImpactG

Unity Tip - Camera movement with the new input system

Jan 26th, 2021
1,320
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.InputSystem;
  5.  
  6. public class CameraMovement : MonoBehaviour {
  7.  
  8.     public float movementSensitivity = 20f;
  9.     public float rotationSensitivity = 20f;
  10.     public float panSensitivity = 20f;
  11.  
  12.     private Vector2 m_inputMouseMoveDelta;
  13.     private Vector2 m_inputMove;
  14.     private float m_verticalMove;
  15.     private bool m_CameraMoveFlag = false;
  16.     private bool m_CameraPanFlag = false;
  17.  
  18.     public void SetCameraMoveFlag( InputAction.CallbackContext context ) {
  19.         switch ( context.phase ) {
  20.             case InputActionPhase.Started:
  21.             case InputActionPhase.Performed:
  22.                 m_CameraMoveFlag = true;
  23.                 break;
  24.             default:
  25.                 m_CameraMoveFlag = false;
  26.                 break;
  27.         }
  28.     }
  29.     public void SetCameraPanFlag( InputAction.CallbackContext context ) {
  30.         switch ( context.phase ) {
  31.             case InputActionPhase.Started:
  32.             case InputActionPhase.Performed:
  33.                 m_CameraPanFlag = true;
  34.                 break;
  35.             default:
  36.                 m_CameraPanFlag = false;
  37.                 break;
  38.         }
  39.     }
  40.     public void SetCameraMove( InputAction.CallbackContext context ) => m_inputMove = context.canceled ? Vector2.zero : context.ReadValue<Vector2> ();
  41.     public void SetMouseMoveDelta( InputAction.CallbackContext context ) => m_inputMouseMoveDelta = context.canceled ? Vector2.zero : context.ReadValue<Vector2> ();
  42.     public void SetVerticalMove( InputAction.CallbackContext context ) => m_verticalMove = context.canceled ? 0f : context.ReadValue<float> ();
  43.     void Update() {
  44.         Rotation ();
  45.         Move ();
  46.         Pan ();
  47.     }
  48.     private void Rotation() {
  49.         if ( m_CameraMoveFlag ) {
  50.             transform.Rotate ( Vector3.up * m_inputMouseMoveDelta.x * rotationSensitivity * Time.deltaTime, Space.World );
  51.             transform.Rotate ( Vector3.left * m_inputMouseMoveDelta.y * rotationSensitivity * Time.deltaTime, Space.Self );
  52.         }
  53.     }
  54.     private void Move () {
  55.         if ( m_CameraMoveFlag ) {
  56.             transform.position += ( transform.right * m_inputMove.x + transform.forward * m_inputMove.y ) * Time.deltaTime * movementSensitivity;
  57.             transform.position += transform.up * Time.deltaTime * movementSensitivity * m_verticalMove;
  58.         }
  59.     }
  60.     private void Pan() {
  61.         if ( m_CameraPanFlag ) {
  62.             transform.position -= ( transform.right * m_inputMouseMoveDelta.x + transform.up * m_inputMouseMoveDelta.y ) * Time.deltaTime * panSensitivity;
  63.         }
  64.     }
  65. }
  66.  
Advertisement
RAW Paste Data Copied
Advertisement