Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:05,740 --> 00:00:10,320
In this video we are going to clean up our code inside of our player class.
2
00:00:10,320 --> 00:00:12,400
As of right now we've just finished the.
3
00:00:12,450 --> 00:00:16,610
Player behavior for everything related to movement.
4
00:00:16,710 --> 00:00:23,670
Now the core principle of object oriented programming is to effectively organize your code and be able
5
00:00:23,670 --> 00:00:27,900
to efficiently debug your programs.
6
00:00:27,900 --> 00:00:33,900
For example if we ever have anything wrong with our player movement we need an easy way to detect where
7
00:00:33,900 --> 00:00:36,560
our player movement code is and fix it.
8
00:00:36,600 --> 00:00:40,890
As you can see right now we're using the Update method which is our game loop and everything is being
9
00:00:40,890 --> 00:00:46,530
called inside of update and we're going to be continuing to add core mechanics and features to our game
10
00:00:46,620 --> 00:00:48,150
using the Update method.
11
00:00:48,150 --> 00:00:53,130
What's gonna happen eventually is if we continue to just utilize the Update method we're gonna run into
12
00:00:53,130 --> 00:00:56,310
a problem where it's very difficult to debug our programs.
13
00:00:56,310 --> 00:01:02,100
So one of the core concepts of object oriented programming is to organize our code by doing that we
14
00:01:02,100 --> 00:01:05,400
can create our own custom methods.
15
00:01:05,400 --> 00:01:09,990
So for example void Update is automatically called from the model behavior on its own.
16
00:01:10,050 --> 00:01:12,750
As long as it's attached to a game object this will run.
17
00:01:12,750 --> 00:01:20,370
However what we can do is we can come down here and we can create our own method let's call this void
18
00:01:21,000 --> 00:01:22,200
calculate movement
19
00:01:25,100 --> 00:01:29,300
this method is going to be responsible for all things movement related.
20
00:01:29,390 --> 00:01:33,170
I'm going to take the movement code that's inside of update.
21
00:01:33,170 --> 00:01:39,260
I'm going to select it all and I'm going to right click or command see.
22
00:01:39,470 --> 00:01:41,460
You can also just cut it if you'd like.
23
00:01:41,460 --> 00:01:44,800
And I'm going to paste it inside of the calculate movement method.
24
00:01:44,960 --> 00:01:50,450
All of our code is now inside of this method related to movement inside a method called calculate movement
25
00:01:50,690 --> 00:01:54,330
if there's ever a bug or ever a problem with our movement code.
26
00:01:54,410 --> 00:01:56,020
I know exactly where to look.
27
00:01:56,120 --> 00:02:01,410
It's related to movement which means it has to be inside of this calculate movement method.
28
00:02:01,430 --> 00:02:03,440
This method here can be called anything.
29
00:02:03,470 --> 00:02:09,700
Void is just a default return type it means that the code runs from top to bottom and that's it.
30
00:02:09,770 --> 00:02:13,460
Now unlike update this is only called when we call this method.
31
00:02:13,460 --> 00:02:18,130
Update is automatically called once per frame or 60 frames per second.
32
00:02:18,170 --> 00:02:23,990
We need to call this from update to call a function you simply say the function name and you initialize
33
00:02:23,990 --> 00:02:25,490
it with parentheses.
34
00:02:25,490 --> 00:02:31,440
So now an update we're calculating our movement every frame we're going to call all of this code.
35
00:02:31,460 --> 00:02:36,810
However we just cleaned it up to where now our update is clean for other code that we're going to create.
36
00:02:36,860 --> 00:02:39,740
And we've organized our movement code inside of here.
37
00:02:39,770 --> 00:02:43,750
The last thing I'm going to do is I'm just going to clean up our code a bit and remove any of the pseudocode
38
00:02:43,820 --> 00:02:44,560
that we have.
39
00:02:44,600 --> 00:02:46,370
That is no longer needed.
40
00:02:46,370 --> 00:02:50,090
When it comes to commenting your code you really only have to come in your code.
41
00:02:50,090 --> 00:02:55,580
If you can't understand what's going on just by reading it I know that this is horizontal input vertical
42
00:02:55,580 --> 00:02:55,870
input.
43
00:02:55,880 --> 00:02:59,420
I don't need comments over it that tell me what that is the direction.
44
00:02:59,420 --> 00:03:02,070
I know that this is calculating the direction translate.
45
00:03:02,090 --> 00:03:04,070
I know that this is doing the translation.
46
00:03:04,070 --> 00:03:07,610
For example here if transform not position that Y is greater than zero.
47
00:03:07,700 --> 00:03:11,960
I don't need a comment anymore telling me what it's doing if I can read it in plain English what it's
48
00:03:11,960 --> 00:03:12,800
doing.
49
00:03:12,800 --> 00:03:15,110
So that's your rule of thumb for when to use comments.
50
00:03:15,140 --> 00:03:17,550
If it's not clear at first glance what's happening.
51
00:03:17,720 --> 00:03:20,990
Put in the comment OK.
52
00:03:21,060 --> 00:03:24,860
So we now have our player movement here.
53
00:03:25,020 --> 00:03:30,960
And one thing I want to show you guys if you're interested in clamping the movements for your y position
54
00:03:30,960 --> 00:03:31,800
here like this.
55
00:03:31,890 --> 00:03:37,710
There is one more optimized way to do that and it's using a math method in unity called clamping.
56
00:03:37,710 --> 00:03:43,830
Right now I'm clamping the values between negative three point eight in zero and alternative method
57
00:03:43,830 --> 00:03:44,880
to do that.
58
00:03:44,880 --> 00:03:56,160
Other than using if l sifts here is that I could just say I could just say transform dot position equals
59
00:03:56,220 --> 00:03:59,210
a new vector 3 and I'm going to pass in.
60
00:03:59,220 --> 00:04:06,030
Let's say we don't care about the X so transform not position x and then I can say math f dot clamp
61
00:04:06,450 --> 00:04:13,200
and math f that clamp allows us to clamp a value between a min and max the value that I want to clamp
62
00:04:13,740 --> 00:04:20,370
is the transformed position about Y and the value for minimum is going to be negative three point eight
63
00:04:20,400 --> 00:04:29,790
F and the max is going to be zero and then I can add a Z zero for Z with this line of code is doing
64
00:04:29,850 --> 00:04:35,940
is it's going to clamp the transform position y between negative three point eight and zero eliminating
65
00:04:35,940 --> 00:04:42,700
the need for this if else statement now I can't do that for the rapping because I don't want to clamp
66
00:04:42,700 --> 00:04:43,060
it.
67
00:04:43,070 --> 00:04:48,390
I need to make sure that I can go above it so that I can get the default or get the reverse of it.
68
00:04:48,470 --> 00:04:53,710
So if I save this and hop back into unity and by all means you do not have to copy what I'm doing.
69
00:04:53,720 --> 00:04:56,780
I want you guys to have your own unique implementations here.
70
00:04:56,810 --> 00:05:01,730
I'm just going along and thinking of best practices that I think you should know in case in the future
71
00:05:01,730 --> 00:05:06,780
you want to get more advanced you'll see here that I can now run the application.
72
00:05:06,850 --> 00:05:08,640
I can still walk to the left and right.
73
00:05:08,650 --> 00:05:15,210
So if I go all the way to the right I can walk to the left and if I go all the way down I can't go below
74
00:05:15,220 --> 00:05:17,290
and if I go all the way up I can't go above.
75
00:05:18,460 --> 00:05:21,930
So that inline piece of code there is clamping us.
76
00:05:22,020 --> 00:05:22,760
All righty.
77
00:05:23,080 --> 00:05:25,350
So our calculate movement is looking great.
78
00:05:25,360 --> 00:05:30,370
Make sure you're calling calculate movement update and that's going to conclude everything related to
79
00:05:30,370 --> 00:05:31,840
our player movement.
80
00:05:31,840 --> 00:05:33,610
I'll see you guys in the next section.
8398
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.