r/unity • • 1d 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?

Edit: This has been solved!

3 Upvotes

10 comments sorted by

2

u/Soraphis 1d 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 1d 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?

2

u/Soraphis 1d 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

1

u/SketchyCharacters 1d ago edited 1d 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.

Edit: the input component also includes some debugging options too.

1

u/Silly-Iggy 1d 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 13h ago

When I started using unity around 5.3 era over 10 years ago, SendMessage was already frowned uppon.

So, I wouldn't say it's the default way 🫣

1

u/Silly-Iggy 3h ago

By "default" I don't mean what is commonly used. I mean what the default Unity settings is.

When I used the first time, before configuring anything, it was set to "Send Message". I had to change from that to "Send Unity Events".

I never used messages and never search what it actually does. So I didn't quite know it was not very used.

1

u/JakSilver00 1d ago

So I typically start with templates or assets when I'm learning something new to see how other people are doing it. Luckily, Unity provides these things for you. They have a starter first and third person asset. And once you start understanding the basics, you can then add custom controls and bindings and so forth.

1

u/GigglyGuineapig 1d ago

It's another tutorial and a video, but maybe my walk though will help? Sometimes it takes a few more teachers to understand a concept and I've gotten a lot of good feedback about my approach. I show how I am using a relay system with a scriptable object for it and how other scripts are then accessing those, for example for movement. 

https://youtu.be/MaulSHrhA8I?is=L3oFp0zerYVF5X2k

1

u/bigmonmulgrew 10h ago

The strength of the new input system is its flexibility.

The weakness of the new input system is it's flexibility.

I remember when I first picked it up. I didn't realise there's so many ways to do the same thing. It means if you do two tutorials they will likely be different and you end up confused and thinking you are missing something because you don't see how the two are connected.

Try searching for tutorials made within the last 12 months. You will find they have matured a lot. Also try the ones on Unity's own learning site.

If you are following one and get stuck come back here posting which tutorial you are following and your code so we can give more specific help and maybe clarify some of the sticking points