Heads up! To view this whole video, sign in with your Courses account or enroll in your free 7-day trial. Sign In Enroll
Well done!
You have completed Unity Basics!
You have completed Unity Basics!
Preview
One of the most crucial aspects of creating a game is responding to the player’s input. Let’s see how we can do this in our new script.
Unity 6 Documentation
Resources
Related Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign upRelated Discussions
Have questions about this video? Start a discussion with the community and Treehouse staff.
Sign up
Okay, before
we continue, I have to mention this.
0:01
Be aware that there are two ways
of working with player input in Unity.
0:04
There's the older input manager
and the newer input system.
0:08
The input manager is the built-in system
that's been around for a long time.
0:12
It's simple, easy to use, and still works
great but has some limitations.
0:17
On the other hand,
0:22
the input system is more powerful
and flexible, but it's a bit too complex
0:23
for this introductory course
and this simple game that we're making.
0:27
Check the teacher's notes below
for some documentation
0:31
on the newer input system
if you're curious.
0:33
Let's quickly go to Edit,
0:37
Project Settings,
0:39
and click on Input Manager.
0:42
Let's expand this Axes dropdown.
0:44
These are pre-configured inputs
and are designed to provide
0:47
a standardized set of controls
for common game actions.
0:50
They provide a quick starting point for basic game
controls. Let's expand this one named Jump.
0:54
We can see all sorts of
customizable fields here,
1:00
but we interested in is this positive button
one here. This means that when the player
1:02
hits the space key, we can acknowledge this
in our code by checking this Jump input.
1:07
Let's actually change the name
to flap with a capital F,
1:13
as that makes a little more sense
for our game
1:16
since our player will be flapping
its wings.
1:18
Let's actually
copy this while we're here too,
1:21
as these things are case sensitive
and we need to be exact.
1:24
Let's close
this and jump back into our script.
1:27
As mentioned earlier, this Update method
1:31
is being called on every single frame
when the game is playing.
1:33
This is a perfect place to listen for
player input so we can catch it
1:37
the very moment it happens.
1:40
To do this,
we need to write Input with a capital
1:42
I, then a dot because we want to access
a method from this Input class.
1:45
And the method we want is GetButtonDown.
1:50
Since this is a method,
we need to give it its parentheses
1:54
and end the line with a semicolon.
1:57
If we hover over this, we see it
says it returns true
2:00
during the frame the user pressed down
the virtual button identified by buttonName.
2:04
And up here we see we
need to provide it a string value
2:08
of the button name we want to listen
for within its parentheses.
2:12
When working with strings in C-sharp,
we use quotation marks.
2:16
Make sure you're not using apostrophes
or single quotes.
2:19
Inside these quotation marks,
we can paste our button name.
2:23
There are other
2:27
input methods that target
when a button is being held down
2:27
or even when a button is released,
2:30
but catching the moment it's pressed
is exactly what we want.
2:32
Now, this on its own
won't do anything for us.
2:36
We want to actually check
2:38
if this action has happened
and this method is returning true.
2:40
So let's wrap
this in a conditional statement.
2:43
We'll say if,
2:46
we'll get rid of this semicolon,
2:51
and put in our curly braces.
2:53
This is checking if this is truthy,
2:56
so it'd be the same as writing
if this equals true.
2:58
This should constantly return false
until a frame of our game happens
3:03
where the player presses
the space key down.
3:06
Let's write a message to the console window
3:10
to make sure this is working.
To do this in Unity, we write
3:12
Debug.Log, and within the
3:15
parentheses, we give it the value
we want to print to the console.
3:17
Let's just write a string and have it
say Flap!
3:22
This is essentially the same as using
3:25
console.writeline
in C-sharp outside of Unity.
3:27
Don't forget your semicolon.
3:31
Okay, let's save this script with Command
3:33
or Control S and go back to Unity.
3:35
We'll see that
since we made and saved changes, it's
3:39
going to compile our script,
and once that's done, we can press play.
3:41
I'm going to open the console window
and press play.
3:45
And now, if I press the space key,
3:49
we can see down here in the console
window, we immediately get our message.
3:50
So it's working!
3:54
You can toggle this collapse option here,
and it'll group identical logs
3:56
to one line
3:59
and provide a counter on the right
to show how many times it was logged.
4:00
We're successfully capturing player input!
4:04
In the next video,
we'll get this little circle flying.
4:07
See you there.
4:10
You need to sign up for Treehouse in order to download course files.
Sign upYou need to sign up for Treehouse in order to set up Workspace
Sign up