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.

Button interaction

Discussion in 'Input System' started by SqualLyuk, May 22, 2020.

  1. SqualLyuk

    SqualLyuk

    Joined:
    May 5, 2020
    Posts:
    8
    Hi everyone,
    I'm trying the new input system and I am confused on how it works.
    I have assigned the alt-left as a Button for interaction. To interact with a door I have to assign playerController.OnDisable(); and playerController.OnEnable(); otherwise the if-statement is applied several time in a row and not just once.
    Code (CSharp):
    1. bool openDoor;
    2.     void Update()
    3.     {
    4.        
    5.         if (playerController.interacting != 0)
    6.         {
    7.            
    8.             openDoor = true;
    9.             playerController.OnDisable();
    10.         }
    11.         else
    12.         {
    13.             openDoor = false;
    14.             playerController.OnEnable();
    15.         }
    16.  
    17.         if (openDoor)
    18.         {
    19.             CheckInteract();
    20.         }
    21.        
    22.  
    23.     }
    Do I need the playerController.OnEnable(); and playerController.OnDisable(); lines? Shouldn't be automatic by having Interact as a Button?

    This is the part in the PlayerController script which is called:
    Code (CSharp):
    1.  
    2. public class PlayerController : MonoBehaviour, PlayerInput.IPlayerActions
    3. {
    4.   private Vector2 moving;
    5.     private Vector2 looking;
    6.     public float interacting;
    7.     private float running;
    8.     private PlayerInput keyboardControls;
    9.    
    10.     void Awake()
    11.     {
    12.         keyboardControls = new PlayerInput();
    13.        /* keyboardControls.Player.Move.performed += ctx => OnMove(ctx);
    14.         keyboardControls.Player.Look.performed += ctx => OnLook(ctx);
    15.         keyboardControls.Player.Jump.performed += ctx => OnJump(ctx);
    16.         keyboardControls.Player.Jump.performed += ctx => OnRun(ctx);*/
    17.         keyboardControls.Player.SetCallbacks(this);
    18.        
    19.        
    20.     }
    21. /*...*/
    22. public void OnEnable()
    23.     {
    24.         keyboardControls.Player.Enable();
    25.        
    26.        
    27.  
    28.     }
    29.  
    30.     public void OnDisable()
    31.     {
    32.        
    33.             keyboardControls.Player.Disable();
    34.      
    35.        
    36.     }
    37.  
    38.     public void OnMove(InputAction.CallbackContext context)
    39.     {
    40.  
    41.         moving = context.ReadValue<Vector2>();
    42.        
    43.  
    44.     }
    45.  
    46.     public void OnLook(InputAction.CallbackContext context)
    47.     {
    48.      
    49.         looking = context.ReadValue<Vector2>();
    50.     }
    51.  
    52.     public void OnInteract(InputAction.CallbackContext context)
    53.     {
    54.        
    55.        interacting = context.ReadValue<float>();
    56.     }
    57.  
    58.     public void OnRun(InputAction.CallbackContext context)
    59.     {
    60.        
    61.         running = context.ReadValue<float>();
    62.      
    63.     }
    64. }
     
  2. SqualLyuk

    SqualLyuk

    Joined:
    May 5, 2020
    Posts:
    8