Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Input not reqistered new input system

Discussion in 'Scripting' started by unity_CD2540EB4C20AF61E5F0, Sep 10, 2023.

  1. unity_CD2540EB4C20AF61E5F0

    unity_CD2540EB4C20AF61E5F0

    Joined:
    Sep 7, 2023
    Posts:
    3
    So when i try to rotate using my scroll wheel i usally dont get any input at all, it works some times but most of the time it returns 0 as the input. I am using unitys new input system.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class CameraController : MonoBehaviour
    8. {
    9.     public InputActionAsset cameraControls;
    10.     private InputActionMap cameraMap;
    11.     private InputAction panAction;
    12.     private InputAction rotateAction;
    13.     private InputAction mouseRotateAction;
    14.  
    15.     public float panSpeed = 10f;
    16.     public float rotationSpeed = 10f;
    17.  
    18.     private Vector3 forwardDirection;
    19.  
    20.     float rotateAmount;
    21.  
    22.     private void Start()
    23.     {
    24.         forwardDirection = transform.forward;
    25.     }
    26.  
    27.     private void Update()
    28.     {
    29.         Pan();
    30.         Rotate();
    31.     }
    32.  
    33.     private void Pan()
    34.     {
    35.         Vector2 panInput = panAction.ReadValue<Vector2>();
    36.  
    37.         float panX = panInput.x;
    38.         float panY = panInput.y;
    39.  
    40.         Vector3 panMovement = forwardDirection * panY + transform.right * panX;
    41.  
    42.         transform.position += panMovement * panSpeed * Time.deltaTime;
    43.     }
    44.  
    45.     private void Rotate()
    46.     {
    47.         float rotateInput = rotateAction.ReadValue<float>();
    48.  
    49.         transform.Rotate(Vector3.up * rotateInput * rotationSpeed * Time.deltaTime);
    50.  
    51.         forwardDirection = transform.forward;
    52.     }
    53.  
    54.     private void MouseRotation(InputAction.CallbackContext context)
    55.     {
    56.         Vector2 mouseDelta = Mouse.current.delta.ReadValue();
    57.  
    58.         rotateAmount = mouseDelta.x * rotationSpeed * Time.deltaTime;
    59.  
    60.         transform.Rotate(Vector3.up, rotateAmount);
    61.     }
    62.  
    63.     private void MouseRotationOff(InputAction.CallbackContext context)
    64.     {
    65.         Debug.Log(rotateAmount);
    66.     }
    67.  
    68.     private void OnEnable()
    69.     {
    70.         cameraMap = cameraControls.FindActionMap("CameraMap");
    71.         panAction = cameraMap.FindAction("PanAction");
    72.         rotateAction = cameraMap.FindAction("RotateAction");
    73.         mouseRotateAction = cameraMap.FindAction("MouseRotateAction");
    74.  
    75.         mouseRotateAction.started += MouseRotation;
    76.         mouseRotateAction.canceled += MouseRotationOff;
    77.  
    78.         cameraMap.Enable();
    79.     }
    80.  
    81.     private void OnDestroy()
    82.     {
    83.         cameraMap.Disable();
    84.     }
    85. }
     
  2. unity_CD2540EB4C20AF61E5F0

    unity_CD2540EB4C20AF61E5F0

    Joined:
    Sep 7, 2023
    Posts:
    3
    So solved it by remaking the code a bit here is the finished product!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using System.Threading;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class CameraController : MonoBehaviour
    8. {
    9.     public InputActionAsset cameraControls;
    10.     private InputActionMap cameraMap;
    11.     private InputAction panAction;
    12.     private InputAction rotateAction;
    13.     private InputAction mRotateAction;
    14.  
    15.     public float panSpeed = 10f;
    16.     public float rotationSpeed = 10f;
    17.     public float mouseRotationSpeed = 10f;
    18.  
    19.     private Vector3 forwardDirection;
    20.     private void Start()
    21.     {
    22.         forwardDirection = transform.forward;
    23.     }
    24.  
    25.     private void Update()
    26.     {
    27.         Pan();
    28.         Rotate();
    29.     }
    30.  
    31.     private void Pan()
    32.     {
    33.         Vector2 panInput = panAction.ReadValue<Vector2>();
    34.  
    35.         float panX = panInput.x;
    36.         float panY = panInput.y;
    37.  
    38.         Vector3 panMovement = forwardDirection * panY + transform.right * panX;
    39.  
    40.         transform.position += panMovement * panSpeed * Time.deltaTime;
    41.     }
    42.  
    43.     private void Rotate()
    44.     {
    45.         float rotateInput = rotateAction.ReadValue<float>();
    46.  
    47.         transform.Rotate(Vector3.up * rotateInput * rotationSpeed * Time.deltaTime);
    48.  
    49.         forwardDirection = transform.forward;
    50.     }
    51.  
    52.     private void MouseRotation(InputAction.CallbackContext context)
    53.     {
    54.         if (!Mouse.current.middleButton.isPressed)
    55.             return;
    56.  
    57.         float mouseRotate = context.ReadValue<Vector2>().x;
    58.  
    59.         transform.Rotate(Vector3.up * mouseRotate * mouseRotationSpeed * Time.deltaTime);
    60.  
    61.     }
    62.  
    63.     private void OnEnable()
    64.     {
    65.         cameraMap = cameraControls.FindActionMap("CameraMap");
    66.         panAction = cameraMap.FindAction("PanAction");
    67.         rotateAction = cameraMap.FindAction("RotateAction");
    68.         mRotateAction = cameraMap.FindAction("MRotateAction");
    69.  
    70.         mRotateAction.performed += MouseRotation;
    71.  
    72.         cameraMap.Enable();
    73.     }
    74.  
    75.     private void OnDisable()
    76.     {
    77.         mRotateAction.performed -= MouseRotation;
    78.  
    79.         cameraMap.Disable();
    80.     }
    81. }