Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Value events not firing constantly

Discussion in 'Input System' started by lorenalexm, Jan 25, 2021.

  1. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    Began playing around with the "new" InputSystem in Unity, and I have hit quite the roadblock. I created a fresh 3D project from Hub, and imported the InputSystem from the Package Manager. Followed along with the Quick Start Guide and noticed that the input events were firing sporadically - as seen in the gif below. This is with Keyboard & Mouse as the input; I do not have a controller accessible at the time to test. Keys were held down, in hopes of receiving constant events as a Value Action is supposed to provide.

    InputSystem Errors.gif

    For testing purposes the code is dead simple, and should not be a bottleneck causing actions to fire slowly. The
    Debug.Log
    statement was added after the fact to see if a steady stream of events were coming in. They were not.

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.InputSystem;
    3.  
    4. public class PlayerScript : MonoBehaviour
    5. {
    6.     [SerializeField]
    7.     private float _speed = 200f;
    8.     [SerializeField]
    9.     private Rigidbody _rigidbody = null;
    10.  
    11.     public void Start()
    12.     {
    13.         if(_rigidbody == null)
    14.         {
    15.             Debug.LogError("Rigidbody is null! Please set in inspector");
    16.             return;
    17.         }
    18.     }
    19.  
    20.     public void InvokeOnMove(InputAction.CallbackContext context)
    21.     {
    22.         Debug.Log($"InvokeOnMove method called at { System.DateTime.UtcNow }");
    23.         if (_rigidbody == null)
    24.         {
    25.             Debug.LogError("Rigidbody is null! Please set in inspector");
    26.             return;
    27.         }
    28.  
    29.         var inputValues = context.ReadValue<Vector2>();
    30.         var force = new Vector3(inputValues.x, 0, inputValues.y);
    31.         _rigidbody.AddForce(force * _speed);
    32.     }
    33. }
    34.  
    I have tried using both Invoke Unity Events and Send Messages as the behavior type - with properly named methods - neither have worked. The inspector seems otherwise to be setup fine?

    InputSystem Inspector.png

    As I am attempting to apply force to a rigidbody so I have the InputSystem setup to fire events within the FixedUpdate loop. I also double checked that the Value Action type was indeed selected.

    InputSystem Manager.png
    InputSystem Actions.png

    Am I missing something simple here? Is there another way this needs to be accomplished? I appreciate any help in getting this resolved. Thank you!

    Unity Version: 2019.4.8f1
    InputSystem Version: 1.0.2
     
  2. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    This is "working" by writing the
    InputValue.Get<Vector2>()
    to a property, and then using that property within the
    FixedUpdate
    loop. As the property is only updated when events are received, it seems to function fine while holing a key for input.

    Is this how it is supposed to be done? Have I either missed something, or misunderstood the documentation to how Value events are handled?
     
  3. dval

    dval

    Joined:
    Jul 25, 2012
    Posts:
    24
    With the Player Input set you have, what is calling InvokeOnMove()?
    If you change the name of that method to OnMove() it will be called by Player Input.
    Because you have an Input Action named "Move", it will call message/event whatever "OnMove".
    This is what you should respond to.
    Your component should also receive "OnLook" and "OnFire" events with that Player Input setup.
     
  4. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    I appreciate you replying to me @dval ! As the question was first raised over a year ago, I am quite a bit fuzzy on how exactly I had setup the project for learning the new InputSystem. I would have to assume that I had the event specifically set within the inspector, negating the need to comply with the methods name matching the InputAction name.

    All in all the issue came down to my misunderstanding of the new InputSystem compared to the old. I was too accustomed to receiving input values ever frame/physics step, that I never stopped to consider that I would only need to respond to new values from the InputAction and could just use the old value until a new was received.
     
    dval likes this.
  5. dval

    dval

    Joined:
    Jul 25, 2012
    Posts:
    24
    Oi, I just looked at the month!

    Sorry for the necro.
     
  6. lorenalexm

    lorenalexm

    Joined:
    Dec 14, 2012
    Posts:
    307
    No worries, I appreciate you trying to assist!