in this video we'll be creating a
cross-platform player controller using
the unity input system
the input system is a package that
allows you to easily set up controls for
multiple platforms
you can install it from the package
manager and it is a verified package for
unity 2019 lts in newer versions of
unity
the project shown is available for
download on github following the link in
the description below
if you want other information check the
input system page
you can find the link in the description
as well
in this unity project we have a
character controlled by the keyboard
we can walk around using the wasd keys
and we can use the spacebar to attack
the inputs are currently detected by the
input manager
in the input manager most of the work is
done programmatically in script
and it requires a lot of extra code to
change the binding of the keys at
runtime or switch the input device
such as keyboard console game pads or
even touch screen
we've introduced the unity input system
which allows for simpler and more
flexible input mapping
rebinding and configuration for all
types of projects and devices
let's set up the input system and
convert our player controller to use
keyboard
gamepad and mobile controls to install
the input system package
open window package manager in the open
package manager window
scroll down through the available
packages and select input system
let's install the package by clicking
install on the bottom right corner of
the package manager window
note that the input system comes with
some useful samples that you can import
into your project
now that the input system package is
added to the project we are able to
create a new asset that stores our input
actions let's create it by clicking the
right mouse button inside the project
and select create
input actions rename it player actions
and double-click to open the input
actions editor window
the input actions window is divided into
three columns
action maps actions and properties
let's start by creating an action map
click on the plus button in the corner
an action map represents a set of
controls dedicated to a particular mode
of your game
for example usually we want different
controls in the pause menu compared to
the gameplay
we may also want different controls for
different modes of gameplay such as
walking versus driving
for this reason action maps can also be
switched at run time using the switch
current action map method
let's name our new action map player
controls
with this action map selected we can
create some actions by clicking the plus
button in the upper corner of the second
column
an action is something the player can do
such as move attack
or jump it shouldn't be inherently
linked to a specific input or button and
should be kept generic
we should define all the actions that
our player can perform and then we can
define multiple bindings for each action
such as for different devices or
platforms let's name our first action
attack by selecting an action we can
change its properties in the third
column
for attack we want to choose the button
as the action type since we just want to
press a button to perform the attack
a button can be a keyboard key a trigger
or a normal button on a gamepad
let's create another action and name it
movement
this time we want to choose the value as
action type
the value type should be used for inputs
which can track continuous changes such
as a joystick
since we are using value a second field
is enabled
the control type represents the type of
value we want in return
in this case we'll use vector 2 to get
the x-axis and y-axis of the joystick
on the attack action if we click on the
plus button we can add a new binding
for this binding we can now choose a
path this is how we define what the
binding corresponds to
here we can find all the different types
of inputs we can set up
from this options menu we can manually
search for the input that we want
navigate through the submenus or use the
search bar at the top
we can also click on listen to detect
the input we push
let's set the e key for the attack
action
now let's click the plus button on the
movement action
here we can add a binding or 2d vector
composite
the 2d vector composite option will
create a 2d vector with up
down left and right this makes sense for
our movement action
let's call this wasd keys
for each of these bindings we can choose
a path as we did for the attack
let's set up the wasd keys as the
binding for each direction
now to confirm our changes we need to
save the asset by clicking on the button
at the top
alternatively we can enable the auto
save feature using the flag on the left
which will save automatically
when we edit the asset this feature is
off by default so take care to switch it
on or save the asset after any changes
now that we have our input actions
assets set up we need an instance of it
in the scene so we can detect input at
runtime
this can be done by using a player input
component
as our warrior character is what we want
to control with this input actions asset
we can add the player input component to
it
in the actions field we can assign the
actions asset
another important field is the behavior
that determines the way the player input
component notifies that something has
occurred
we're going to use invoke unity events
this way all the actions will be exposed
in the inspector as events and we can
attach our callbacks to them
besides the actions that we've defined
there are also some built-in ones such
as on device
lost or on controller changed for a
deeper explanation of all the behaviors
check the documentation link below
since we've set the invoke unity events
option we need to declare on the
playercontroller the callbacks for our
actions
and link them to the events in the
inspector for the movement we declare
the on movement method
here we can access the parameter of type
input action
dot callback context which is a data
structure that contains all the
information provided about what
triggered this action
calling the read value generic method we
can read the vector2 value
and use it to override the movement
vector in a similar way we implement the
onattack method
we read the started value which is a
boolean that represents if the action
has just started
in case started is true we use the
animator of the player to play the
attack animation
in the inspector we can now select the
callbacks for each event
clicking the plus button we can add a
new callback for each callback we have
to select the object that holds the
method that we want to use
and from the right hand menu we choose
what method to call navigating through
all the scripts that the selected object
has
let's add the callbacks for movement and
attack
now we're able to move our player and
attack using the input system
before setting up our gamepad controls
we'll create a new control scheme for
our keyboard
a scheme is a group of bindings for the
actions that we can easily separate for
different devices in their bindings
in the input window click on all control
schemes
add control scheme let's call this
keyboard and add the keyboard list to it
for each binding we can choose which
scheme they belong to
this also allows us to filter them in
the selection above
let's add all the bindings that we've
created before to this new scheme
now that everything is set up for the
keyboard we can do the same for the
gamepad
click again on all control schemes and
select add control scheme
this time call it gamepad
let's create a new binding for the
movement of our gamepad
click on the plus button add binding in
the path field select
gamepad from here we can choose input
from a specific device like the
playstation controller the switch
controller
or we can choose a generic left stick
that works for all supported gamepads
the same concept can be applied to the
attack action
for gamepad button bindings we can
choose the position of the button that
we want
for instance the playstation controller
has the x button in the same position as
the a button on the
xbox controller and the b button on the
nintendo switch pro controller
we can just choose button south to cover
all those cases
now if we press play we are able to move
an attack using a gamepad
the player input component will
automatically detect the input we're
using and assign it to the player
so we can easily switch at runtime and
use both the gamepad and the keyboard
there's a new input debugger that comes
with the new input system package that
can help us see all connected input
devices and their inputs
you can find it under window analysis
input debugger
here we can see all the connected
devices this list is updated in real
time so if we connect or disconnect the
device we will immediately
see it even if we are outside the play
mode if we double click on a device a
dedicated window opens up
here we can find all the buttons that
belong to this device
if we push a button we can see that its
value updates and an event is logged
the input system package also supports
mobile for example
in this canvas we have the on-screen
stick component that simulates the left
stick of a generic gamepad
we can choose the control path to use
from the inspector just as we did in the
input window
we have a similar component that
simulates the buttons that is called
on-screen button
this simulated gamepad is also visible
in the input debug window
if we enter play mode everything works
as expected
we can use the simulated gamepad just
like any of the other input devices
we can also spawn multiple warriors
since the code is generic
and the input device's signing is
automatic all we need is a loop to call
the instantiation api to spawn the
warriors prefabs
we can set the loop to how many warrior
players we desire
each warrior shares the same code and
has a player input component on itself
that allows it to be recognized as a
player
and detect the triggers of the actions
if we open the input debugger window
during play mode we can see that all the
player inputs are shown
and for each player input the relevant
connected device is shown as well
the player input component also covers
automatically the disconnection or the
reconnection of a controller
these events are exposed and we can use
them to implement custom logic
if you'd like to try out this example
for yourself this project is available
on github and linked in the description
below
for more information check the input
system page link below as well
thanks for watching
you