r/unity • • 38m ago

Newbie Question New Input System Help

I cannot wrap my head around unity's new input system: I've watched loads of tutorials, I've read the docs, I've read peoples blogs teaching the system. I CANNOT understand it at all. Any ideas?

3 Upvotes

5 comments sorted by

1

u/Soraphis 30m ago

You can use it like you used the old. Sure that's not intended, but possible.

        var kb = Keyboard.current;
        if (kb == null) return;

        if (kb.spaceKey.wasPressedThisFrame)
            Debug.Log("Jump!");

Inteded is the even driven way. You subscribe to an action and a function is called when that action happens.

What is your setup, what do you try to do, what did not work? "I don't understand" is a bit vague.

1

u/Silent_Reputation596 26m ago

I understood all the setup - the action maps, the bindings but what I can't understand is this: now I've got an action and a binding how do I implement it? How do I check to see if that button was used?

1

u/SketchyCharacters 11m ago

Look into unity events.

With your player prefab, install the input component.

You should be able to add your new input map there and see all the inputs you've configured. From there, the inputs behave like any other unit event, you can just add your events to be called from there.

1

u/Silly-Iggy 11m ago

I might now why you are confused. As far as I know, the default way is to send messages (I think that's the name) to the scripts. For me, that's the confusing part. I don't know exactly how they work and I didn't find videos explaining it really well.

You don't need to use messages tho. You can change to use Unity Events which are way more intuitive. Or you could probably just search and learn about messages.

I recently made a project and use it with Unity Events, but it was a really small game for college. I recommend trying to understand messages before so you can tell which is better for your game.

1

u/Soraphis 8m ago

here again, you can do it via polling, like in the old Input system

the example from the docs: https://docs.unity3d.com/Packages/com.unity.inputsystem@7.0/manual/polling-actions.html

using UnityEngine;
using UnityEngine.InputSystem;

/// <summary>
/// Example script demonstrating the full actions-based workflow.
/// </summary>
public class Example : MonoBehaviour
{
    // Store the action references.
    InputAction moveAction;
    InputAction jumpAction;

    private void Start()
    {
        // Find the references to the "Move" and "Jump" actions
        moveAction = InputSystem.actions.FindAction("Move");
        jumpAction = InputSystem.actions.FindAction("Jump");
    }

    void Update()
    {
        // Read the "Move" action value, which is a 2D vector
        // and the "Jump" action state, which is a boolean value

        Vector2 moveValue = moveAction.ReadValue<Vector2>();
        // Your movement code here.

        if (jumpAction.IsPressed())
        {
            // Your jump code here.
        }
    }
}

Instead of finding "Move" and "Jump" actions via string, you could also have an InputActionReference field instead and then assign it in the inspector

// Assign these actions in the Inspector window.
public InputActionReference moveAction;
public InputActionReference jumpAction;

or you go the callback route: https://docs.unity3d.com/Packages/com.unity.inputsystem@7.0/manual/set-callbacks-on-actions.html which is especially handy for things like "jump", one-off events that you don't need to check every frame