Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:05,660 --> 00:00:12,270
In this video we are going to set a starting position for our player by creating our first see sharp
2
00:00:12,270 --> 00:00:15,600
script that's going to define him.
3
00:00:15,930 --> 00:00:21,750
That's going to define the characteristics and behavior of our player to do this.
4
00:00:21,750 --> 00:00:26,610
We're gonna create a new scripts folder by right clicking the assets folder selecting create and we're
5
00:00:26,610 --> 00:00:31,260
gonna create a new folder all of the scripts that are going to make up our video game are going to be
6
00:00:31,260 --> 00:00:37,980
stored inside of the scripts folder with the scripts folder selected you can right click and go to create
7
00:00:38,190 --> 00:00:40,790
and we're gonna create a new C sharp script.
8
00:00:40,890 --> 00:00:46,470
If you're completely new to coding you are in good hands and we're going to be walking through everything
9
00:00:46,470 --> 00:00:51,900
together as we always are going to be completing challenges that are going to help you become a self-learning
10
00:00:52,140 --> 00:00:56,330
motivated individual that can actually program and solve problems on your own.
11
00:00:56,400 --> 00:01:00,210
So to do this we're going to rename the file as player.
12
00:01:00,210 --> 00:01:05,280
It's important that you rename it while it's highlighted or you'll need to adjust it later.
13
00:01:05,280 --> 00:01:10,360
You need to make sure that the class name in the file name are exactly the same.
14
00:01:10,500 --> 00:01:16,050
You'll see here that if I suck the player we can see a preview of the script right here in the inspector.
15
00:01:16,050 --> 00:01:21,840
It's very important that the public class player the name player they're matches the file name.
16
00:01:21,840 --> 00:01:28,470
If I were to create a new C sharp script and then I click off and I lose that highlighter ability and
17
00:01:28,470 --> 00:01:29,600
then adjust it.
18
00:01:29,610 --> 00:01:37,740
So for example if I were to name this to my custom scripts you'll notice here that inside the actual
19
00:01:37,740 --> 00:01:43,770
class it says new behavior script and unity will actually give us an error when we try to use it.
20
00:01:43,770 --> 00:01:47,400
You need to make sure that the class name in the file on a match.
21
00:01:47,400 --> 00:01:48,480
It's a simple adjustment.
22
00:01:48,480 --> 00:01:53,160
Once we're in the script but it's convenient to name it properly the first time.
23
00:01:53,160 --> 00:01:59,190
So I'm going to just delete that script and let's double click our player script to open it in Visual
24
00:01:59,190 --> 00:02:02,120
Studio.
25
00:02:02,360 --> 00:02:07,340
So here we are in Visual Studio and you can see here that we have our public class player and then a
26
00:02:07,340 --> 00:02:15,830
colon and then model behavior that colon stands for inherits or extends Model Behavior model behavior.
27
00:02:15,830 --> 00:02:18,110
It's a unity specific term.
28
00:02:18,170 --> 00:02:24,710
This is what actually allows us to drag and drop scripts or behaviors onto game objects to control that
29
00:02:24,980 --> 00:02:29,990
and you'll be using model behaviors as well as custom classes which are just simple classes like this
30
00:02:30,380 --> 00:02:32,680
throughout your coding days with unity.
31
00:02:32,780 --> 00:02:37,730
Now with Unity model behavior gives us two functions out of the box you get.
32
00:02:37,760 --> 00:02:43,160
Void Start and void Update and you'll see here that we have a couple of comments that can be created
33
00:02:43,490 --> 00:02:46,250
with two slashes if you're completely new to coding.
34
00:02:46,250 --> 00:02:51,140
Don't worry I promise by the end of this course you will be on your way to becoming a self-sufficient
35
00:02:51,230 --> 00:02:56,810
software engineer so that you don't have to follow countless youtube tutorials to learn how to code.
36
00:02:56,870 --> 00:03:01,970
Let's go over the quick scripting outline that you see here at the very top.
37
00:03:01,970 --> 00:03:03,540
These are called name spaces.
38
00:03:03,560 --> 00:03:07,790
We're not going to get too focused on name spaces throughout this course but just know that they act
39
00:03:07,820 --> 00:03:14,420
as libraries that give you access to coding for example model behavior is specific to the using Unity
40
00:03:14,420 --> 00:03:15,530
engine namespace.
41
00:03:15,530 --> 00:03:20,610
If I remove this you'll see here that things turn white because it doesn't know what they are.
42
00:03:20,750 --> 00:03:28,010
In order to use them on a behavior of unity we have to include the namespace or library using Unity
43
00:03:28,010 --> 00:03:28,880
engine.
44
00:03:29,240 --> 00:03:35,420
In order to gain access to that code library the next two names faces here or libraries to access code
45
00:03:35,450 --> 00:03:38,670
or system that collections and system duck collections not generic.
46
00:03:38,690 --> 00:03:44,780
These are part of the dot net framework that allow us to write some C sharp code a part of the using
47
00:03:44,780 --> 00:03:49,150
Unity engine and model behavior is the void Start in void Update.
48
00:03:49,160 --> 00:03:52,700
These are methods that are called automatically from Unity for example.
49
00:03:52,730 --> 00:03:57,570
Void Start is called when you start the game in void Update is considered a game loop.
50
00:03:57,650 --> 00:04:03,770
It typically runs about 60 frames per second on a typical computer and it runs every frame.
51
00:04:04,340 --> 00:04:07,750
So that's where all of our logic and user input is going to go.
52
00:04:07,880 --> 00:04:10,670
Now for this what do we want to do with our player.
53
00:04:10,670 --> 00:04:15,500
This script is going to be attached the player and behave as the player and what we want to do right
54
00:04:15,500 --> 00:04:20,630
now is basically when we start the game let's go ahead and give us a starting position.
55
00:04:20,630 --> 00:04:27,230
So basically we're going to say take the current position and assign it a start position and let's say
56
00:04:27,230 --> 00:04:33,980
the current position of our character right now is at negative two point six seven on the Y when we
57
00:04:33,980 --> 00:04:34,910
start the game.
58
00:04:34,970 --> 00:04:37,700
I want to snap the position to zero.
59
00:04:37,730 --> 00:04:38,910
How can we do that.
60
00:04:39,080 --> 00:04:43,820
And what you need to get familiar with in unity in programming in general is the concept of writing
61
00:04:43,820 --> 00:04:44,780
out pseudocode.
62
00:04:44,780 --> 00:04:49,070
In comments you'll see here that the task is to take the current position whatever it's at.
63
00:04:49,070 --> 00:04:54,680
When we start the game and basically assign it a new position and we want to basically say new position
64
00:04:55,070 --> 00:04:59,810
and assign it a 0 4 x 0 4 y and a 0 for Z.
65
00:04:59,810 --> 00:05:02,710
So how do we do this in order to do this.
66
00:05:02,720 --> 00:05:05,160
Unity works in a very hierarchal structure.
67
00:05:05,210 --> 00:05:07,310
How do I get the current position.
68
00:05:07,310 --> 00:05:09,320
Well let's look at the game object.
69
00:05:09,380 --> 00:05:14,720
If I were to take the script and attach it to the game object by dropping it on the player in the hierarchy
70
00:05:15,080 --> 00:05:19,520
or by just dragging it in the inspector here it gets added as a component.
71
00:05:19,720 --> 00:05:20,930
And this is important.
72
00:05:20,930 --> 00:05:24,920
Unity is a component based design which is why it's so user friendly.
73
00:05:25,040 --> 00:05:28,070
I can simply just add behaviors to game objects.
74
00:05:28,070 --> 00:05:33,680
Right now this script is attached to our player game object and if I wanted to get the position of the
75
00:05:33,680 --> 00:05:35,390
player how do I do that.
76
00:05:35,420 --> 00:05:38,810
I first have to access the transform component.
77
00:05:38,810 --> 00:05:44,080
Then when someone the transform I can get the position rotation or scale.
78
00:05:44,420 --> 00:05:48,620
So we just looked at the visual representation for editing to do and let's take a look at it through
79
00:05:48,620 --> 00:05:49,490
code.
80
00:05:49,490 --> 00:05:51,140
I need to get the current position.
81
00:05:51,200 --> 00:05:57,530
Well I need to first access the transform of the object and then if I want the position we can use the
82
00:05:57,530 --> 00:05:59,820
dot operator to go further in.
83
00:05:59,840 --> 00:06:03,350
So I'm on the transform component dot position.
84
00:06:03,350 --> 00:06:08,130
If I wanted the rotation it would be transform dot rotation.
85
00:06:08,240 --> 00:06:11,430
Let's say I wanted the y value of the position.
86
00:06:11,510 --> 00:06:17,120
If I look on the player I access the transform the position and then I can go further in.
87
00:06:17,150 --> 00:06:24,740
So I could say X or Y and you'll see here that I can type transform dot rotation or transform dot position
88
00:06:25,130 --> 00:06:29,440
dot y so you can see here that's very hierarchal.
89
00:06:29,540 --> 00:06:34,390
It's what we want to do is take the current position and assign it a new position.
90
00:06:34,390 --> 00:06:40,090
The way we do that is through the use of what's called a vector 3 a vector three is what defines all
91
00:06:40,090 --> 00:06:41,670
position types of unity.
92
00:06:41,680 --> 00:06:46,810
Anything that is going to involve positioning of an A game object or an object and unity is going to
93
00:06:46,810 --> 00:06:53,350
be reassigned through what's called a vector 3 and it has to use the new keyword.
94
00:06:53,440 --> 00:06:59,560
So here if I type transform dot position which gets the current position let's take a look at the tooltip
95
00:06:59,620 --> 00:07:04,720
which you need to get in a habit of reading you'll see here that it's a vector three everything related
96
00:07:04,720 --> 00:07:09,700
to position in rotation data and unity is going to use a vector three.
97
00:07:09,760 --> 00:07:12,250
The next thing here is that it says get inset.
98
00:07:12,280 --> 00:07:17,970
That means that I can get the position as well as set the position in order to set the position.
99
00:07:18,010 --> 00:07:23,920
You use an equal sign to assign it in any time you're working with vector 3s you're always 100 percent
100
00:07:23,920 --> 00:07:29,560
of the time going to use a new vector three to define a new position.
101
00:07:29,560 --> 00:07:35,470
So here I'm taking the current position and I'm assigning it a new position by typing in New vector
102
00:07:35,470 --> 00:07:41,200
3 and if I open a parentheses you'll see here that we have three overrides I can say new vector three
103
00:07:41,200 --> 00:07:47,380
I like this I can specify an X and Y if we're in a 2D setting or I can create a new vector with given
104
00:07:47,410 --> 00:07:49,450
x y and z components.
105
00:07:49,480 --> 00:07:56,460
So here I want to say zero on the x zero on the y in zero on the Z.
106
00:07:56,530 --> 00:07:57,970
Let's save this.
107
00:07:58,420 --> 00:08:00,160
Hop back into unity.
108
00:08:00,160 --> 00:08:06,370
Make sure that you attach the script to the game object and you'll see here that I'm currently going
109
00:08:06,370 --> 00:08:13,500
to position myself at negative 2 and if I run the game we're going to snap to 0 0 0.
110
00:08:13,770 --> 00:08:14,550
And there you go.
111
00:08:14,550 --> 00:08:20,960
We created our first line of code which takes the current position and assigns it a new position.
112
00:08:21,090 --> 00:08:26,430
You should be able to begin to imagine the scenarios where reassigning positions and video games can
113
00:08:26,430 --> 00:08:27,690
be really handy.
114
00:08:27,690 --> 00:08:29,490
Think of a teleportation system.
115
00:08:29,490 --> 00:08:32,130
You walk into something and appear somewhere else.
116
00:08:32,130 --> 00:08:36,780
All they're doing there is just reassigning the position and playing a particle effect to make it look
117
00:08:36,780 --> 00:08:41,870
like you actually teleported somewhere where in reality you're just changing positions.
118
00:08:41,880 --> 00:08:42,900
I'll see you in the next video.
12922
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.