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

Is there a way to combine similar actions( e.g 1-9 to select control groups?)

Discussion in 'Input System' started by AurelWu, Mar 31, 2019.

  1. AurelWu

    AurelWu

    Joined:
    Oct 11, 2013
    Posts:
    26
    Lets say I want to use numeric 1-9 to select control groups or toggle weapons or whatever. Do I need to define a action for each single one of those or can I somehow just create 1 action and pass the pressed key as parameter?
     
  2. Rene-Damm

    Rene-Damm

    Joined:
    Sep 15, 2012
    Posts:
    1,779
    You can bind the same action to arbitrary many controls and then check the control in the callback. For something that's specific to just keyboard, you could do

    Code (CSharp):
    1. void MyActionCallback(InputAction.CallbackContext context)
    2. {
    3.     var key = ((KeyControl) context.control).keyCode;
    4.     // ...
    5. }
     
    AurelWu likes this.
  3. AurelWu

    AurelWu

    Joined:
    Oct 11, 2013
    Posts:
    26
    thanks a lot, that works perfectly (and I probably could have found out myself sorry!)