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

Question Coordinating control between multiple input systems.

Discussion in 'Input System' started by Capricornum, Sep 13, 2020.

  1. Capricornum

    Capricornum

    Joined:
    Feb 1, 2018
    Posts:
    22
    Dear Community,

    In my game I have different input classes.

    - one for dragging objects around with left mouse button
    - one for moving the camera with middle mouse button
    - one for highlighting objects with right mouse button
    - one for instantiating objects on different keypresses
    - a Menu canvas with different buttons for left mouse button
    - a button that highlights when hovering over it with the mouse.

    Some of these input classes should only process input when others are not processing. For example I don‘t want to allow instantiation while dragging. Or not allow moving the camera while on the menu Or just highlight the button when not dragging etc.

    Unfortunately I am at a loss. How would I go about allowing and disallowing these input classes when neccessary?

    Kind regards.
     
  2. djweaver

    djweaver

    Joined:
    Jul 4, 2020
    Posts:
    105
    Sounds like you need to track the state of some of these actions. The input system already does this with the phases, started, performed, and canceled, however, you may need to abstract these out to a higher level that can be accessed by any of the classes at any time. You could do this with a static class that stores state information, like bool isOnMenu, or isDragging, and perform a check before the appropriate function you want to block to see if that state is true.
     
    Capricornum and SplunkyDev like this.
  3. Capricornum

    Capricornum

    Joined:
    Feb 1, 2018
    Posts:
    22
    Thanks djweaver. That is very helpful. Just to clarify. In my static class I have a bool isDragging. In my Instantiating class I have
    void Update { if (Input.GetKeyDown(KeyCode.A) && !StaticManager.isDragging) blablabla }


    What do you think of an event system, where I fire events like DragStarted or MenuLoaded or CameraSetInMotion. In the other scripts I then subscribe to the events and start and stop coroutines listening for input accordingly.
     
  4. djweaver

    djweaver

    Joined:
    Jul 4, 2020
    Posts:
    105
    I'm afraid that I don't know enough about the advantages/disadvantages of using an event system outside of the context of the new input system I mentioned (ie. using started/performed/canceled checks in callbacks). I've been noobing around these forums trying to figure it out but no bites yet lol. I mean, I get the whole concept of using events for other things, but when it relates to input specifically, I don't really understand when or why it would be beneficial to deviate from setting booleans that represent state in the input action callbacks like I suggested. Wish I could 'splain it to you, but I need someone to 'splain it to me first. There just aren't any good resources that I can find that cover it, but if you are using old input system, I believe if you lurk around youtube and discord you could probably find some tried and true design patterns like observer pattern. Jason Weimann and Infallible Code are my go-to channels on yt. Weimann covers a lot about events and typically uses the old input system to implement most, if not all of his examples. And of course there is probably a Unite lecture or two covering it as well if you wanna go down the rabbit hole. Haha sorry, wish I could help you with tangible advice, but I'm too green at both unity and C# :rolleyes:
     
    Capricornum likes this.
  5. Capricornum

    Capricornum

    Joined:
    Feb 1, 2018
    Posts:
    22
    Man. You are more of a help than you know. You help me view my options, get things clearer in my head. All your input is well appreciated!
    I did have a quick look at the new input system. I just wasn't up for the task of learning something new that I can do with my own code already. Also I didn't see an option for disabling or enabling other inputs.
    Thanks also for the research suggestions. I will have a look at those. Let's see what I can come up with.
     
    djweaver likes this.