Changing Action Maps with Unity's "New" Input System

Поділитися
Вставка
  • Опубліковано 10 лип 2024
  • Changing action maps with Unity's new input system is easy and allows precise control of which inputs are being listened to - switching between a 3rd person controller, vehicle controller or UI buttons. An input manager can provide a static instance of an input action asset as well as centralized control of toggling action maps making implementation easy and error-free.
    Version 1.0.2 of the input system was used along with Unity 2020.3
    Assets Used (affiliate links)
    ******************************
    Low Poly Farm: assetstore.unity.com/packages...
    Male Peasant Modular Pack: assetstore.unity.com/packages...
    Want to support the Channel?
    *******************************
    Shop the Unity Asset Store (affiliate): assetstore.unity.com/top-asse...
    Become A Channel Member: / @onewheelstudio
    Or a Patron: / onewheelstudio
    Grab a T-Shirt: onewheelstudio.com/merch/
    My video and streaming kit: kit.co/onewheelstudio/tutoria...
    Buy a Game Design Books: kit.co/onewheelstudio/game-de...
    Other Links
    ************
    Discord: discord.onewheelstudio.com
    Twitter: / onewheelstudio
    Devlog: onewheelstudio.com/
    Twitch: / onewheelstudio
    Timestamps
    *************
    0:00 Before watching!!
    0:46 Why Switch Action Maps?
    1:28 Fixing a Problem
    2:08 Input Manager
    3:51 Implementation Example
    4:20 Action Maps
    4:24 Tractor Controller Class
    5:31 Final Thoughts
    6:37 THE END
    #UnityInputSystem #GameDevelopment #Unity3D #indiedeveloper

КОМЕНТАРІ • 51

  • @OneWheelStudio
    @OneWheelStudio  3 роки тому +3

    Changing Action Maps Blog Post: onewheelstudio.com/blog/2021/6/27/changing-action-maps-with-unitys-new-input-system

  • @hiurro
    @hiurro 2 роки тому +17

    If anyone is trying to follow this and getting stuck at defining the event Action you have to add using.system in order to access Action. The top of the code is cut off in the screenshot so if you're new to C# like me or just forgot that could slip you up.

    • @OneWheelStudio
      @OneWheelStudio  2 роки тому +1

      Thanks! I'm sure you aren't the only one.

  • @nikescar
    @nikescar 3 роки тому +1

    Nice! I use a different system but looks like this works too

  • @seppukun208
    @seppukun208 2 роки тому +1

    Cool. I too was thinking of making some kinda centralised input controller.
    I foresee too much of a problem with say when you go into or out of a pause menu and the menu controller toggling the appropriate action maps on and off, which in turns also disables the controls for all the other controllers. It maybe be a bit coupled but should be fine for 90% of situations.

  • @OfficialMageHD
    @OfficialMageHD 3 роки тому +1

    Ayyy new vid to check out!
    Oh also, don't know if you remember I was talking about a weird issue with button on hold in the 2021.2 beta. Not sure about exactly what was going on, but looks like as of now value is what's needed to get on hold function to work right at least for clicking with a mouse.

    • @OneWheelStudio
      @OneWheelStudio  3 роки тому

      Good to know. I wonder if it's a bug or a change in functionality?

    • @OfficialMageHD
      @OfficialMageHD 3 роки тому

      @@OneWheelStudio I hadn't used value in the version released for 2021.1, so I went and checked it real quick and also took a quick check back on your other video, and it seems like value has been reworked to be a bit more like button, and button seems broken in the way value performed if set in 2021.1 how I have it set in 2021.2b.
      I'm using visual scripting mostly so this might be helping get a bit more info as well, although I'm not very knowledgeable about how stuff is working behind the scenes so might be talking out of my ass here lol. You said that value calls started, performed, and canceled when a value changes. If I set value and have the input system event set for on hold, it'll send two pulses down the flow graph. So perhaps before it was giving one pulse for start, second pulse since it changed value, and after that stalling until release. Not sure if it also sends a third pulse on release due to having states setup to switch on release so I'll need to test that more.
      In 2021.2b I believe having the action type set to button, and using the on hold event did about the same as that, however it's been a few days since I've really paid attention to see if it was down to sending two pulses as well. Value on the other hand now continues on hold until release.
      Without digging deeper it's hard to say if they just swapped value and button function or not. Maybe they're planning to remove button and leave it as value and passthrough, while giving value the ability to function as button did. I'll do some more testing tomorrow to see if I can really narrow it down more then take it to the forums, that'll give me an excuse to avoid working on UI haha

  • @isjesse1
    @isjesse1 2 роки тому +1

    I don't know what it is, but your videos are so clear and easy to understand

    • @OneWheelStudio
      @OneWheelStudio  2 роки тому

      I'm glad to hear the videos are clear and helpful. 15+ years of classroom teaching are worth something ;)

  • @Renegen1
    @Renegen1 2 роки тому

    Thank you, you convinced me to use an InputManager.
    Is there a way to subscribe input action events in the InputManager rather than the individual script controllers, though?

  • @blenderstuffs
    @blenderstuffs 3 роки тому

    Amazing.

  • @Betruet
    @Betruet Рік тому

    Great tutorial. Although I really wanted to know how to handle local multiplayer with vehicles.. still trying to figure it out. Thanks so much for the great content

    • @OneWheelStudio
      @OneWheelStudio  Рік тому

      I've got a video on local spilt screen using the new input system. Maybe it'll help? ua-cam.com/video/l9HrraxtdGY/v-deo.html

    • @Betruet
      @Betruet Рік тому +1

      @@OneWheelStudio ah yeah but the vehicle action map swapping is the real issue. Regardless I'll take a look at your video it might give me a new idea, thanks :)

    • @OneWheelStudio
      @OneWheelStudio  Рік тому +1

      @@Betruet Hmm. Yeah okay. Makes sense. Happy to help brainstorm solve this one if you want to come on by the OWS discord. Sounds like an interesting challenge.

  • @aaaalord
    @aaaalord 3 місяці тому

    hi , there is issue like when i toggleActionMap my player input still works! why this happens ? thanks

  • @rayrain7055
    @rayrain7055 2 роки тому

    Model color darkens when exporting to FBX file

  • @whatdymean108
    @whatdymean108 Рік тому

    Hello, I am using the unity template for a Third Person Shooter game and I don't quite know how to address a certain problem with switching action maps.
    The sprinting behavior is defined as a Pass Through Action Type and using Invoke Unity Events it simply negates the value of "_sprint" parameter in the player controller.
    Now the problem is that I have defined another Action Map for the UI and if I hold Shift to run while opening the inventory (and switching to UIActionMap) the value of "_sprint" remains true.
    Closing the inventory and switching back to the default Action Map results in the inversion of the Shift key behavior (Sprint by default, hold to walk)
    So my question is: Is there any way to tell the engine to assume KeyUp event on shift key (or every key in general) when switching Action Maps or is there a "cleaner" way to address the behavior of keys that need to be hold for a certain action?

    • @OneWheelStudio
      @OneWheelStudio  Рік тому

      Under "interactions" you can set hold, tap and press (plus a couple others) to better define the behavior. Hope that helps.

  • @Joooooooooe11
    @Joooooooooe11 2 роки тому

    If I subscribe to actions that are part of an action map that is disabled, they don't do anything when the input is used until their action map is swapped in, correct? I'm still struggling to wrap my head around swapping action maps and exactly how everything works. Even using the built in swapactionmaps functionality in the PlayerInput component seemed like it didn't even work half the time or had weird side effects I didn't know how to address because I didn't know what was causing them, so I finally just decided to jump into the fire and attempt learning the ins and outs of the system by bashing my head against the wall long enough.

    • @OneWheelStudio
      @OneWheelStudio  2 роки тому +1

      Yes. If you subscribe to an action that is on a disabled action map nothing should happen. The idea behind changing action maps is to simply enable or disable action maps. The goal of the method presented here is to do that in a controlled or central to keep things tidy.

    • @Joooooooooe11
      @Joooooooooe11 2 роки тому +1

      @@OneWheelStudio I figured out my problem. I forgot to swap my objects to use the static manager so couple of them were still using their own instance and it was causing problems since they thought actions were null since their action maps hadn’t been instantiated. It’s working like a charm now.
      Thank you for your videos. Several of them have been lifesavers lately, especially as I have been trying to learn the new input system as I’ve refactored over.

  • @Keanine
    @Keanine 2 роки тому

    If this system was to be used in a splitscreen game, would you need to have an ActionMap for each player in a list and only adjust the relevant one?

    • @OneWheelStudio
      @OneWheelStudio  2 роки тому +1

      I haven't looked into local multiplayer, but my guess would be that you would need to instances of the action map each connected to the different inputs. Unity does have an example that you can download, but I haven't had time to jump into that yet.

  • @tenpovine1668
    @tenpovine1668 2 роки тому

    Invoke unity events?

  • @purplevincent4454
    @purplevincent4454 Рік тому

    No I don't like the classes all being able to change the input with static instance. You can either do two things, have the input manager subscribe to and react to external state (this is what I am doing) or you can just inject an instance of the input manager only to classes who need it (like player), this way you won't have that static instance proliferating your code and being changed in places you mistakenly didn't account for.

  • @jamesgage6703
    @jamesgage6703 2 роки тому +4

    I was getting errors when trying to create a new InputAction from a Monobehavior. I had to set my inputActions on the InputManager in an Awake function for it to work properly.
    Awake()
    {
    inputActions = new "YourInputActionsNameHere"();
    }

    • @OneWheelStudio
      @OneWheelStudio  2 роки тому

      Yeah. I've since had similar issues. I've had some odd "race conditions" when using the new system. I'm working on a key rebinding video and ran into them all over the place there. Truly not sure why.

    • @jamesgage6703
      @jamesgage6703 2 роки тому +1

      @@OneWheelStudio I had the same, for the time I changed my InputManager script in Script Execution Order to be -50 so it loads before my other scripts, but may be worth looking into Lazy Initialization for it down the road.

    • @OneWheelStudio
      @OneWheelStudio  2 роки тому +1

      Yeah. That's essentially what I did. Lots of checks for null... Not pretty. Not the way I like to do things, but it does work.

  • @diliupg
    @diliupg 2 роки тому

    I am on Unity 2020.3.18f1 LTS and had a time of my life trying to get the Ui-Input Action Map to work alongside the player Action map. It never does! Do you know how to do that? I found that when I run the game, the player grabs the Player action map and the UI Action map simply does not work. If there is no player at the start of the game it works, so what I did was to start the game without the player accessing the Player Action map and then after the game launched enable the player from the Game Manager in code. This way the UI buttons work alongside the player controls. Has anybody else faced this problem? Do you, One Wheel Studio, know a better way. Thanks for all your tutorials.

    • @OneWheelStudio
      @OneWheelStudio  2 роки тому

      Hmm. Not sure what the issue is - hard to say without seeing the code. Sounds like the action maps aren't getting enabled/disabled correctly.

  • @toastyshrimp1882
    @toastyshrimp1882 2 роки тому +5

    action doesn't even look like a real word anymore

    • @OneWheelStudio
      @OneWheelStudio  2 роки тому +1

      The script for the video was hard to read in places! Action, action, action, action...

  • @turkey0709
    @turkey0709 11 місяців тому

    I tried to attach the InputManager to a gameobject, but an error is reported in the console. UnityException: CreateScriptableObjectInstanceFromType is not allowed to be called from a MonoBehaviour constructor

    • @OneWheelStudio
      @OneWheelStudio  11 місяців тому

      It's been a long while since I made this video, but I don't think the InputManager has anything to do with scriptable objects.... Am I wrong? Does the error point to another class that might be listening to an event?

    • @linkfting3407
      @linkfting3407 9 місяців тому

      @@OneWheelStudiothe generated csharp wrapper class will try to create a InputActionAsset instance which is a ScriptableObject, its not allowed to create SO in ctor, it must be created in lifecycles like Awake or Start, bc SO has lifecycles too.

    • @linkfting3407
      @linkfting3407 9 місяців тому

      oh no... that's a no go solution too... because onEnable s are not strictly executed after all Awake s across objects...

  • @richardguilliams7278
    @richardguilliams7278 2 роки тому

    I just messed up. I wrote a state machine interface using one of your tutorials and got that working... then i added this input system and when i did that i updated the system making the old input api unusable and now my state machine doesnt work. Im unsure how to make the state machine work with the new input system. since none of my state machine scripts are attached to the player i cant make the action map invoke anything from them.

    • @OneWheelStudio
      @OneWheelStudio  2 роки тому

      If you hear into the project setting you can change a setting to use both the old and new input system. Not a perfect solution but might get you moving again.

    • @richardguilliams7278
      @richardguilliams7278 2 роки тому +1

      @@OneWheelStudio thank you, i got it working again. yeah i might wanna just wait to use this new input system for my next game. with my current one i think id have to change alot of work ive already done which would be the second time i did that haha.

    • @OneWheelStudio
      @OneWheelStudio  2 роки тому

      A mid project switch would be difficult for sure! Glad it’s working again.

    • @richardguilliams7278
      @richardguilliams7278 2 роки тому

      @@OneWheelStudio indeed thank you. and thank you so much for your lectures and tutorials about design patterns you have truly helped take my coding to the next level.
      Now im reworking my state machine into a modular form that i can plug stateActions into by implimenting the strategy pattern.
      Ultimately id like all my functionality to be modular and workable from the front end as monobehaviors. Trying to sell them on the asset store for some side income.
      You have been a major contributor so sincerely, thank you again.

    • @richardguilliams7278
      @richardguilliams7278 2 роки тому

      If I end up succeeding with this which i believe I will, I would love to contribute to your channel.
      pretty much my state machine will make calls to the action I place in its Action parameter. So a JumpState will have a JumpAction that stores the functionality.
      That way you can easily swap it out for another type of jump.
      Same with the StatManager, The RaycastManager, and other manager i have made.
      I know im dragging on but honeslty i've never felt more excited to code than now that i have a better understanding of classes and monoBehaviours and these patterns. Two years of study and im finally excited to code lmao

  • @StickyLabDev
    @StickyLabDev 3 місяці тому

    unclear

  • @spartadiunruly6212
    @spartadiunruly6212 3 роки тому

    MAYBE CYRPTO BRO