I have the player input moving, he runs and jumps and does all sorts of things, however, I’d like to disable it and enable it at will, for example when I need to prevent player input while a big boss appears and shows off for a few seconds.
I’m looking at this page on the unity docs which says to use PlayerInput.DeactivateInput. However I can’t find a way to use this.
Is it a case of using: PlayerInput input = GetComponent()
then calling input.DeactivateInput()?
The site doesn’t say.
The reason I can’t try this at the moment is because my player input script is also called PlayerInput, and I don’t know how to differentiate between the player input class I added to the gameobject, and the Unity PlayerInput that’s added to the class to use the new input system.
Please refer to my comment further below as I made a big mistake years ago when making this comment. I feel like it’s caused a lot of google searches to come up with issue.
I very much shorthanded the code, but I believe I made the mistake of saying you can declare a new PlayerInput component when I was trying to convey you can disable the inputactionmap. I was relatively new to the system and completely missed the PlayerInput reference I made.
In truth, it would be more along the lines of (more quick code, haven’t checked it) -
This is using the script generated from the input asset.
As for the original purpose of the post, here’s a testing script I used to show how to disable or enable a player input-
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.InputSystem;
public class DEBUGPlayerInput : MonoBehaviour
{
public PlayerInput _playerInput;
IEnumerator Start()
{
while (true)
{
yield return new WaitForSeconds(1f);
_playerInput.DeactivateInput();
Debug.Log("Deactivated Input");
yield return new WaitForSeconds(1f);
_playerInput.ActivateInput();
Debug.Log("Activated Input");
}
}
}
You were correct in that you need to call DeactivateInput or ActivateInput, you just need to reference the PlayerInput.
I apologize for the general confusion my previous comment probably caused, I didn’t know there were previous comments asking about the code I posted. I’ll change it up a bit to reflect this comment.
Best method I found to disable the input system is to reference through the ‘InputActionAsset’, then call on your input system you made. From here, you can then reference it in your code to call .Enable() or .Disable().
You can do this but on an individual input level as well.
The below should also work with your current Toggle script so long as you have “Player” as an action map and “Move” as an action.
public void EnableMovement()
{
var input = _pInput.actions.FindActionMap("Player").FindAction("Move");
input.Enable();
}
public void DisableMovement()
{
var input = _pInput.actions.FindActionMap("Player").FindAction("Move");
input.Disable();
}