Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:05,670 --> 00:00:12,030
Now that we have user input working the next challenge is going to be restraining the player through
2
00:00:12,030 --> 00:00:13,140
player bounds.
3
00:00:13,620 --> 00:00:14,610
So let's check this out.
4
00:00:14,610 --> 00:00:20,070
I can move around freely and if I move all the way to the right let's say here that I wanted to prevent
5
00:00:20,070 --> 00:00:22,230
the player from moving any further.
6
00:00:22,230 --> 00:00:27,300
And if I go all the way down I can prevent the player from going down or if you want to get really fancy
7
00:00:27,450 --> 00:00:33,300
let's say I do move all the way to the right I wrap and appear all the way at the left in order to do
8
00:00:33,300 --> 00:00:38,130
this we need to understand what's going on in our view let's also restrain us from going above let's
9
00:00:38,130 --> 00:00:41,760
say zero on the y in order to do this.
10
00:00:41,790 --> 00:00:50,470
We simply need to check if the player on the x position here is let's say greater than or equal to nine.
11
00:00:50,520 --> 00:00:57,600
We can't move and if we're on the x position and we're less than or equal to let's say negative nine
12
00:00:58,320 --> 00:01:00,180
then we can't move any further.
13
00:01:00,180 --> 00:01:05,360
Or if you're all the way past let's say 11:00 you appear at 11:00.
14
00:01:05,370 --> 00:01:09,210
And likewise if you're all the way at 11:00 you appear a negative eleven.
15
00:01:09,210 --> 00:01:10,410
So we create sort of wrap.
16
00:01:10,920 --> 00:01:17,010
Likewise we say on the Y if the y position is greater than zero then what do we do.
17
00:01:17,010 --> 00:01:18,930
Well we need to set us back to zero.
18
00:01:18,990 --> 00:01:26,820
And if the y position is less then let's say negative three point eight we need to set us back to negative
19
00:01:26,820 --> 00:01:29,270
three point eight so that we can't move.
20
00:01:29,280 --> 00:01:33,740
So in order to do this we have to work with what's called an if statement.
21
00:01:33,960 --> 00:01:41,190
The next concept of fundamental learning C sharp and again check out the unity C sharp Survival Guide
22
00:01:41,400 --> 00:01:44,300
for dozens of challenges on mastering C sharp.
23
00:01:45,030 --> 00:01:50,250
So to do an if statement here what we need to do is we need to first type out the logic that we're trying
24
00:01:50,250 --> 00:01:51,290
to achieve here.
25
00:01:51,330 --> 00:01:53,490
I've gone ahead and I've cleaned up my script a bit.
26
00:01:53,490 --> 00:01:59,230
I've just removed some comments and I'm going to continue to do that throughout the course just to clean
27
00:01:59,230 --> 00:02:00,780
up our code.
28
00:02:00,780 --> 00:02:03,970
Now here what I have is what is the logic we're trying to do.
29
00:02:04,140 --> 00:02:09,810
Let's go ahead and restraints on just the top let's say I don't want to be able to go above zero on
30
00:02:09,810 --> 00:02:10,590
the Y.
31
00:02:10,740 --> 00:02:13,330
What is the logic in the pseudocode for this.
32
00:02:13,350 --> 00:02:15,570
I recommend you always type out your pseudocode.
33
00:02:15,570 --> 00:02:21,240
It's going to help you in the end become a better software engineer to type this out in pseudocode which
34
00:02:21,240 --> 00:02:30,810
is just comments were basically checking if the let's go ahead and say here if player position on the
35
00:02:30,810 --> 00:02:42,000
Y is greater than zero then we need to say here y position equals zero pseudocode.
36
00:02:42,000 --> 00:02:43,750
If then logic that's all we're doing here.
37
00:02:43,800 --> 00:02:47,280
This is called pseudocode where you type it out in English and then we're going to just convert it to
38
00:02:47,280 --> 00:02:54,950
code and if statement is very similar to if then logic basically if some condition is true then we're
39
00:02:54,950 --> 00:02:56,310
gonna do some logic.
40
00:02:56,340 --> 00:03:02,610
A great example of this just to quickly test it is let's say here we had two variables FLOAT A equals
41
00:03:02,610 --> 00:03:07,180
five AND FLOAT B equals 10.
42
00:03:07,260 --> 00:03:11,310
If I wanted to check if five is greater than 10 which it's not.
43
00:03:11,310 --> 00:03:12,760
How could I do that.
44
00:03:12,810 --> 00:03:18,990
Well I could type out an if statement which looks like this as the syntax and I could say here if A
45
00:03:19,170 --> 00:03:27,060
is greater than B that says if a which is five is greater than B then run this code.
46
00:03:27,060 --> 00:03:32,480
Now the question here is is that a true condition is a greater than B is five greater than ten.
47
00:03:32,490 --> 00:03:33,390
The answer is No.
48
00:03:33,390 --> 00:03:35,670
Therefore this code never runs.
49
00:03:36,150 --> 00:03:45,120
However if this was switched to where this is 10 and this is five is a greater than B Again it's 10
50
00:03:45,270 --> 00:03:46,310
greater than five.
51
00:03:46,380 --> 00:03:47,010
Yes.
52
00:03:47,010 --> 00:03:53,670
So now this statement runs we use if statements to control logic and instructions for our program.
53
00:03:53,730 --> 00:03:56,860
So what I wanna do here is I'm checking the player position on the Y.
54
00:03:56,880 --> 00:03:58,710
How do I get the Y position.
55
00:03:58,710 --> 00:04:07,520
Well if we look here in Unity I access the transform property the position followed by the y value.
56
00:04:07,520 --> 00:04:10,040
And that's going to give me the Y position at the current time.
57
00:04:10,650 --> 00:04:18,390
So I'm going to check if the y position of our player which is done through transform that position
58
00:04:18,590 --> 00:04:19,570
don't y.
59
00:04:19,580 --> 00:04:25,620
And I'm checking to see if it's greater than or equal to zero if it's greater than equal to zero.
60
00:04:25,620 --> 00:04:30,270
I need to set my entire position on the y back to zero.
61
00:04:30,270 --> 00:04:35,730
Now you might be thinking I could just say transform dot position dot y equals zero.
62
00:04:36,330 --> 00:04:42,390
However we can't do this in C sharp you're not allowed to do this because we're neglecting the x and
63
00:04:42,390 --> 00:04:43,080
z values.
64
00:04:43,080 --> 00:04:43,860
What about them.
65
00:04:43,860 --> 00:04:45,510
Are they just chopped liver.
66
00:04:45,510 --> 00:04:45,850
No.
67
00:04:45,930 --> 00:04:49,920
We need to acknowledge what happens to the x and z values.
68
00:04:50,040 --> 00:04:52,830
We want them to stay the exact same.
69
00:04:52,830 --> 00:04:58,770
Watch what happens here if I type transform not position equals let's say a new position.
70
00:04:58,770 --> 00:05:05,070
And the reason why I no that is because transform our position it's taking in a vector 3 and I can say
71
00:05:05,070 --> 00:05:06,290
get reset.
72
00:05:06,290 --> 00:05:12,400
So I'm going to set it to a new position and I'm going to say 0 0 0.
73
00:05:12,720 --> 00:05:13,530
By doing this.
74
00:05:13,530 --> 00:05:15,470
Watch what happens.
75
00:05:15,690 --> 00:05:20,050
I can go back into unity let it compile and I'm going to run the game.
76
00:05:20,050 --> 00:05:24,600
And when I run this I'm going to snap to zero and I can move around I can move left and right up and
77
00:05:24,600 --> 00:05:31,770
down I can still do everything but if I go to the top whatever happens I can go down I can go up and
78
00:05:31,770 --> 00:05:33,120
I'm restrained everything's working.
79
00:05:33,210 --> 00:05:39,660
But what happens now if I go all the way to the right and then up I snap to the center we have a bug
80
00:05:40,220 --> 00:05:42,490
it shouldn't be doing that why is it doing that.
81
00:05:42,540 --> 00:05:48,700
And the reason for that is because we reset the x and y positions to zero.
82
00:05:48,750 --> 00:05:54,600
When really I wanted the x value to stay whatever the current X is I just wanted the Y to be zero.
83
00:05:55,110 --> 00:06:00,870
So in order to fix this we have to tell the program to use our current x value.
84
00:06:01,020 --> 00:06:02,970
And how do you get the current x value.
85
00:06:03,060 --> 00:06:11,760
Well you say transform dot position dot X and that's going to say use the current x position whatever
86
00:06:11,760 --> 00:06:20,250
it is set the Y to Z set the Z to Z or zero by running the application again you'll see here that I
87
00:06:20,250 --> 00:06:27,180
can run this and no matter where I am on the x axis we will always be restrained to zero on the Y.
88
00:06:27,180 --> 00:06:30,750
And now I can move along but I can't go above.
89
00:06:30,780 --> 00:06:36,450
So now let's take a look at how to add the bottom now.
90
00:06:36,560 --> 00:06:38,620
In unity we have if statements.
91
00:06:38,660 --> 00:06:44,150
There's also something called an L statement which means that if this is not a true condition it will
92
00:06:44,150 --> 00:06:46,430
default to an else.
93
00:06:46,460 --> 00:06:51,580
So for example if chance went on that position that Y is greater than equal to zero is false.
94
00:06:51,740 --> 00:06:52,730
This will run.
95
00:06:53,630 --> 00:06:55,720
We also have something called in.
96
00:06:55,730 --> 00:07:05,640
Else if and else if statement in unity or in C sharp in general basically is checking the same condition
97
00:07:06,240 --> 00:07:07,330
I'm checking here.
98
00:07:07,380 --> 00:07:16,630
If the Y position is greater than 0 else if the y position is less then three point eight I want to
99
00:07:16,630 --> 00:07:23,580
do the same thing we use else ifs if we're checking the exact same property which we are in this case.
100
00:07:23,650 --> 00:07:29,560
So here in pseudocode we would type I'm just gonna remove that if player position on the Y is greater
101
00:07:29,560 --> 00:07:32,410
than zero Y position equal zero.
102
00:07:32,410 --> 00:07:41,520
Else if position on the Y is less than negative three point eight F because we're working with decimal
103
00:07:41,520 --> 00:07:50,280
values then what needs to happen y pose equals negative three point eight F so to convert that to actual
104
00:07:50,280 --> 00:07:55,130
code we would say here if and then else.
105
00:07:55,740 --> 00:08:02,790
So if chance when opposition Y is greater than equal to zero else if transform not position not y we
106
00:08:02,790 --> 00:08:07,260
only use the elses if we're checking the same condition here we check chance on our position not Y.
107
00:08:07,650 --> 00:08:09,690
And here we check transient opposition not Y.
108
00:08:10,140 --> 00:08:18,810
And now I'm checking if it's less than or equal to negative three point eight F and if it is then what
109
00:08:18,810 --> 00:08:21,210
are we going to do.
110
00:08:21,210 --> 00:08:29,610
If it is less than negative three point eight F we're simply going to take the current position and
111
00:08:29,610 --> 00:08:31,860
assign it a new position.
112
00:08:31,860 --> 00:08:41,110
I want to keep the current x position and then I want to assign a negative three point eight F on the
113
00:08:41,110 --> 00:08:45,310
Y let's save this and lets us it out
114
00:08:48,480 --> 00:08:54,250
will run the application start unity and you'll see here that I am now fully restraint from the top
115
00:08:54,250 --> 00:08:54,730
of the screen.
116
00:08:54,730 --> 00:08:58,680
I can't go above it and I can move freely on the X and I can't go below it.
117
00:08:58,690 --> 00:09:02,640
We've now restricted our player on the vertical axis as a challenge.
118
00:09:02,650 --> 00:09:09,970
I'd like you to go ahead and fully restrain the player on the left and as a bonus go ahead and make
119
00:09:09,970 --> 00:09:15,570
it so that when the player is all the way past the bounds they wrap to get you started.
120
00:09:15,610 --> 00:09:17,710
We'll go through the pseudocode together.
121
00:09:17,710 --> 00:09:20,860
We're going to use a new if statement because we're checking a new condition.
122
00:09:20,880 --> 00:09:24,820
I'm no longer checking the wire I'm now checking the X so here.
123
00:09:24,940 --> 00:09:34,540
If player on the x is greater than let's say 11:00 then X pose is going to equal negative negative 11.
124
00:09:34,540 --> 00:09:43,570
So if you go all the way to the right I want to appear on the left and then elusive player on the X
125
00:09:45,720 --> 00:09:48,400
is less than negative 11.
126
00:09:48,450 --> 00:09:53,090
Then why do we need to do X pose equals positive 11.
127
00:09:53,100 --> 00:09:53,910
So good luck.
128
00:09:53,910 --> 00:09:56,120
Give it a shot and I'll see you in the challenge review.
13255
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.