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

Bug Player Input component not sending messages to script on same object

Discussion in 'Input System' started by afrederick, Aug 11, 2023.

  1. afrederick

    afrederick

    Joined:
    Jul 17, 2017
    Posts:
    5
    Player Input component on game object. I've tried explicitly setting the default scheme to KeyboardMouse and Gamepad, but neither changes the results.
    upload_2023-8-10_20-46-21.png

    My PlayerInputModule script above. All of this code worked yesterday. The only thing I changed was that I set up control schemes in the input actions asset. I added the OnMove function for debugging.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. [RequireComponent(typeof(PlayerInput))]
    7. public class PlayerInputModule : MonoBehaviour, IPlayerModule
    8. {
    9.     // TODO: Add in charged fire action, and crouch
    10.     //[SerializeField] private bool _analogMovement;
    11.     private PlayerInput _playerInput;
    12.     private InputAction _moveActionValue;
    13.     private InputAction _sprintActionValue;
    14.     private InputAction _lookActionValue;
    15.     private InputAction _jumpActionValue;
    16.     private InputAction _interactActionValue;
    17.     private InputAction _fireActionValue;
    18.     private Vector2 _moveVector;
    19.  
    20.     //public bool AnalogMovement { get { return _analogMovement; } }
    21.     public Vector2 MoveInputVector { get { return _moveActionValue.ReadValue<Vector2>(); } }
    22.     public Vector2 LookInputVector { get { return _lookActionValue.ReadValue<Vector2>(); } }
    23.     public bool SprintPressedThisFrame { get { return _sprintActionValue.WasPressedThisFrame(); } }
    24.     public bool JumpPressedThisFrame { get { return _jumpActionValue.WasPressedThisFrame(); } }
    25.     public bool InteractPressedThisFrame { get { return _interactActionValue.WasPressedThisFrame(); } }
    26.     public bool FirePressedThisFrame { get { return _fireActionValue.WasPressedThisFrame(); } }
    27.     public string CurrentControlScheme { get { return _playerInput.currentControlScheme; } }
    28.  
    29.  
    30.     private void Awake()
    31.     {
    32.         Initialize();
    33.         Debug.Log(_playerInput.currentActionMap.enabled);
    34.     }
    35.  
    36.     private void Update()
    37.     {
    38.         if (Keyboard.current.wKey.wasPressedThisFrame)
    39.         {
    40.             Debug.Log("w key pressed");
    41.         }
    42.     }
    43.  
    44.     public void Initialize(Player player)
    45.     {
    46.         _playerInput = GetComponent<PlayerInput>();
    47.     }
    48.  
    49.     public void Initialize()
    50.     {
    51.         _playerInput = GetComponent<PlayerInput>();
    52.  
    53.         _moveActionValue = _playerInput.actions["Move"];
    54.         _sprintActionValue = _playerInput.actions["Sprint"];
    55.         _lookActionValue = _playerInput.actions["Look"];
    56.         _jumpActionValue = _playerInput.actions["Jump"];
    57.         _interactActionValue = _playerInput.actions["Interact"];
    58.         _fireActionValue = _playerInput.actions["Fire"];
    59.     }
    60.  
    61.     public void OnMove(InputValue value)
    62.     {
    63.         _moveVector = value.Get<Vector2>();
    64.         Debug.Log(_moveVector);
    65.     }
    Shouldn't the OnMove function get called when I hit any of the WASD keys?

    Here's my action map. I have two control schemes set up, and I've ticked all the boxes for each binding.

    upload_2023-8-10_20-47-46.png

    For context, I also have the Unity Standard Assets first person controller in the project. That works fine in a different scene. Right now, the scene that's giving me the problem has only this object, and the minimum other setup (camera, a plane and some walls).

    I'm stumped. Anyone see what I'm doing wrong?
     
  2. DevDunk

    DevDunk

    Joined:
    Feb 13, 2020
    Posts:
    4,497
    Do you need to set the default scheme maybe?

    You could also switch to UnityEvents instead of Send Messages. That way it is (in my opinion) easier to work with and clearer what exactly gets called.
     
  3. afrederick

    afrederick

    Joined:
    Jul 17, 2017
    Posts:
    5
    I tried explicitly setting the default scheme and it doesn't change anything. Now I'm trying UnityEvents. It should be calling this little MoveEventTest function, but I'm not seeing anything in the console.

    upload_2023-8-11_11-25-11.png

    Code (CSharp):
    1. public void MoveEventTest()
    2.     {
    3.         Debug.Log("MoveEventTest called");
    4.     }
     

    Attached Files:

  4. afrederick

    afrederick

    Joined:
    Jul 17, 2017
    Posts:
    5
    I deleted both control schemes, while keeping all the bindings exactly the same, and now everything works. I'm happy for that, but it would be great to understand why. Other than ticking the correct box for each binding in the action map asset, what else is required?
     
    DevDunk likes this.