Search Unity

Player Input component

Discussion in 'Input System' started by Nyxal_Indie, Mar 27, 2020.

  1. Nyxal_Indie

    Nyxal_Indie

    Joined:
    Jun 26, 2019
    Posts:
    179
    Hi guys, i'm looking to the new input system and didn't understand a thing. In this video
    it's used Player Input Component meanwhile here
    a script is created to listen to the input events. What am i missing, what should i use to handle the inputs?
    Thanks :)
     
  2. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    Hello,

    There is multiple ways to have a stable input system with this API.
    You can use the Player Input component to send message (or invoke events if your prefer the editor) to scripts attached to the same GameObject.

    Or you can use the auto-generated C# class, in which case you need to have a reference to it in your scripts.

    I just got my head around it a few hours ago.
     
    Nyxal_Indie likes this.
  3. Nyxal_Indie

    Nyxal_Indie

    Joined:
    Jun 26, 2019
    Posts:
    179
    Love you so much. Definitely going for the Player Input component :D
     
  4. Nyxal_Indie

    Nyxal_Indie

    Joined:
    Jun 26, 2019
    Posts:
    179
    Hey, i got a doubt and hope you can help me. I tried using the Player Input component for the movement of my player and i don't know if it's ok. I have attached the Player Input component and the PlayerMovement script. What i did is: inside Player Input component put the InputActionsAsset i created for the commands (Action name: Movement) and as behavior call Unity Events. Inside Unity events i found the action Movement and put the player object and calling the method Move inside the PlayerMovement script. The code for Move is:
    Code (CSharp):
    1. public void Move(InputAction.CallbackContext context) {
    2.  
    3.         movement = context.ReadValue<Vector2>();
    4.  
    5.     }
    6.  
    7.     void FixedUpdate() {
    8.  
    9.         rigidBody2D.AddForce(movement * speed, ForceMode2D.Impulse);
    10.  
    11.     }
    It's working but do you think is this a good way to go?
     
  5. Monique_Dumont

    Monique_Dumont

    Joined:
    Feb 18, 2020
    Posts:
    41
    Well I think you should keep FixedUpdate in the class scope, but apart from that it seems to be good !
     
    ProceduralCatMaker likes this.