Snippets

Alex Darby Read the current value out of an action map in the new Unity input system

Created by Alex Darby
using UnityEngine;
using UnityEngine.InputSystem;
					

public class ActionTester : MonoBehaviour
{
	public PlayerInput	m_PlayerInput;					// hook this up to your player input component
	public string		m_ActionNameToCheck = "Fire";	// set this to the name of the action you want to check

	public bool ActionIsPressed()
	{
		// you may need to check the cAction.type to ensure this works for other action types
		float fCurrentValue = m_cAction.ReadValue< float >();
		return ( fCurrentValue != 0f );
	}


	//////////////////////////////////////////////////////////////////////////
	// NOTE: this assumes the current actionmap is not going to change
	InputAction m_cAction = null;

    void Start()
    {
		m_cAction = m_PlayerInput.currentActionMap.FindAction( m_ActionNameToCheck );        
    }

    void Update()
    {			
		if( ActionIsPressed() )
		{
			Debug.Log( "[" + Time.time + "] " + m_ActionNameToCheck + "!" );							
		}
    }
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.