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. Dismiss Notice

ReadValueAsButton() not available?? How to assign button vallues to variables

Discussion in 'Input System' started by lobotomista, May 29, 2020.

  1. lobotomista

    lobotomista

    Joined:
    Jul 21, 2018
    Posts:
    39
    Hello! So I was trying to migrate the robbieplatformer project to the new input system but this callback seems to be missing in action. Also I dont understand how to use interactions to check for button holds.

    For the movement was pretty easy I changed:

    Code (CSharp):
    1. horizontal += Input.GetAxis("Horizontal");
    for

    Code (CSharp):
    1. Movement = MyInput.Player.Move.ReadValue<Vector2>();
    2. horizontal += Movement.x;
    The problem comes after I try to assign jump code, going from this:

    Code (CSharp):
    1. jumpPressed = jumpPressed || jumpButton.GetButtonDown();
    the new input system doesn't seem to have any equivalents

    Code (CSharp):
    1. MyInput.Player.Jump.triggered
    never returns true

    and
    Code (CSharp):
    1. MyInput.Player.Jump.ReadValueAsButton()
    Gives me a method doesn't exist error.

    Any Ideas as to how to solve this? Also: How would I engage the hold interactios so I can use the jump held variable from the same game.
     
  2. Snowdrama

    Snowdrama

    Joined:
    May 10, 2014
    Posts:
    28
    I just tested this I'm not sure why triggered would never return true. Here's my example, in mine I use "controls.Ship.Shoot.triggered" instead of "MyInput.Player.Jump.triggered" but it does in fact work it shows false each frame until I click and then it returns true once.

    Code (CSharp):
    1. public class PlayerMovementController : MonoBehaviour
    2. {
    3.     Controls controls;
    4.     void OnEnable()
    5.     {
    6.         if(controls == null)
    7.         {
    8.             controls = new Controls();
    9.          
    10.         }
    11.         controls.Enable();
    12.     }
    13.     void OnDisable()
    14.     {
    15.         if(controls != null)
    16.         {
    17.             controls.Disable();
    18.         }
    19.     }
    20.     private void Update()
    21.     {
    22.         Debug.Log(controls.Ship.Shoot.triggered);
    23.     }
    24. }
    My shoot action is an action type button that's bound to the right trigger and the mouse
    upload_2020-5-28_22-34-16.png
    I'm assuming you've enabled it since it seems like you're saying the movement one works.

    Another option might be to actually implement the action's callback like this:

    Code (CSharp):
    1. public class PlayerMovementController : MonoBehaviour
    2. {
    3.     Controls controls;
    4.  
    5.     bool isShooting = false;
    6.     void OnEnable()
    7.     {
    8.         if(controls == null)
    9.         {
    10.             controls = new Controls();
    11.             controls.Ship.Shoot.started += OnShoot;
    12.         }
    13.         controls.Enable();
    14.     }
    15.     void OnDisable()
    16.     {
    17.         if(controls != null)
    18.         {
    19.             controls.Disable();
    20.         }
    21.     }
    22.     private void Update()
    23.     {
    24.         if (isShooting)
    25.         {
    26.             Debug.Log("Bang");
    27.             isShooting = false;
    28.         }
    29.     }
    30.     public void OnShoot(InputAction.CallbackContext ctx)
    31.     {
    32.         isShooting = true;
    33.         Debug.Log("OnShoot");
    34.     }
    35. }
    36.  
    Line 11 is the important one. "controls.Ship.Shoot.started += OnShoot;" I assign the OnShoot function to the started phase of the Shoot action.

    Obviously replace OnShoot with OnJump and isShooting with isJumping. The OnShoot function above will trigger on the "controls.Ship.Shoot.started" or in your case "MyActions.Player.Jump.started"

    So if you wanted to add a hold action you can separate out the started and canceled actions

    Code (CSharp):
    1. public class PlayerMovementController : MonoBehaviour
    2. {
    3.     Controls controls;
    4.  
    5.     bool isShooting = false;
    6.     void OnEnable()
    7.     {
    8.         if(controls == null)
    9.         {
    10.             controls = new Controls();
    11.             controls.Ship.Shoot.started += OnShootStarted;
    12.             controls.Ship.Shoot.canceled += OnShootCanceled;
    13.         }
    14.         controls.Enable();
    15.     }
    16.     void OnDisable()
    17.     {
    18.         if(controls != null)
    19.         {
    20.             controls.Disable();
    21.         }
    22.     }
    23.  
    24.     private void Update()
    25.     {
    26.         if (isShooting)
    27.         {
    28.             Debug.Log("Bang");
    29.         }
    30.     }
    31.  
    32.     public void OnShootStarted(InputAction.CallbackContext ctx)
    33.     {
    34.         isShooting = true;
    35.         Debug.Log("OnShootStarted");
    36.     }
    37.     public void OnShootCanceled(InputAction.CallbackContext ctx)
    38.     {
    39.         isShooting = false;
    40.         Debug.Log("OnShootCanceled");
    41.     }
    42. }
    43.  
     
  3. lobotomista

    lobotomista

    Joined:
    Jul 21, 2018
    Posts:
    39
    I Did some more experiments, funnily if I hold and press, he jumps and my debug log shows true I have a lot of commented stuff because of my experiments but here's the code:
    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. [DefaultExecutionOrder(-100)]
    8. public class InputProcessor : MonoBehaviour
    9. {
    10.     Inputs MyInput;                                  //New input system reference
    11.     [HideInInspector] public Vector2 Movement;
    12.     [HideInInspector] public float horizontal;      //Float that stores horizontal input
    13.      public bool jumpHeld;         //Bool that stores jump pressed
    14.      public bool jumpPressed;      //Bool that stores jump held
    15.     [HideInInspector] public bool crouchHeld;       //Bool that stores crouch pressed
    16.     [HideInInspector] public bool crouchPressed;    //Bool that stores crouch held
    17.     bool readyToClear;
    18.     Rigidbody2D rb;
    19.  
    20.     // Start is called before the first frame update
    21.  
    22.     private void Awake() {
    23.         MyInput = new Inputs();
    24.     }
    25.    
    26.     void Start()
    27.     {
    28.         rb = GetComponent<Rigidbody2D>();
    29.         // MyInput.Player.Jump.started += _ => jump();
    30.     }
    31.  
    32.     private void OnEnable() => MyInput.Enable();
    33.  
    34.     private void OnDisable() => MyInput.Disable();
    35.     // Update is called once per frame
    36.     void Update()
    37.     {
    38.         ClearInput();
    39.        
    40.         ProcessInputs();
    41.        
    42.         horizontal = Mathf.Clamp(horizontal, -1f, 1f);
    43.     }
    44.  
    45.     void FixedUpdate()
    46.     {
    47.         if (jumpPressed){
    48.             jump();
    49.         }
    50.         readyToClear = true;
    51.     }
    52.  
    53.     void ClearInput()
    54.     {
    55.         if (!readyToClear)
    56.             return;
    57.  
    58.         //Reset all inputs
    59.         horizontal = 0f;
    60.         jumpPressed = false;
    61.         jumpHeld = false;
    62.         crouchPressed = false;
    63.         crouchHeld = false;
    64.  
    65.         readyToClear = false;
    66.     }
    67.  
    68.     void ProcessInputs()
    69.     {
    70.        
    71.         Movement = MyInput.Player.Move.ReadValue<Vector2>();
    72.         //Accumulate horizontal axis input
    73.         horizontal += Movement.x;
    74.  
    75.         //Accumulate button inputs
    76.         jumpPressed = jumpPressed || MyInput.Player.Jump.triggered;
    77.  
    78.         //jumpHeld = jumpHeld || jumpHeld();
    79.  
    80.         Debug.Log(jumpPressed);
    81.         // crouchPressed = crouchPressed || Input.GetButtonDown("Crouch");
    82.         // crouchHeld = crouchHeld || Input.GetButton("Crouch");
    83.     }
    84.     private void jump()
    85.     {
    86.         rb.AddForce(new Vector2(0, 4), ForceMode2D.Impulse);
    87.     }
    88.    
    89. }
    90.  
    BTW that comented assign in the start method made the jumps work as intended. But I just can't make it work on the context of the robbie platformer.
    Another question about crouching since it's part of an axis. How would you recommend aproaching that?
     
  4. Snowdrama

    Snowdrama

    Joined:
    May 10, 2014
    Posts:
    28
    I haven't actually seen this Robbie Platformer project you speak I googled a bit, and I've found a blog post about a game but can't find a project, of so I don't actually know to what context the code is being used hahaha.

    But yeah I see the problem now, since you're using FixedUpdate it can eat the input if the input is cleared before fixed update is called. In this case I think you can avoid ProccessInputs entirely as long as you store the state of the inputs rather than accumulating them, and not poll the input status manually, and then just call clear at the end of FixedUpdate for one time use values. Here's that same code updated with more how I would structure it. I also added horizontal movement

    As for your final question basically you take the input and check the Y value, if Y is below some threshold since Y down is down on an analog stick, then you're crouching. I added it to my example.

    Code (CSharp):
    1. using System;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.InputSystem;
    6.  
    7. public class InputProcessor : MonoBehaviour
    8. {
    9.     [Header("Jump")]
    10.     public bool jumpHeld;         //Bool that stores jump pressed
    11.     public bool jumpPressed;      //Bool that stores jump held
    12.     [Header("Crouch")]
    13.     public float crouchThreshold = -0.6f; //if the Y input is less than this, crouch
    14.     [Header("Movement")]
    15.     public float movementSpeed = 10;
    16.  
    17.     private Inputs MyInput;       //New input system reference
    18.     private Vector2 Movement;     //The input direction.
    19.     private bool crouchHeld;      //Bool that stores crouch pressed
    20.     private bool crouchPressed;   //Bool that stores crouch held
    21.     private Rigidbody2D rb;
    22.  
    23.     // Start is called before the first frame update
    24.     private void Awake()
    25.     {
    26.         MyInput = new Inputs();
    27.         MyInput.Player.Jump.started += OnJumpInput;
    28.         MyInput.Player.Jump.canceled += OnJumpInputCanceled;
    29.  
    30.         //performed is called any time the axis changes.
    31.         MyInput.Player.Move.started += OnMoveInput;
    32.         MyInput.Player.Move.performed += OnMoveInput;
    33.         MyInput.Player.Move.canceled += OnMoveInput; //need to do this for keyboard
    34.     }
    35.  
    36.     void Start()
    37.     {
    38.         rb = GetComponent<Rigidbody2D>();
    39.     }
    40.  
    41.     private void OnEnable() => MyInput.Enable();
    42.  
    43.     private void OnDisable() => MyInput.Disable();
    44.  
    45.     void Update()
    46.     {
    47.         if (crouchHeld)
    48.         {
    49.             this.transform.localScale = new Vector3(1, 0.5f, 1);
    50.         }
    51.         else
    52.         {
    53.             this.transform.localScale = new Vector3(1, 1f, 1);
    54.         }
    55.     }
    56.  
    57.     void FixedUpdate()
    58.     {
    59.         if (jumpPressed)
    60.         {
    61.             Jump();
    62.         }
    63.         Move();
    64.         ClearInput();
    65.     }
    66.  
    67.     void ClearInput()
    68.     {
    69.         //Reset all one time inputs
    70.         //held inputs don't get cleared here
    71.         //they are cleared when the button is released
    72.         jumpPressed = false;
    73.         crouchPressed = false;
    74.     }
    75.     private void Move()
    76.     {
    77.         //I assume you don't want to move horizontally when crouching.
    78.         if (!crouchHeld)
    79.         {
    80.             rb.AddForce(new Vector2(Movement.x, 0) * movementSpeed, ForceMode2D.Force);
    81.         }
    82.     }
    83.  
    84.     private void Jump()
    85.     {
    86.         rb.AddForce(new Vector2(0, 4), ForceMode2D.Impulse);
    87.     }
    88.  
    89.     private void OnMoveInput(InputAction.CallbackContext ctx)
    90.     {
    91.         //this is already a normalized direction vector. No need to clamp X.
    92.         Movement = MyInput.Player.Move.ReadValue<Vector2>();
    93.  
    94.         if(Movement.y < crouchThreshold)
    95.         {
    96.             //if the movement is less than the crouch threshold then the player is holding down
    97.             crouchPressed = true;
    98.             crouchHeld = true;
    99.         }
    100.         else
    101.         {
    102.             //the axis changed and the player is no longer holding down
    103.             crouchHeld = false;
    104.         }
    105.     }
    106.  
    107.  
    108.     private void OnJumpInput(InputAction.CallbackContext ctx)
    109.     {
    110.         //we jump and we started holding
    111.         jumpPressed = true;
    112.         jumpHeld = true;
    113.     }
    114.  
    115.     private void OnJumpInputCanceled(InputAction.CallbackContext ctx)
    116.     {
    117.         //the button was released so we're no longer holding jump
    118.         jumpHeld = false;
    119.     }
    120. }
    121.  
    I added a simple scale crouch, and horizontal movement(when not crouched). Though personally I wouldn't use add force I'd just set the velocity for responsiveness.

    Made a simple scene in my project and slapped this on a random icon sprite and got this:
     
  5. lobotomista

    lobotomista

    Joined:
    Jul 21, 2018
    Posts:
    39