Search Unity

How to reference New InputSystem Input Values in C# Script?

Discussion in 'Input System' started by Fox_GAMING, Jan 1, 2021.

  1. Fox_GAMING

    Fox_GAMING

    Joined:
    May 7, 2018
    Posts:
    12
    Hi, I'm working on a game, where the player collects trash, from Trash cans, and dumps them into a Trash Truck. I'm currently trying to convert my game over to the new Input System, so that I can have support for multiple controllers.

    I'm using a particular asset, on the Unity Asset Store, called First Person All-In-One. It uses the Old Input System.
    I've written a script, called MultInput, which uses the New Input System.
    Here's the Script:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class MultInput : MonoBehaviour
    7. {
    8.     #region oldscript
    9.     /*
    10.     [SerializeField] public InputAction movement;
    11.  
    12.     Vector2 move;
    13.     Vector2 direction;
    14.  
    15.     private void Awake()
    16.     {
    17.         movement.performed += OnMovementPerformed;
    18.         movement.canceled += OnMovementPerformed;
    19.     }
    20.  
    21.     private void OnMovementPerformed(InputAction.CallbackContext context)
    22.     {
    23.         direction = context.ReadValue<Vector2>();
    24.  
    25.         Horizontal = direction.x;
    26.         Vertical = direction.y;
    27.     }
    28.  
    29.     private void OnDisable()
    30.     {
    31.         movement.Disable();
    32.     }
    33.  
    34.     private void OnEnable()
    35.     {
    36.         movement.Enable();
    37.     }
    38.  
    39.     // Start is called before the first frame update
    40.     void Start()
    41.     {
    42.    
    43.     }
    44.  
    45.     // Update is called once per frame
    46.     void Update()
    47.     {
    48.         Vector2 d = new Vector2(direction.x, direction.y) * Time.deltaTime;
    49.         Debug.Log("MultInput-Move- :" + d);
    50.     }
    51.  
    52.     public float Vertical { get; set; }
    53.  
    54.     public float Horizontal { get; set; }
    55.     */
    56.     #endregion
    57.  
    58.     #region newscript
    59.  
    60.     public PlayerControls controls;
    61.  
    62.     public float primVal;
    63.     public float secVal;
    64.     public float randKey;
    65.     public float equipKey;
    66.     public float jumpKey;
    67.     public float crouchKey;
    68.  
    69.     void Awake()
    70.     {
    71.         controls = new PlayerControls();
    72.  
    73.         controls.Gameplay.Primary.performed += ctx => primVal = ctx.ReadValue<float>();
    74.         controls.Gameplay.Primary.canceled += ctx => primVal = 0.0f;
    75.  
    76.         controls.Gameplay.Secondary.performed += ctx => secVal = ctx.ReadValue<float>();
    77.         controls.Gameplay.Secondary.canceled += ctx => secVal = 0.0f;
    78.  
    79.         controls.Gameplay.RandKey.performed += ctx => randKey = ctx.ReadValue<float>();
    80.         controls.Gameplay.RandKey.canceled += ctx => randKey = 0.0f;
    81.  
    82.         controls.Gameplay.EquipKey.performed += ctx => equipKey = ctx.ReadValue<float>();
    83.         controls.Gameplay.EquipKey.canceled += ctx => equipKey = 0.0f;
    84.  
    85.         controls.Gameplay.Jump.performed += ctx => jumpKey = ctx.ReadValue<float>();
    86.         controls.Gameplay.Jump.canceled += ctx => jumpKey = 0.0f;
    87.  
    88.         controls.Gameplay.Crouch.performed += ctx => crouchKey = ctx.ReadValue<float>();
    89.         controls.Gameplay.Crouch.canceled += ctx => crouchKey = 0.0f;
    90.     }
    91.  
    92.     void PrimButton()
    93.     {
    94.         Debug.Log("Primary Button!");
    95.     }
    96.  
    97.     void Update()
    98.     {
    99.         Debug.Log("Primary Value: " + primVal);
    100.         Debug.Log("Secondary Value: " + secVal);
    101.         Debug.Log("RandKey Value: " + randKey);
    102.         Debug.Log("EquipKey Value: " + equipKey);
    103.         Debug.Log("JumpKey Value: " + jumpKey);
    104.         Debug.Log("CrouchKey Value: " + crouchKey);
    105.     }
    106.  
    107.     void OnEnable()
    108.     {
    109.         controls.Gameplay.Enable();
    110.     }
    111.  
    112.     void OnDisable()
    113.     {
    114.         controls.Gameplay.Disable();
    115.     }
    116.     #endregion
    117. }
    118.  
    In the FPSAIO Asset's First-Person-Controller Script (Called FirstPersonAIO.cs,), It uses the Old Input System, and uses things like, "Input.GetAxis", and, "Input.GetButtonDown". I'm trying to reference my MultInput Script within this Asset's Script. I've also added a Debug.Log that gets the float value of one of the Input Actions, and it does work, when I press the Key, it correctly displays 1, and when I release it, it displays 0. However, (In This Example, at least, I'm trying to get the Jump Key to work.,), the Player / Character will not jump.
    Here's the code I have currently, from the FPSAIO Script. MultInt is the reference to the MultInput Script.
    Code (CSharp):
    1.     private void Update(){
    2.  
    3.         #region Look Settings - Update
    4.  
    5.         Debug.Log("FPSAIO Jump: " + multInt.jumpKey);
    6.         multInt.controls.Gameplay.Jump.performed += ctx => multInt.jumpKey = ctx.ReadValue<float>();
    7.         multInt.controls.Gameplay.Jump.canceled += ctx => multInt.jumpKey = 0.0f;
    8.  
    9.         if (enableCameraMovement && !controllerPauseState){
    10.             float mouseYInput = 0;
    11.             float mouseXInput = 0;
    12.             float camFOV = playerCamera.fieldOfView;
    13.             if (cameraInputMethod == CameraInputMethod.Traditional || cameraInputMethod == CameraInputMethod.TraditionalWithConstraints){
    14.                     mouseYInput = mouseInputInversion == InvertMouseInput.None || mouseInputInversion == InvertMouseInput.X ? Input.GetAxis("Mouse Y") : -Input.GetAxis("Mouse Y");
    15.                     mouseXInput = mouseInputInversion == InvertMouseInput.None || mouseInputInversion == InvertMouseInput.Y ? Input.GetAxis("Mouse X") : -Input.GetAxis("Mouse X");
    16.             }
    17.             else{
    18.                 mouseXInput= Input.GetAxis("Horizontal") * (mouseInputInversion == InvertMouseInput.None || mouseInputInversion == InvertMouseInput.Y ? 1 : -1);
    19.             }            if(targetAngles.y > 180) { targetAngles.y -= 360; followAngles.y -= 360; } else if(targetAngles.y < -180) { targetAngles.y += 360; followAngles.y += 360; }
    20.             if(targetAngles.x > 180) { targetAngles.x -= 360; followAngles.x -= 360; } else if(targetAngles.x < -180) { targetAngles.x += 360; followAngles.x += 360; }
    21.             targetAngles.y += mouseXInput * (mouseSensitivity - ((baseCamFOV-camFOV)*fOVToMouseSensitivity)/6f);
    22.             if (cameraInputMethod == CameraInputMethod.Traditional){ targetAngles.x += mouseYInput * (mouseSensitivity - ((baseCamFOV - camFOV) * fOVToMouseSensitivity) / 6f);}
    23.             else {targetAngles.x = 0f;}
    24.             targetAngles.x = Mathf.Clamp(targetAngles.x, -0.5f * verticalRotationRange, 0.5f * verticalRotationRange);
    25.             followAngles = Vector3.SmoothDamp(followAngles, targetAngles, ref followVelocity, (cameraSmoothing)/100);
    26.        
    27.             playerCamera.transform.localRotation = Quaternion.Euler(-followAngles.x + originalRotation.x,0,0);
    28.             transform.localRotation =  Quaternion.Euler(0, followAngles.y+originalRotation.y, 0);
    29.         }
    30.  
    31.         #endregion
    32.  
    33.         #region Input Settings - Update
    34.         //if(canHoldJump ? (canJump && Input.GetButton("Jump")) : (Input.GetButtonDown("Jump") && canJump) ){
    35.         if (canHoldJump ? (canJump && multInt.jumpKey == 1) : (multInt.jumpKey == 1 && canJump))
    36.         {
    37.             jumpInput = true;
    38.         }//else if(Input.GetButtonUp("Jump")){jumpInput = false;}
    39.         else if (multInt.jumpKey == 0) { jumpInput = false; }
    40.  
    41.  
    Also, and Update, I realize this now that this probably should be mentioned, but it appears as if none of the KeyBindings work with the New Input System. For all of them, I'm doing something along the lines of,
    Code (CSharp):
    1. if(multInt.FLOATNAME == 1.0f)
    instead of
    Code (CSharp):
    1. if(Input.GetKey(KeyCode.THEKEY)
    , or something along those lines.

    Does anyone have any idea how I can fix this? Or what I should do? Anything to help would be greatly appreciated.

    If anyone needs any more information, please, let me know!
    Thanks!
     
    Last edited: Jan 1, 2021