1
00:00:00,000 --> 00:00:00,000
Hello, guys.

2
00:00:00,000 --> 00:00:04,000
In this video, we're going to make a move and trap the trap that is kind of patrolling from left to

3
00:00:04,000 --> 00:00:05,000
right.

4
00:00:05,000 --> 00:00:09,000
Or if I could say it in another words, moving from one move point to another.

5
00:00:09,000 --> 00:00:10,000
Let's do this.

6
00:00:10,000 --> 00:00:17,000
Let's create two D sprites and I want to choose Hexagon pointer top.

7
00:00:17,000 --> 00:00:25,000
Yeah, this one let's name is moving Trap I guess certain layer platform minus one.

8
00:00:26,000 --> 00:00:29,000
And let's reset position to zero.

9
00:00:31,000 --> 00:00:34,000
And color should be red because it's dangerous.

10
00:00:35,000 --> 00:00:37,000
Now we have visuals for this trap.

11
00:00:37,000 --> 00:00:40,000
What I want to do next is create a script.

12
00:00:40,000 --> 00:00:43,000
And I'm going to name it as moving track.

13
00:00:44,000 --> 00:00:47,000
Now, what would be your first instinct to make this trap?

14
00:00:47,000 --> 00:00:48,000
I guess you need to make a damage.

15
00:00:48,000 --> 00:00:52,000
And for that you could go and create on trigger enter.

16
00:00:53,000 --> 00:00:53,000
Right?

17
00:00:53,000 --> 00:00:55,000
Yes, that is absolutely correct.

18
00:00:55,000 --> 00:00:58,000
But we use an object oriented programming.

19
00:00:59,000 --> 00:01:04,000
And for there, we need to reuse code as much as possible.

20
00:01:04,000 --> 00:01:10,000
We can copy code from here, just copy and paste like this.

21
00:01:10,000 --> 00:01:15,000
But it's not a good practice If you can make it in a different way and you can make it in a different

22
00:01:15,000 --> 00:01:18,000
way, you can use inheritance.

23
00:01:18,000 --> 00:01:23,000
It's one of the main principles of object oriented programming, inheritance, how you do it.

24
00:01:23,000 --> 00:01:29,000
So as of now, this script is inherited from a behavior, and this script is inherited from monarch

25
00:01:29,000 --> 00:01:29,000
behavior.

26
00:01:30,000 --> 00:01:35,000
But we can go here and instead of modern behavior, we can just type trap.

27
00:01:36,000 --> 00:01:42,000
And now whatever information is in the trap can be passed to move in trap.

28
00:01:42,000 --> 00:01:48,000
For example, I'm going to create public float damage.

29
00:01:48,000 --> 00:01:49,000
All right.

30
00:01:50,000 --> 00:01:57,000
Now I have here variable damage and if in the start I would like to assign it to like 50 or something

31
00:01:57,000 --> 00:01:57,000
day much 50.

32
00:01:57,000 --> 00:02:00,000
I could and I could go to move in trap.

33
00:02:02,000 --> 00:02:05,000
Create the start function and assign damage to 50.

34
00:02:05,000 --> 00:02:09,000
And move in trap would have its own variable damage.

35
00:02:09,000 --> 00:02:10,000
Not like in the trap.

36
00:02:10,000 --> 00:02:16,000
So basically it's another object with the properties and functions of this object.

37
00:02:16,000 --> 00:02:18,000
But what if we don't want to make it public?

38
00:02:18,000 --> 00:02:19,000
We want to make it private.

39
00:02:19,000 --> 00:02:20,000
There is a way to do that.

40
00:02:20,000 --> 00:02:28,000
But this time, instead of private, you type protected and now it is available only to this script

41
00:02:28,000 --> 00:02:32,000
or for any script that is inherited from the script.

42
00:02:32,000 --> 00:02:35,000
But if you make it private, you can see in the movie Trap.

43
00:02:35,000 --> 00:02:37,000
Now I don't have access to damage.

44
00:02:39,000 --> 00:02:41,000
It shows me an error.

45
00:02:41,000 --> 00:02:43,000
All right, so what are we going to do here?

46
00:02:43,000 --> 00:02:45,000
We're going to delete this.

47
00:02:45,000 --> 00:02:52,000
And instead of private void, we're going to do protected virtual void on trigger enter, blah blah.

48
00:02:53,000 --> 00:02:57,000
And I guess I want to make it a bit prettier like this.

49
00:02:57,000 --> 00:02:58,000
I like it more.

50
00:02:58,000 --> 00:03:03,000
Now we go to move in trap, delete this, and I'm going to type override.

51
00:03:04,000 --> 00:03:09,000
And I can choose from the list on trigger enter to the.

52
00:03:09,000 --> 00:03:17,000
So this one line of code contains all of the stuff that is written here and you can adjust it and add

53
00:03:17,000 --> 00:03:18,000
some other stuff.

54
00:03:18,000 --> 00:03:21,000
So you would get damage and you would debug a message and so on.

55
00:03:21,000 --> 00:03:23,000
Let's try and do that.

56
00:03:23,000 --> 00:03:26,000
Divide log damaged by moving trap.

57
00:03:28,000 --> 00:03:31,000
And here I would write the backlog.

58
00:03:33,000 --> 00:03:35,000
Damaged by regular trop.

59
00:03:37,000 --> 00:03:38,000
And I need the curly brackets for that.

60
00:03:38,000 --> 00:03:39,000
Sorry.

61
00:03:43,000 --> 00:03:45,000
Okay, let's go and see the difference.

62
00:03:45,000 --> 00:03:46,000
Going to enter play mode.

63
00:03:46,000 --> 00:03:51,000
And now look, if I'll enter my player into the regular trap.

64
00:03:51,000 --> 00:03:52,000
I'll see.

65
00:03:52,000 --> 00:03:54,000
Message damaged by regular trap.

66
00:03:54,000 --> 00:03:55,000
All right.

67
00:03:55,000 --> 00:03:56,000
Let's try again.

68
00:04:01,000 --> 00:04:03,000
And this time I'll use moving trap.

69
00:04:05,000 --> 00:04:05,000
And wait.

70
00:04:05,000 --> 00:04:09,000
We need on trigger enter right circle Collider.

71
00:04:10,000 --> 00:04:11,000
Is trigger.

72
00:04:11,000 --> 00:04:12,000
So.

73
00:04:12,000 --> 00:04:13,000
Boom.

74
00:04:13,000 --> 00:04:14,000
Damaged by regular chop.

75
00:04:14,000 --> 00:04:16,000
And damaged by moving.

76
00:04:16,000 --> 00:04:16,000
Chop.

77
00:04:16,000 --> 00:04:17,000
So you got the idea.

78
00:04:17,000 --> 00:04:19,000
Now let's actually give it a collider.

79
00:04:20,000 --> 00:04:23,000
And I want to make it a bit smaller than it should be.

80
00:04:25,000 --> 00:04:27,000
Again, just to make it fair game.

81
00:04:27,000 --> 00:04:28,000
Yeah.

82
00:04:30,000 --> 00:04:31,000
And let's go back.

83
00:04:31,000 --> 00:04:33,000
So now you have the idea how it works.

84
00:04:35,000 --> 00:04:37,000
Can I delete the bug from here?

85
00:04:41,000 --> 00:04:43,000
And in the moving trap, I'm going to delete the.

86
00:04:43,000 --> 00:04:46,000
And now we're going to make functionality of the moving trap.

87
00:04:46,000 --> 00:04:47,000
Exactly.

88
00:04:47,000 --> 00:04:48,000
How are we going to move it?

89
00:04:48,000 --> 00:04:49,000
Very simple.

90
00:04:49,000 --> 00:04:51,000
First, we need a speed.

91
00:04:51,000 --> 00:04:56,000
How fast is going to move So serialize field private float speed.

92
00:04:58,000 --> 00:04:59,000
Also in the rotated crop.

93
00:04:59,000 --> 00:05:00,000
So it looks pretty.

94
00:05:00,000 --> 00:05:05,000
So I'm going to create serialize field, private float rotation speed.

95
00:05:06,000 --> 00:05:09,000
And now we need a position where are we going to move to?

96
00:05:09,000 --> 00:05:14,000
So the destination for us for this trap, how are we going to create it?

97
00:05:14,000 --> 00:05:22,000
With the help of transform, serialize, field, private transform, move point.

98
00:05:22,000 --> 00:05:23,000
Right.

99
00:05:23,000 --> 00:05:32,000
So if you would like to have a couple of move points, you could do like move .1. to 1.3, but that

100
00:05:32,000 --> 00:05:34,000
is actually a wrong way to do that.

101
00:05:34,000 --> 00:05:37,000
What you can do is use an array.

102
00:05:38,000 --> 00:05:46,000
So after type of variable, you just put square brackets and now it will be an array of transforms.

103
00:05:46,000 --> 00:05:47,000
Let me show you.

104
00:05:48,000 --> 00:05:51,000
We actually did this with a little part, right?

105
00:05:51,000 --> 00:05:54,000
I forgot, so I didn't have to really explain again.

106
00:05:54,000 --> 00:05:56,000
So now I have more endpoints.

107
00:05:56,000 --> 00:05:56,000
Zero.

108
00:05:57,000 --> 00:06:00,000
If I'll create a couple of them move point.

109
00:06:01,000 --> 00:06:03,000
I'll give it the gizmos over a dot.

110
00:06:04,000 --> 00:06:06,000
I'll duplicate it a couple of times.

111
00:06:06,000 --> 00:06:07,000
And now.

112
00:06:08,000 --> 00:06:11,000
One, two, three.

113
00:06:12,000 --> 00:06:15,000
I move points and I can get access to that move points with a number.

114
00:06:15,000 --> 00:06:24,000
So I'm going to create an int for the number private int and I'll just give it a name of an I.

115
00:06:24,000 --> 00:06:30,000
And then in the update I'm going to move transform of my trap with the help of Vector Tree move towards.

116
00:06:30,000 --> 00:06:32,000
So we do this transform position.

117
00:06:33,000 --> 00:06:35,000
Equals to vector three.

118
00:06:35,000 --> 00:06:36,000
Move towards.

119
00:06:36,000 --> 00:06:42,000
And it allows us to move from current position to target position by choosing vector three.

120
00:06:42,000 --> 00:06:48,000
We're going to choose from current position, transform position to move point with the I.

121
00:06:50,000 --> 00:06:51,000
That position.

122
00:06:52,000 --> 00:06:53,000
And now we need to choose speed.

123
00:06:53,000 --> 00:06:56,000
And I want to type speed multiplied by time.

124
00:06:57,000 --> 00:06:59,000
Don at the time.

125
00:06:59,000 --> 00:07:03,000
So it will move, not accordingly to the frame update, but accordingly to time the delta time.

126
00:07:03,000 --> 00:07:05,000
Let's go and see this first.

127
00:07:06,000 --> 00:07:10,000
So currently my eye is zero by default, so it's going to choose zero.

128
00:07:10,000 --> 00:07:11,000
Move point.

129
00:07:11,000 --> 00:07:12,000
First one.

130
00:07:13,000 --> 00:07:15,000
Let me take it and move it away.

131
00:07:16,000 --> 00:07:17,000
Just like this.

132
00:07:17,000 --> 00:07:20,000
I'm going to set speed for.

133
00:07:20,000 --> 00:07:21,000
I'm going to play mode.

134
00:07:23,000 --> 00:07:25,000
We can see it's moving there.

135
00:07:27,000 --> 00:07:27,000
Let's click.

136
00:07:27,000 --> 00:07:28,000
This is Trigger then.

137
00:07:28,000 --> 00:07:29,000
Now it's not here.

138
00:07:30,000 --> 00:07:38,000
Now how we could change that, how we could make it move between points, we could just change the number

139
00:07:38,000 --> 00:07:39,000
of the AI.

140
00:07:39,000 --> 00:07:47,000
So let's check this if vector to the distance and it allows us to check distance between two objects.

141
00:07:47,000 --> 00:07:57,000
So if distance between transform position and move point with the eye position is less than let's try

142
00:07:57,000 --> 00:08:02,000
point 25 F, then we're going to do a plus plus.

143
00:08:02,000 --> 00:08:06,000
So we're going to do plus one and we're going to check if I.

144
00:08:07,000 --> 00:08:12,000
Is bigger than 1.. lengths.

145
00:08:13,000 --> 00:08:17,000
Then we're going to do I equals to zero so it won't exceed the limit.

146
00:08:17,000 --> 00:08:18,000
Let's go and see that.

147
00:08:19,000 --> 00:08:23,000
I'm going to place this more points in the different places just like that.

148
00:08:28,000 --> 00:08:30,000
Boom cycling between them.

149
00:08:30,000 --> 00:08:31,000
Oh, an exit bounce.

150
00:08:31,000 --> 00:08:32,000
One, three.

151
00:08:33,000 --> 00:08:34,000
Wait a second.

152
00:08:34,000 --> 00:08:34,000
Why is that?

153
00:08:35,000 --> 00:08:36,000
Oh, yeah.

154
00:08:36,000 --> 00:08:38,000
Let's put bigger or equals here.

155
00:08:38,000 --> 00:08:38,000
Yeah.

156
00:08:42,000 --> 00:08:44,000
And cycling between.

157
00:08:45,000 --> 00:08:45,000
Very cool.

158
00:08:46,000 --> 00:08:48,000
Now I want to make it rotate.

159
00:08:49,000 --> 00:08:54,000
How we could do a rotation, we could change Z with the time on Delta time, so our rotation on the

160
00:08:54,000 --> 00:08:58,000
Z would be like that and it will behave like it's rotating.

161
00:08:59,000 --> 00:09:01,000
Let's put zero here.

162
00:09:01,000 --> 00:09:03,000
Go back to script.

163
00:09:03,000 --> 00:09:05,000
Over here in the update.

164
00:09:05,000 --> 00:09:09,000
We can check if our position is bigger than destination on the X.

165
00:09:09,000 --> 00:09:17,000
If transform position that x is bigger than move point a dot position means.

166
00:09:17,000 --> 00:09:22,000
We are from the left of the moving point means we move into the right.

167
00:09:22,000 --> 00:09:24,000
And actually, it doesn't matter.

168
00:09:24,000 --> 00:09:25,000
We can just rotate to any side.

169
00:09:25,000 --> 00:09:26,000
It will look pretty.

170
00:09:26,000 --> 00:09:27,000
Transform.

171
00:09:27,000 --> 00:09:28,000
Rotate.

172
00:09:29,000 --> 00:09:31,000
And we need an oiler here.

173
00:09:31,000 --> 00:09:34,000
I'll just do a new vector 300.

174
00:09:34,000 --> 00:09:39,000
And for the Z, it's going to be rotation speed multiplied by time, dot, delta time.

175
00:09:41,000 --> 00:09:50,000
Position that X and let's do another one else transform rotate new vector 300 minus.

176
00:09:50,000 --> 00:09:55,000
So it's going to be another side rotation speed multiplied by time to time.

177
00:09:55,000 --> 00:09:59,000
Let's save it all and go set some rotation speed.

178
00:10:00,000 --> 00:10:01,000
Let's try to 100.

179
00:10:04,000 --> 00:10:04,000
Look.

180
00:10:05,000 --> 00:10:07,000
Let's see the unemployment actually.

181
00:10:08,000 --> 00:10:10,000
Can I move them here?

182
00:10:11,000 --> 00:10:12,000
Oh, my God.

183
00:10:13,000 --> 00:10:14,000
It looks pretty, right?

184
00:10:15,000 --> 00:10:16,000
I like it.

185
00:10:16,000 --> 00:10:18,000
You can place as many move points as you like.

186
00:10:18,000 --> 00:10:23,000
What I want to do here is kind of inconvenient to drag four objects together.

187
00:10:23,000 --> 00:10:26,000
What I want to do here is to create an empty pattern for them.

188
00:10:27,000 --> 00:10:30,000
And I'm going to name it as moving up again.

189
00:10:32,000 --> 00:10:35,000
So now they are all together and I can move them like this.

190
00:10:35,000 --> 00:10:38,000
I also want to delete one of the move points.

191
00:10:38,000 --> 00:10:39,000
I don't like it.

192
00:10:40,000 --> 00:10:42,000
So just two of them.

193
00:10:43,000 --> 00:10:47,000
And I'll place one I know here and one here.

194
00:10:48,000 --> 00:10:49,000
Kind of last touch here.

195
00:10:49,000 --> 00:10:56,000
I want to set position of the trap equals to first move point so it will be spawned on the move point.

196
00:10:57,000 --> 00:11:00,000
And it will look pretty and kind of predictable.

197
00:11:00,000 --> 00:11:09,000
So just in the start, I can return just one line of code transform position equals to move .0. position.

198
00:11:11,000 --> 00:11:14,000
So now, no matter what, it's going to be found at first position.

199
00:11:15,000 --> 00:11:16,000
Let's go and test.

200
00:11:16,000 --> 00:11:17,000
First point.

201
00:11:17,000 --> 00:11:18,000
Second point.

202
00:11:18,000 --> 00:11:19,000
And.

203
00:11:20,000 --> 00:11:20,000
Yeah.

204
00:11:22,000 --> 00:11:25,000
But it's not necessarily honestly, just something I want to do.

205
00:11:25,000 --> 00:11:26,000
All right.

206
00:11:26,000 --> 00:11:27,000
Good enough.

207
00:11:27,000 --> 00:11:29,000
I think it takes long to make this video.

208
00:11:29,000 --> 00:11:30,000
So let's go to prefab.

209
00:11:30,000 --> 00:11:33,000
I kinda carry the weight by explanation of different points.

210
00:11:34,000 --> 00:11:39,000
Let's just drag moving track to the prefab folder and we almost there in the next video, we're going

211
00:11:39,000 --> 00:11:42,000
to change colors of the platform and then we can build level parts.

212
00:11:42,000 --> 00:11:42,000
See you there.

