Unsure about how to implement unity's input system, the code I have doesn't work

I am trying to detect when a key was pressed using this code

public void MoveLeft(InputAction.CallbackContext context)
{
if (context.performed)
{
horizontal = -1;
buttonPressed = true;
}
else if(context.canceled)
{
buttonPressed = false;
}
}

public void MoveRight(InputAction.CallbackContext context)
{
if (context.performed)
{
horizontal = 1;
buttonPressed = true;
}
else if (context.canceled)
{
buttonPressed = false;
}
}

I am using unity’s input system, and I have two inputs called MoveLeft and MoveRight. The horizontal variable does not change when I press the key, and I don’t know why. I am a beginner, and just started unity a week ago.

Definitely start with the official Unity3D tutorials for this. The answer will ALWAYS be much more than just some code, nothing that could easily be typed in this forum field tiny text box.

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

Imphenzia: How Did I Learn To Make Games:

How are you hooking up these methods into the input system? Are you using the PlayerInput component? Subscribing to Input action delegates?

More information is needed here.

Nonetheless for this kind of input, it is still just easier to poll on a per-frame basis IMO.

Please don’t tag with 2D-physics unless the question is about 2D-physics.

In this case, the “input system” tag would be more appropriate.

I’ll change the tags for you.

Thanks.

Sorry, I didn’t know which tags to use and picked the one I thought was best. Thanks for changing it for me!

1 Like

Not a problem, totally get that there’s a heck of a lot of tags to choose from. :slight_smile: