Search Unity

Resolved Input system movement from InputActionAsset

Discussion in 'Input System' started by Sauron1234, Apr 28, 2022.

  1. Sauron1234

    Sauron1234

    Joined:
    Sep 22, 2014
    Posts:
    29
    here is what I want to do:
    I have an InputActionAsset. I want to add a code on a gameObject (Player) to move the player.
    that code will take a InputActionAsset as variable and move the object according to the InputActionAsset "Move" action that Ive made.
    Then, when I press a key, I can change the "Move" action bindings.

    I know how to change bindings, but what bothers me is I need a PlayerInput component. Is there a way to by-pass this and just use the InputActionAsset? This component seems redundant and useless, since I am attaching a script anyways to access the PlayerInput, that gets the InputActionAsset. Why the middle man??
    Ive already got so many components on my gameobjects.

    So is there a way to create our own movement script straight from a InputActionAsset and change/add/remove bindings and sawp or add/remove actions???
     
  2. Sauron1234

    Sauron1234

    Joined:
    Sep 22, 2014
    Posts:
    29
    yes! I found it for those who need:


    public InputActionAsset actionAsset;
    private void Start()
    {
    actionAsset.actionMaps[0].Enable(); //if you didnt already enable actions....
    }
    private void Update()
    {
    if( actionAsset.actionMaps[0].actions["ActionName"].triggered ) //do stuff here//
    }


    however I am not doing this. I don't want to use a ActionAsset but will simply be hard coding my actions on runtime like this:


    //(prolly put this inside player gameObject)


    public InputAction myAction; //can be a list of actions later when I add more//

    private void Start()
    {
    //create the actions and enable them
    myAction = new InputAction();
    myAction.Enable();

    myAction.AddBinding("<Gamepad>/buttonSouth");
    myAction.AddBinding("<Mouse>/leftButton");
    myAction.AddBinding("<Keyboard>/space");
    }
    private void Update()
    {
    if (myAction.triggered ) //do stuff here//
    }