Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Changing to a different Action Map?

Discussion in 'Input System' started by notMateo, Jan 29, 2020.

  1. notMateo

    notMateo

    Joined:
    Mar 2, 2018
    Posts:
    36
    How can I switch to a different Action Map using code? Is there a way to disable and enable action maps? I haven't been able to find it. I'm using the Player Input component and this is what my code looks like so far, save for the random other controls:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.InputSystem;
    5.  
    6. public class PlayerControlsManager : MonoBehaviour
    7. {
    8.     public PlayerInventory playerInventory;
    9.     public PlayerControls controls;
    10.  
    11.     void Awake()
    12.     {
    13.         controls = new PlayerControls();
    14.         playerInventory = this.gameObject.GetComponent<PlayerInventory>();
    15.         playerMovement = this.gameObject.GetComponent<PlayerMovement>();
    16.  
    17.         controls.Gameplay.Interact.started += ctx =>
    18.             playerInventory.heldInteract = true;
    19.  
    20.         controls.Gameplay.Interact.canceled += ctx =>
    21.             playerInventory.heldInteract = false;
    22.     }
    23.  
    24.     public void OnEnable()
    25.     {
    26.         controls.Enable();
    27.     }
    28.  
    29.     public void OnDisable()
    30.     {
    31.         controls.Disable();
    32.     }
    33.  
    34.     public void OnKillControls(){
    35.         Debug.Log("Disabling Controls!");
    36.         controls.Gameplay.Disable();
    37.     }
    38. }
     
  2. transat

    transat

    Joined:
    May 5, 2018
    Posts:
    779
    I use PlayerInput.SwitchCurrentActionMap("OtherActionMap"). But I don't do it "in code". With PlayerInput, I switch the actionmap when I press a certain button on my controller:

    Screen Shot 2020-01-29 at 6.46.08 pm.png

    Now if I press RightStick on my gamepad, it triggers the above, and the remaining gamepad buttons are automatically mapped to the Character actionmap, thereby extending my controller's functionality (at the cost of complicating things a bit for the end-user perhaps). Example in a game context: I can have a character crouch with the X key when on foot but if I move that character next to a car and press RightStick, the character opens the door of the car and gets in while the actionmap is changed to Driving. Now X will make the car brake (as one doesn't usually crouch in a car). Or a more common scenario would be pressing the "Options" button on my controller to enable my menu UI while switching to the UI actionmap.
     
    mamabox, Lefko, l3m35 and 2 others like this.
  3. notMateo

    notMateo

    Joined:
    Mar 2, 2018
    Posts:
    36
    Thank you so much! I had no idea it could be this straight-forward. Appreciate it.