Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:05,700 --> 00:00:11,730
In this video we're going to take a look at using the input manager of unity to map our user inputs
2
00:00:11,820 --> 00:00:13,440
to our character.
3
00:00:13,440 --> 00:00:19,350
For example I want to be able to move left and right based on the Andes Keys and up and down based on
4
00:00:19,350 --> 00:00:27,270
my W.S. keys in order to do this we need to take a look at the input manager to check out the input
5
00:00:27,270 --> 00:00:33,810
MANAGER We're going to go to edit project settings and you'll see here that there is something called
6
00:00:33,930 --> 00:00:42,180
input and within the input we have what's called accesses unity by default provides us with 18 different
7
00:00:42,180 --> 00:00:47,300
accesses that we can use to map user input and you can add your own.
8
00:00:47,520 --> 00:00:52,590
For example you'll see there's things here that are jump in fire which are already mapped to things
9
00:00:52,590 --> 00:00:56,010
like game controllers and the spacebar.
10
00:00:56,010 --> 00:01:01,770
What we're interested here is the horizontal and vertical axis is if I check out the horizontal axis
11
00:01:01,770 --> 00:01:07,830
here you'll see here that it has a negative button as well as a positive button and it's the left arrow
12
00:01:08,040 --> 00:01:09,270
in the right arrow.
13
00:01:09,270 --> 00:01:14,560
It also has an alternate negative button and positive button which is map to the HD keys.
14
00:01:14,610 --> 00:01:19,560
So what I want to do is I want to use this access to move horizontally.
15
00:01:19,740 --> 00:01:22,140
The next thing you'll notice here is that it has a name.
16
00:01:22,140 --> 00:01:28,240
I'm accessing the horizontal axis and by doing that I can use a and d.
17
00:01:28,380 --> 00:01:36,840
But what needs to make sense to me is how how do I use this to move my character with a D.
18
00:01:36,870 --> 00:01:38,880
Well it comes down to what do I get.
19
00:01:38,880 --> 00:01:40,010
What is this value.
20
00:01:40,020 --> 00:01:44,550
If I were to do anything with this horizontal axis what does it mean.
21
00:01:44,580 --> 00:01:47,650
So let's check out what's being returned here.
22
00:01:47,790 --> 00:01:49,170
What are the things that the axis is.
23
00:01:49,170 --> 00:01:54,800
Don't really tell you is what they return buttons are Easy buttons are boolean they're true or false
24
00:01:54,810 --> 00:01:59,220
if you hit a button but what about a horizontal and vertical axis that determines if you're hitting
25
00:01:59,220 --> 00:02:01,500
an HD key to move an object.
26
00:02:01,500 --> 00:02:02,870
Well this is where it comes down to.
27
00:02:02,880 --> 00:02:08,160
We need to store it in a variable in viewing what I'm going to do here for testing purposes and you
28
00:02:08,160 --> 00:02:11,300
don't have to follow along with this but if you'd like to you can.
29
00:02:11,430 --> 00:02:18,720
I'm going to create a public float variable of type horizontal or not have type just the name horizontal
30
00:02:18,720 --> 00:02:23,280
I'm just you can name it whatever you want but here we have public float horizontal and let's call it
31
00:02:23,310 --> 00:02:24,940
horizontal input.
32
00:02:24,990 --> 00:02:30,990
The reason why it's a float is because I know that that horizontal input is going to return a float
33
00:02:30,990 --> 00:02:31,860
value.
34
00:02:31,860 --> 00:02:39,480
And what I want to do is I want to say the horizontal input variable is equal to the input manager which
35
00:02:39,480 --> 00:02:44,340
is the input and then what was the hierarchal step to get to that access.
36
00:02:44,340 --> 00:02:53,210
Well we go to input accesses and then we specify the access we want I want the horizontal axis encode.
37
00:02:53,250 --> 00:03:00,600
You say input get access and then you open a parentheses and then we can pass in a string of the axis
38
00:03:00,600 --> 00:03:05,580
name anytime we're working with strings you're gonna use quotation marks and we're gonna type in here
39
00:03:05,850 --> 00:03:14,040
the horizontal axis if we save this and hop back into unity I'm going to close the input manager and
40
00:03:14,040 --> 00:03:21,030
run the game and watch what values appear next to this horizontal input in the inspector you'll see
41
00:03:21,030 --> 00:03:26,430
here that our character is not going to be controlled but if I hold down the left arrow it goes from
42
00:03:26,430 --> 00:03:31,710
zero to negative 1 if I hold down the right arrow it goes from zero to positive 1.
43
00:03:32,250 --> 00:03:40,320
Likewise if I hold down the 8 key or a negative on the D key is 1 if I'm not holding any keys the horizontal
44
00:03:40,320 --> 00:03:48,960
input value is 0 using that knowledge we can use this horizontal input variable and the way we can use
45
00:03:48,960 --> 00:03:53,940
this is I can basically I don't need to necessarily see it so I'm going to remove it from there and
46
00:03:53,940 --> 00:03:59,520
I'm going to create a local variable that's only used inside of update and it's going to be of type
47
00:03:59,520 --> 00:04:06,570
floats and it's going to be a horizontal input equals input I get access horizontal once I've declared
48
00:04:06,570 --> 00:04:09,180
that variable I'm going to move this to the top
49
00:04:12,010 --> 00:04:18,970
and then in our transformed not translate method I'm gonna say vector three dot right multiplied by
50
00:04:18,970 --> 00:04:24,320
our horizontal input multiplied by the speed multiplied by real time.
51
00:04:24,430 --> 00:04:32,020
And if I save this and we go back into unity like Unity compile and let's run the application I can
52
00:04:32,020 --> 00:04:40,240
now move using my d key to go write a key to go left right arrow key to go right and left Cherokee to
53
00:04:40,240 --> 00:04:43,450
go left and if I stop moving I stopped moving.
54
00:04:43,450 --> 00:04:49,540
Now let's take a look at how that works right now we declared our horizontal input using what's called
55
00:04:49,540 --> 00:04:56,170
a local variable of type float and we just set it to the input dock get access horizontal inside here
56
00:04:56,170 --> 00:04:58,160
I included the multiplication for that.
57
00:04:58,180 --> 00:05:04,300
So now we have vector 3 about right which is new vector 3 1 you know on the right multiplied by horizontal
58
00:05:04,300 --> 00:05:12,040
input which is let's say 0 when I start out multiplied by our speed which is three point five f mortified
59
00:05:12,040 --> 00:05:20,440
by real time if I'm not moving what's my vector if I'm not moving 1 times three point five becomes I'm
60
00:05:20,440 --> 00:05:29,320
sorry if I'm not moving one times zero is zero zero times three point five is zero and zero times real
61
00:05:29,320 --> 00:05:30,530
time is still zero.
62
00:05:30,550 --> 00:05:33,860
So we're not moving the vector length is zero.
63
00:05:33,940 --> 00:05:41,830
Now what happens if I hit the D key or the right arrow key horizontal input becomes one new vector three
64
00:05:41,830 --> 00:05:47,500
one times one is positive one one times three point five becomes three point five and then three point
65
00:05:47,490 --> 00:05:53,590
five times real time is meters per second I now move to the right three point five meters per second
66
00:05:54,130 --> 00:05:59,110
likewise what happens if I hold down the eight key or the left Cherokee this becomes a negative one
67
00:05:59,570 --> 00:06:05,650
and if we look at the math here we have vector three about right which is one times a negative one which
68
00:06:05,650 --> 00:06:07,010
becomes negative one.
69
00:06:07,030 --> 00:06:12,940
So now we're moving to the left negative one times three point five becomes negative three point five
70
00:06:13,740 --> 00:06:15,460
and then Z for the rest.
71
00:06:15,640 --> 00:06:20,950
So that's how the math is working underneath so as a challenge what I'd like you to do is figure out
72
00:06:21,100 --> 00:06:29,320
how to move vertically we're currently moving left and right like this go ahead and add the second access
73
00:06:29,320 --> 00:06:33,130
which is the vertical axis and go ahead and move up and down.
74
00:06:33,130 --> 00:06:34,360
I'll see you in the Challenger view.
8674
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.