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

New Input System Correct Usage

Discussion in 'Input System' started by Korov, Apr 15, 2020.

  1. Korov

    Korov

    Joined:
    Apr 17, 2017
    Posts:
    3
    Few questions about how to use new Input System.
    The first example and question: Is it safe to store ctx in floats/vector2 and use them in other functions, also in the fixed update?

    (this is just a part of code)

    Code (CSharp):
    1. void Awake(){
    2. myControls = new Controls();
    3. myControls.Player.Move.performed += HandleMove;
    4. }
    5.  
    6. private void HandleMove(InputAction.CallbackContext ctx)
    7.     {
    8.      
    9. valueX = ctx.ReadValue<Vector2>().x;
    10. valueY = ctx.ReadValue<Vector2>().y;
    11.     }
    12.  
    13. private void MoveForFixed(){
    14. rb.velocity=new Vector2(valueX*speed,valueY*speed);
    15. }
    16.  
    17. void FixedUpdate(){
    18. MoveForFixed()
    19. }
    The second question is about processing events. Go to project settings->Input System Package
    There is Update mode, by default it is Dynamic Update, what If is set to Process Events in Fixed Update.
    Usually, when getting input from Fixed Update, you will miss input and that is why Input detection was in Update(old input system), so is here something different, what is the usage and benefit from Process Events in Fixed Update.
     
    Last edited: Apr 15, 2020
  2. nickleplated

    nickleplated

    Joined:
    Nov 2, 2018
    Posts:
    26
    It is not safe to store ctx and use it outside of the callback. However, that's not what your code is doing. You are getting the x and y out of the context and storing those, which is fine.
    You may want to change it to make one call to ReadValue, though, and then get the x and y from the result or just store the Vector2 whole.

    e.g.
    Code (CSharp):
    1. var move = ctx.ReadValue<Vector2>();
    2. valueX = move.x;
    3. valueY = move.y;
    4.  
    5. // or just store the vector2 in a field
    6. m_move = ctx.ReadValue<Vector2>();
    7.  
    8. // then in your move method
    9. rb.velocity= m_move * speed;
    Sorry, I don't know about your second question. I would guess that if you need inputs to be processed before each FixedUpdate, you would switch it to that.
     
    Korov likes this.
  3. Korov

    Korov

    Joined:
    Apr 17, 2017
    Posts:
    3
    Thank you!