I was following a tutorial (Local Multiplayer with NEW Input System - Unity Tutorial - YouTube) and I did everything as shown, yet it doesn’t work. It reads the Vector2 just fine, but when I try to jump I get an error message saying space bar presses give a float value that can’t be read as a bool. Could the problem be, that I’m making a 2D and not 3D game? Any ideas how to fix this?
private Vector2 movementInput = Vector2.zero;
private bool jumped = false;
public void OnMove(InputAction.CallbackContext context)
{
movementInput = context.ReadValue<Vector2>();
}
public void OnJump(InputAction.CallbackContext context)
{
jumped = context.ReadValue<bool>();
jumped = context.action.triggered;
}
There are a lot of tutorials out there on the New Input System that vastly overcomplicate how to use it. Here’s a list things you need to do to get Jump working (some basic knowledge required…):
- Install the Input System Package from Package Manager
- Add a Player Input component to your player
- Use the Player Input component to “Create Actions”
- Add Jump to the Actions and create a binding for jump key/button
- Save (or better, tick Auto-Save)
- In the Player Input component, leave Behaviour as Send Messages
- In your code, add the
UnityEngine.InputSystem
namespace
- In your code create a
void OnJump() {}
method
That’s it. No CallbackContext or anything like that. Sure, it might seem like a lot compared to the Old Input System but the point is it’s event based and it has decoupled hardware from your script.
I don’t doubt that you’ve followed the video but line 11 looks very odd to me. Partly because I don’t think it works but also because the next line overwrites the value that you’ve tried to set in line 11. What happens if you comment out line 11? It looks to me as if it should work.
I can’t test it because I don’t have the rest of your project but it should be an easy one for you to test. Let us know how you get on…
jumped = (context.ReadValue() > 0.5f);