Forum rules - please read before posting.

I found a way to detect when a controller is connected and automatically switch to/from it using AC

edited May 2020 in Extending the editor

Intro

I really wanted a way for Unity and AC to detect when a controller is plugged in/ used and automatically switch to a fully controller based input method (and vice versa) on the fly - So if the player presses any button/ moves the stick on the controller, AC recognizes that and switches out the graphics for tutorials/ QTE prompts etc on the fly, and if the user presses a key on the keyboard/ moves the mouse, the QTE icons, cursor controls etc are switched to those for a Keyboard/ mouse scheme.

It _is _possible to use the mouse to control the cursor even when the input method is set to only Keyboard or Controller, but that doesn't work as well as using the Mouse and Keyboard setting for the cursor sensitivity/ accuracy using the mouse. For AC menus to be directly-navigable, the input method also needs to be set to purely Keyboard or Controller. For my previous game, I had an option where the player had to go in a menu to select the control scheme they wanted to use - this of course is cumbersome and isn't ideal at all.

In any case, there are many reasons for wanting your game to be able to detect whether a player is playing on a controller or on the keyboard, and this method will help with that!

Apologies if this is common knowledge, but I could never find any way to do this while looking it up. Searching for just plain Unity threads on the topic also led nowhere and wasted time. I'm also not a programmer, so this may seem like a really roundabout way of doing things, but it works! :) (This also means this should be easy for non-programmers to follow!)

I've also used Rewired as my Input manager - I'm not sure it'll work without Rewired, but you can try! Within AC, I mainly used Active Inputs and Global Variables together to make this work. The specifics of the control schemes will also differ according to each project's needs, but the main solution should remain the same.


Guide

  1. Scripts for switching input methods

First of all, you'll need basic scripts to change the AC input methods. I found these scripts somewhere on AC forums for my previous game, not sure where exactly. The exact settings for these input schemes may differ from project to project. Also remember that right clicking on any AC setting in Unity reveals code for that setting, which you can use to customize the settings in these scripts.

Keyboard and mouse: https://pastebin.com/x5MxEF5H
Keyboard or Controller (this is used for when you want to play with a controller) : https://pastebin.com/GLXsDhJy
KB or Controller WITH cursor (this is if you want to control the cursor with the right stick) : https://pastebin.com/DW6E39eH

Remember, you may want to mix and match and affect other settings depending on your game. I have two additional scripts, as my game also switches between third and first person.

Once you have your scripts set up, save them in your Custom Actions directory. You can read how to do that in the second half of this AC tutorial: https://adventurecreator.org/tutorials/writing-custom-action


  1. Setting up Active Inputs to detect control scheme being used

Now come the fun bits. We'll set up the Adventure Creator Active Inputs so that any input/ button press/ stick or mouse movement will be detected, and this will run an action list that switches the control method. So for the player, it will seem that they can just plug in and pick up the controller, and when they randomly move the sticks or press a button to check whether the game supports controllers (this is something I always do) the Active Input will react to that and switch the control scheme. If the player decides to go back to the Keyboard & Mouse method, the Active Input will react to that and run a different action list, switching the scheme to KB&M.

Here's how to set up the Active Inputs for this:

Link to image if it doesn't load - https://imgur.com/a/vyTYdRf

Similarly, create an Active Input for KBM using similar settings as above, but create a NEW Action List for ActionList when triggered setting (So, "ActiveInput_Keyboard" for instance, instead of the "ActiveInput_Controller" as seen in the picture.) Change the Label **and **Input button settings accordingly as well.

Now, I don't know how having these Action Lists running ALL the time would affect performance etc. One method is to use Global Variables to set and detect the current input method so that it doesn't keep switching to the same method every time a key is pressed. But for this variable to be detected, we do need some Action List to house this check (I use the same one that's triggered upon key press)

For my game, I only need to switch input methods at particular points in the game. These are points when the cursor becomes visible for mingames, controlling an in-game computer, and when opening up menus. Because of this, I've been able to keep both these Active Inputs disabled (unchecked Enabled by default? setting) and I only enable them in Action Lists (Input->Toggle Active->New State) when I need them (when starting the minigame, opening a menu etc) and then disable them again once this section is finished.

If anyone has a better solution (or if you think it's not really bad to have it enabled all the time) do let me know, and I'll add it here! :)

Note - For my game, in its default state it runs on the "Keyboard or Controller" setting - there is no cursor in general gameplay, and for this case either the keyboard or the controller can be plugged in and used by default. When a menu opens up, the Active Input is enabled and the above action list is run so that you can control it with any device. Same for minigames.

So of the 3 scripts I pasted above, I've only used the "Keyboard and Mouse" and the "Controller with cursor" scripts in the Action Lists below. This is because like I said, my game is in the "Keyboard or Controller" method by default, and I switch to that manually within Action Lists when I'm done with the dynamic switching parts, after disabling the Active Inputs.


  1. Setting up the Action Lists

Now we'll set up the Action List that's run through the Active Input.

For Keyboard and Mouse:

Link to image - https://imgur.com/a/LPjwZWF

For Keyboard or Controller:

Link: https://imgur.com/ktIz6VV

The Action List above is quite self explanatory, but let me know if you have questions. I haven't used a variable in the Keyboard or Controller Action List because by default my game is always in that method. So when I'm done with the section that requires switching ability (the parts where the Active Input is Enabled, I tell the game to return to the default Keyboard or Controller setting after disabling the Active Inputs. If I had the variable check in this Action List as well, the condition would always be met (My "Current Input Method" variable would always = "Controller or Keyboard") and nothing would happen.

What I mean by this is - this is again something particular to my game, and you'll have to check how this works for your project setting.

I've removed a few customized settings here, but in my own Action List, I have a variable checking whether the current scene is in first person or third person, and running the appropriate script for that. So this way this method turns out to be quite handy to adjust a lot of player control settings on the fly!

Continued in the comments-


Comments

  • edited May 2020
    1. Using Rewired to bring it together

    Like I said, I've only tried this with Rewired, and I have no idea whether this will work with Unity's basic Input manager, Unity's new input manager, or something like InControl - this is something you'll have to experiment with.

    To keep things clean, we're going to create a new "Map Category" in Rewired. Name this anything - I've named this category "Control Switching".


    Link - https://imgur.com/dteWeCZ

    Then, we need to create two new "Action Categories". Create two new categories: "Controller Switch" and "Keyboard Switch" (or any other name your prefer)


    Link - https://imgur.com/5D1qKf3

    Move to the "Actions" tab. In these new categories, we need to create one new "Action" in each. These "Actions" are the same thing as the Inputs in Unity's Input (So inputs/ Actions like InteractionA, Horizontal, Jump etc)

    For reference, this is what the actions in the "Default" category look like:


    Link - https://imgur.com/r7i6xKQ

    Inside the new Action categories, create one new "Action" - "Controller Switch to" in the "Controller Switch" category, and "Keyboard Switch to" in the "Keyboard Switch" category. The Action name here must be the same as the name you entered in the Input Button field in AC's Active Inputs. Here's a picture showing the settings for the Keyboard Switch category. Do the same for the Controller Switch category and replace the respective names.


    Link - https://imgur.com/QdaloH1

    Finally, go to your Joystick Maps (and Keyboard and Mouse Maps) and set it up so that pressing any relevant button/ player input leads to the "Controller Switch to" or "Keyboard Switch to" Actions getting activated. Choose the previously created "Control Switching" Map Category, and add button presses (Elements), choose the "Controller Switch"/"Keyboard Switch" Action Category in the Element Properties and connect them to the new "Controller Switch to"/"Keyboard Switch to" Action.

    For Controllers and Mouse, this is easy - they have a limited number of buttons/ inputs that a player will usually try out to see if the game detects the input, so all the buttons and movements can be added as Elements connected to the corresponding Action. For Keyboard, set it up so that the most commonly used keys trigger the Action (keys like Space, movement keys, Enter etc) Here's how I've set mine up:

    Controller Map:

    Link - https://imgur.com/MzFHKii

    Keyboard Map:

    Link - https://imgur.com/j94YKUn

    Mouse Map:

    Link - https://imgur.com/gnXdt4C

    And that's it! Now whenever you press any key (as defined by you), as long as the Active Inputs are enabled, it should run the appropriate action list and switch your control method! You can even add a message using a menu that tells the player that the game has switched to a controller etc.

    Side note: I also think that Rewired takes over your Inputs and so you don't need to define anything in Unity's Input settings. In my game, having an Input for CursorHorizontal and CursorVertical created issues - using my dualshock 4 controller, my character would start spinning - this is because the axis for the right stick and the Left Trigger buttons in DS4 and Xbox controllers are the same. The solution for this was to delete the CursorHorizontal and CursorVertical inputs from Unity's Input settings and only define them in Rewired. This way, any controller can be plugged in and switched between, and all devices will work correctly.

    Like I said, this may not be the most optimized method, and it won't be directly applicable for all projects, but hopefully it helps in some way! Let me know if you guys know any better ways of doing this. So far for me, this works perfectly and I'm thrilled about it :)

  • Firstly I want to say thanks to Armaan for this brilliant guide and the creative thinking for getting to this solution.

    Secondly, a couple of notes:

    I believe this guide is missing a step, you will need to assign the joystick, keyboard and mouse maps to your player under the Players category in Rewired.

    See here: https://i.imgur.com/MjCDciw.png

    Also, to speed up the process of assigning joystick, keyboard and mouse maps to their new actions you can copy the default map to the new control switching category using the 'copy map' button. The you just have to click each input in the new map and switch its action to the relevant controller or keyboard switching one.

    https://i.imgur.com/iVCyfrI.png

    And finally, an alternate approach to the 'Controller Switch To' and 'Keyboard Switch To' active inputs.

    https://i.imgur.com/0SK41kw.png

    Set both active inputs to active when the game starts.

    For the Controller Switch To actionlist:

    Disable the Controller Switch To active input (using Input-Toggle active and untick the box)

    Enable the Keyboard Switch To active input (same method as above but tick the box)

    Run your controller switch custom action

    Set global variables for ControllerActive = true and KeyboardActive = false to keep track of things.

    Then for the Keyboard Switch To actionlist do the opposite.

    This way only the input method you're not using is waiting for input at any one time, therefore preventing this action from running everytime you press an input on the current device.


    I hope that makes sense, thanks again Armaan!

  • @Chicky I love that addition! I was a bit worried about the game constantly running an action list with every tap of the currently active input method, but this fixes this by only looking out for the one that is currently inactive. Will add this to my game asap! :D

  • edited October 2021

    If you're using Rewired you can use this script I wrote to detect when an input device is used and trigger an actionlist. The setup in the example script is if a control pad, or mouse & keyboard is used:

    https://adventurecreator.org/forum/discussion/11838/rewired-integration-detect-input-change-and-run-actionlist-script#latest

    This means you don't have to touch active inputs.

Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Welcome to the official forum for Adventure Creator.