Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question New InputSystem GetAxis

Discussion in 'Input System' started by resteqs, May 1, 2021.

  1. resteqs

    resteqs

    Joined:
    Nov 17, 2019
    Posts:
    7
    Hello,
    so i wanted to switch to the new InputSystem. I could figure everything out except one thing. I created a MouseX and MouseY Axis in the old System which im using to track players mouse movement. I would like to get the same values but using the new InputSystem. Unfortunately i couldn't figure it out on my own and would appreciate your help.
    upload_2021-5-2_0-37-18.png

    This is the camera code where the Mouse Input is needed (line 123):

    Code (CSharp):
    1. using UnityEngine;
    2.  
    3. [RequireComponent(typeof(Camera))]
    4. public class OrbitCamera : MonoBehaviour
    5. {
    6.  
    7.     [SerializeField] Transform focus = default;
    8.     [SerializeField, Range(1f, 20f)] float distance = 5f;
    9.     [SerializeField, Min(0f)] float focusRadius = 5f;
    10.     [SerializeField, Range(0f, 1f)] float focusCentering = 0.5f;
    11.     [SerializeField, Range(1f, 360f)] float rotationSpeed = 90f;
    12.     [SerializeField, Range(-89f, 89f)] float minVerticalAngle = -45f, maxVerticalAngle = 45f;
    13.     [SerializeField, Min(0f)] float alignDelay = 5f;
    14.     [SerializeField, Range(0f, 90f)] float alignSmoothRange = 45f;
    15.     [SerializeField] LayerMask obstructionMask = -1;
    16.     Camera regularCamera;
    17.     Vector3 focusPoint, previousFocusPoint;
    18.     Vector2 orbitAngles = new Vector2(10f, 0f);
    19.     float lastManualRotationTime;
    20.     PlayerActions InputActions;
    21.     Vector2 AxisInput;
    22.     Vector3 CameraHalfExtends
    23.     {
    24.         get
    25.         {
    26.             Vector3 halfExtends;
    27.             halfExtends.y =
    28.                 regularCamera.nearClipPlane *
    29.                 Mathf.Tan(0.5f * Mathf.Deg2Rad * regularCamera.fieldOfView);
    30.             halfExtends.x = halfExtends.y * regularCamera.aspect;
    31.             halfExtends.z = 0f;
    32.             return halfExtends;
    33.         }
    34.     }
    35.    
    36.     void OnValidate()
    37.     {
    38.         if (maxVerticalAngle < minVerticalAngle)
    39.         {
    40.             maxVerticalAngle = minVerticalAngle;
    41.         }
    42.     }
    43.  
    44.     void Awake()
    45.     {
    46.         regularCamera = GetComponent<Camera>();
    47.         focusPoint = focus.position;
    48.         transform.localRotation = Quaternion.Euler(orbitAngles);
    49.         Cursor.lockState = CursorLockMode.Locked;
    50.         InputActions = new PlayerActions();
    51.         InputActions.PlayerControls.mouse.performed += ctx => AxisInput = ctx.ReadValue<Vector2>();
    52.     }
    53.     private void OnEnable()
    54.     {
    55.         InputActions.Enable();
    56.     }
    57.     private void OnDisable()
    58.     {
    59.         InputActions.Disable();
    60.     }
    61.  
    62.     void LateUpdate()
    63.     {
    64.         UpdateFocusPoint();
    65.         Quaternion lookRotation;
    66.         if (ManualRotation() || AutomaticRotation())
    67.         {
    68.             ConstrainAngles();
    69.             lookRotation = Quaternion.Euler(orbitAngles);
    70.         }
    71.         else
    72.         {
    73.             lookRotation = transform.localRotation;
    74.         }
    75.  
    76.         Vector3 lookDirection = lookRotation * Vector3.forward;
    77.         Vector3 lookPosition = focusPoint - lookDirection * distance;
    78.  
    79.         Vector3 rectOffset = lookDirection * regularCamera.nearClipPlane;
    80.         Vector3 rectPosition = lookPosition + rectOffset;
    81.         Vector3 castFrom = focus.position;
    82.         Vector3 castLine = rectPosition - castFrom;
    83.         float castDistance = castLine.magnitude;
    84.         Vector3 castDirection = castLine / castDistance;
    85.  
    86.         if (Physics.BoxCast(
    87.             castFrom, CameraHalfExtends, castDirection, out RaycastHit hit,
    88.             lookRotation, castDistance, obstructionMask
    89.         ))
    90.         {
    91.             rectPosition = castFrom + castDirection * hit.distance;
    92.             lookPosition = rectPosition - rectOffset;
    93.         }
    94.  
    95.         transform.SetPositionAndRotation(lookPosition, lookRotation);
    96.     }
    97.  
    98.     void UpdateFocusPoint()
    99.     {
    100.         previousFocusPoint = focusPoint;
    101.         Vector3 targetPoint = focus.position;
    102.         if (focusRadius > 0f)
    103.         {
    104.             float distance = Vector3.Distance(targetPoint, focusPoint);
    105.             float t = 1f;
    106.             if (distance > 0.01f && focusCentering > 0f)
    107.             {
    108.                 t = Mathf.Pow(1f - focusCentering, Time.unscaledDeltaTime);
    109.             }
    110.             if (distance > focusRadius)
    111.             {
    112.                 t = Mathf.Min(t, focusRadius / distance);
    113.             }
    114.             focusPoint = Vector3.Lerp(targetPoint, focusPoint, t);
    115.         }
    116.         else
    117.         {
    118.             focusPoint = targetPoint;
    119.         }
    120.     }
    121.  
    122.     bool ManualRotation()
    123.     {
    124.         Vector2 input = new Vector2(-AxisInput.y, AxisInput.x);
    125.         //Vector2 input = new Vector2(-1, 1);
    126.         const float e = 0.001f;
    127.         if (input.x < -e || input.x > e || input.y < -e || input.y > e)
    128.         {
    129.             orbitAngles += rotationSpeed * Time.unscaledDeltaTime * input;
    130.             lastManualRotationTime = Time.unscaledTime;
    131.             return true;
    132.         }
    133.         return false;
    134.     }
    135.  
    136.     bool AutomaticRotation()
    137.     {
    138.         if (Time.unscaledTime - lastManualRotationTime < alignDelay)
    139.         {
    140.             return false;
    141.         }
    142.  
    143.         Vector2 movement = new Vector2(
    144.             focusPoint.x - previousFocusPoint.x,
    145.             focusPoint.z - previousFocusPoint.z
    146.         );
    147.         float movementDeltaSqr = movement.sqrMagnitude;
    148.         if (movementDeltaSqr < 0.0001f)
    149.         {
    150.             return false;
    151.         }
    152.  
    153.         float headingAngle = GetAngle(movement / Mathf.Sqrt(movementDeltaSqr));
    154.         float deltaAbs = Mathf.Abs(Mathf.DeltaAngle(orbitAngles.y, headingAngle));
    155.         float rotationChange =
    156.             rotationSpeed * Mathf.Min(Time.unscaledDeltaTime, movementDeltaSqr);
    157.         if (deltaAbs < alignSmoothRange)
    158.         {
    159.             rotationChange *= deltaAbs / alignSmoothRange;
    160.         }
    161.         else if (180f - deltaAbs < alignSmoothRange)
    162.         {
    163.             rotationChange *= (180f - deltaAbs) / alignSmoothRange;
    164.         }
    165.         orbitAngles.y =
    166.             Mathf.MoveTowardsAngle(orbitAngles.y, headingAngle, rotationChange);
    167.         return true;
    168.     }
    169.  
    170.     void ConstrainAngles()
    171.     {
    172.         orbitAngles.x =
    173.             Mathf.Clamp(orbitAngles.x, minVerticalAngle, maxVerticalAngle);
    174.  
    175.         if (orbitAngles.y < 0f)
    176.         {
    177.             orbitAngles.y += 360f;
    178.         }
    179.         else if (orbitAngles.y >= 360f)
    180.         {
    181.             orbitAngles.y -= 360f;
    182.         }
    183.     }
    184.  
    185.     static float GetAngle(Vector2 direction)
    186.     {
    187.         float angle = Mathf.Acos(direction.y) * Mathf.Rad2Deg;
    188.         return direction.x < 0f ? 360f - angle : angle;
    189.     }
    190. }
    191.  
     
  2. resteqs

    resteqs

    Joined:
    Nov 17, 2019
    Posts:
    7
    Thank you for your response,
    I have already seen this doc, but I still run into some errors. Also, as far as I know mouse position will not work since i lock the mouse to the center of the screen. I wanted to switch to the new system, so the player can adjust the mouse sensitivity. Maybe you know a solution how to do that with the old system?