1
\$\begingroup\$

I know this is so easy, but I am new Unity user. How do I use Input.GetButton in the new Input System?

using UnityEngine;
using System.Collections;

public class ExampleClass : MonoBehaviour
{
    public GameObject projectile;
    public float fireDelta = 0.5F;

    private float nextFire = 0.5F;
    private GameObject newProjectile;
    private float myTime = 0.0F;

    void Update()
    {
        myTime = myTime + Time.deltaTime;

        if (Input.GetButton("Fire1") && myTime > nextFire)
        {
            nextFire = myTime + fireDelta;
            newProjectile 
                = Instantiate(projectile, transform.position, transform.rotation) as GameObject;

            // create code here that animates the newProjectile

            nextFire = nextFire - myTime;
            myTime = 0.0F;
        }
    }
}
\$\endgroup\$
1
\$\begingroup\$

Semantic actions defined in the Input Manager panel (like "Fire1") can no longer be used with the new input system. But you can still check the status of specific keyboard keys. The 1:1 equivalent of the old Input.GetKey(KeyCode.Space) would be Keyboard.current[Key.Space].isPressed. More examples for drop-in replacements for the old input system can be found in the official migration guide.

However, if you really want to benefit from all the advantages which the new input system offers, including semantic input actions like "Fire1", then you need to rethink the way you handle player input on a fundamental level. The way the new input system is supposed to be used is not by checking the state during an Update method. The new architecture is designed for having dedicated handler methods for input events which get called by the input system when the player performs an input action.

First, you define all your inputs in an Input Action asset. These assets serve basically the same role as the old Input Manager panel in the Project Settings, but they are a lot more advanced and flexible. Among other things, they again allow you to define semantic input actions like "Fire1" and map multiple physical inputs to them.

Then you assign handler methods for those actions in your scripts using either the PlayerInput component or the auto-generated C# class.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.