Insights

10 Unity Input System Best Practices

The new Input System in Unity is powerful and easy to use. Here are 10 best practices to follow when using it.

The Unity Input System is a powerful tool for creating user interfaces and managing user input. It allows developers to create custom input systems that can be used across multiple platforms. However, it can be difficult to get the most out of the system without following some best practices.

In this article, we will discuss 10 best practices for using the Unity Input System. We will cover topics such as creating custom input actions, setting up input axes, and using the Input System to create a more intuitive user experience. By following these best practices, you can ensure that your game or application is optimized for user input.

1. Use the new Input System

The new Input System is more efficient and easier to use than the legacy input system. It also allows for better control over how inputs are handled, making it easier to create custom controls that work with your game. Additionally, the new Input System supports multiple platforms, so you can easily port your game from one platform to another without having to rewrite all of your input code. Finally, the new Input System has a much cleaner API, which makes it easier to understand and debug.

2. Avoid using Update() for input processing

The Update() method is called once per frame, and if you’re using it to process input, then your game will be limited by the framerate. This means that if your game runs at 30 frames per second, then your input processing will only happen 30 times a second as well.

Instead of using Update(), use the new Input System’s events such as OnActionStarted or OnActionEnded. These events are triggered whenever an action occurs, regardless of the framerate, so they provide much more responsive input handling.

3. Use a singleton to access your controls from anywhere in the code

A singleton is a design pattern that allows you to access the same instance of an object from anywhere in your code. This means that if you have multiple scripts accessing the same input controls, they will all be using the same instance of the control. This makes it easier to keep track of changes and ensures that all scripts are working with the same data.

Using a singleton also helps to reduce code duplication and makes it easier to debug any issues related to input. It’s important to note that this best practice should only be used when necessary; for example, if you’re creating a game where multiple players can use different controllers, then it would make sense to create separate instances of the Input System for each player.

4. Create multiple action maps for different contexts and situations

Action maps are a great way to organize your input data and make it easier for developers to access. By creating multiple action maps, you can easily switch between different contexts or situations without having to manually change the bindings of each individual action. This makes it much easier to quickly adapt to changing game states or user preferences.

Additionally, by using multiple action maps, you can also create more complex control schemes that require multiple inputs at once. For example, if you have an action map for driving controls and another one for shooting controls, you can easily combine them into a single action map when the player is in a car while shooting.

5. Use PlayerInput component for player-specific actions

The PlayerInput component allows you to easily set up multiple players in your game, each with their own input settings. This makes it easier to manage different types of inputs for each player and ensures that the correct input is being used for the right action. Additionally, this helps keep your code organized and maintainable since all of the player-specific actions are grouped together in one place.

6. Use ActionMapReference component to switch between action maps

The ActionMapReference component allows you to switch between different action maps without having to manually change the bindings for each input. This is especially useful when you have multiple players using the same game, as it allows them to quickly and easily switch between their own custom control schemes. It also makes it easier to manage your input system in general, since all of the bindings are stored in one place.

7. Use ActionPropertyReference component to bind an action property to a game object

The ActionPropertyReference component allows you to bind an action property to a game object, which means that the value of the action property will be updated whenever the state of the game object changes. This is especially useful for things like buttons and sliders, as it ensures that the action property always reflects the current state of the game object.

Using this component also makes it easier to debug your input system, since you can easily see what values are being set on each game object. Additionally, it helps keep your code clean and organized by separating out the logic for setting up the action properties from the logic for handling user input.

8. Use ActionEventListener component to listen to action events

The ActionEventListener component allows you to easily listen for and respond to action events, such as button presses or joystick movements. This makes it easier to create custom input logic without having to write a lot of code. Additionally, the ActionEventListener component is designed to be used with the Unity Input System’s Actions API, which provides an easy way to define and manage user inputs. By using this combination, you can quickly set up complex input systems that are both flexible and extensible.

9. Use ActionStateCallbackReceiver component to receive callbacks when an action state changes

The ActionStateCallbackReceiver component allows you to easily detect when an action state changes, such as when a button is pressed or released. This can be used to trigger events in your game, like playing a sound effect or changing the camera angle. It also makes it easier to debug and troubleshoot input issues since you can see exactly when an action state changed.

10. Use ActionTriggeredCallbackReceiver component to receive callbacks when an action is triggered

The ActionTriggeredCallbackReceiver component allows you to easily set up a callback for when an action is triggered. This makes it easy to respond to user input in your game or application without having to manually check the state of each action every frame. It also helps keep your code clean and organized, as all of your callbacks can be grouped together in one place.

Previous

10 ASP.NET Core Architecture Best Practices

Back to Insights
Next

10 Azure Data Factory Logging Best Practices