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

Need help with twinstick aiming and PlayerInput in the new Input system.

Discussion in 'Input System' started by Yukken, Jul 30, 2020.

  1. Yukken

    Yukken

    Joined:
    Jul 12, 2015
    Posts:
    93
    My game has twinstick aiming. the player can aim with the mouse and gamepad right stick. Both of them are vector2. But Mouse position is in screen coordinates and needs to be subtracted from the player's postion. On the other hand, gamepad directly gives direction. How do I properly set this up in the input editor besides making them separate?

    I also want to swap controlschemes to gamepad if any gampad key is pressed and vice versa. I am told that PlayerInput does this automatically but I'm unable to get it to work.

    This the code:
    Code (CSharp):
    1. private PlayerInput input;
    2.  
    3.     private void Awake()
    4.     {
    5.         input = GetComponent<PlayerInput>();
    6.     }
    7.  
    8.     private void OnEnable()
    9.     {
    10.         input.onControlsChanged += t;
    11.  
    12.  
    13.         input.actions.FindActionMap("GamePlay").Enable();
    14.         input.actions.FindActionMap("GamePlay").FindAction("Movement").performed += (c) => { Debug.Log(c.ReadValue<Vector2>()); };
    15.         input.actions.FindActionMap("GamePlay").FindAction("Aim").performed += (c) => { Debug.Log("asdasdasdasd" + c.ReadValue<Vector2>()); };
    16.     }
    17.  
    18.     public void t(PlayerInput tt)
    19.     {
    20.         Debug.Log(tt);
    21.     }
    22.  
    23.     private void OnDisable()
    24.     {
    25.         input.onControlsChanged -= t;
    26.     }
    27.    
    These functions are never called when I press buttons in play mode. The debug shows that the keyboard keys are indeed pressed but PlayerInput doesn't do anything,
    Yes, I have C# events selected in the playerinput component. And no, I cannot use unityevents for other reasons.
    The input asset is also set up properly.
    The docs also don't mention anything. What am I doing wrong?

    On the other hand, if I use the generated class from the input asset, everything works properly. However, I am unsure as to how to handle the control scheme transitions.
    Any help?