Would you like to inspect the original subtitles? These are the user uploaded subtitles that are being translated:
1
00:00:03,530 --> 00:00:07,070
And this video, we're going to shoot a recast out of Atlanta.
2
00:00:07,410 --> 00:00:09,970
But we're going to do so at the position of the crosshairs.
3
00:00:09,980 --> 00:00:13,100
So whatever is in front of the cross area is basically can they get hit?
4
00:00:14,090 --> 00:00:19,220
Now, you might be thinking, how do other people know how to script these things?
5
00:00:19,220 --> 00:00:21,940
You know, how do you know how to write a recast?
6
00:00:21,950 --> 00:00:24,170
You know, where do I learn this sort of stuff?
7
00:00:24,710 --> 00:00:29,190
Well, it's actually part of the script in reference, if you know what you want to achieve.
8
00:00:29,210 --> 00:00:31,400
You can actually go into the script in reference.
9
00:00:31,850 --> 00:00:34,460
And let's say that I just type recast.
10
00:00:37,610 --> 00:00:40,370
Oh, I mean, in Google for some reason.
11
00:00:40,400 --> 00:00:43,090
Oh, it's because I typed it in the top there.
12
00:00:44,930 --> 00:00:51,200
So if I do recast and you basically get all these different elements, that's going to explain our Reekers
13
00:00:51,200 --> 00:00:51,730
works.
14
00:00:51,740 --> 00:00:54,200
And the one that we're looking at here is recast, hit.
15
00:00:54,800 --> 00:01:00,110
OK, this is going to explain all the different things that you can do with a recast in terms of like
16
00:01:00,110 --> 00:01:01,250
whatever it's hit.
17
00:01:01,850 --> 00:01:04,450
Well, the one that we're interested in is collider.
18
00:01:04,970 --> 00:01:05,270
Right.
19
00:01:05,780 --> 00:01:10,320
And it's actually going to show you how to actually script that, how to put it together.
20
00:01:10,340 --> 00:01:15,500
So you need a variable of recast hit or a type or whether it's called hit.
21
00:01:15,830 --> 00:01:19,490
And then you need a type of ray that simply called ray.
22
00:01:19,850 --> 00:01:21,570
You choose where that's going to come from.
23
00:01:21,590 --> 00:01:24,320
So it's from the camera, the main camera.
24
00:01:24,590 --> 00:01:27,170
And then this is the position where the ray is fired from.
25
00:01:27,590 --> 00:01:30,110
And then you need a bit of physics to recast.
26
00:01:30,110 --> 00:01:31,670
And it's done like this.
27
00:01:32,030 --> 00:01:36,410
And then you you basically listen if it's actually hit something.
28
00:01:37,310 --> 00:01:39,590
OK, so let's let's actually.
29
00:01:39,590 --> 00:01:40,250
Right, Nathman.
30
00:01:40,280 --> 00:01:45,080
So in our script, first thing we need is obviously recast.
31
00:01:46,310 --> 00:01:48,380
So off type recast, hit.
32
00:01:49,610 --> 00:01:56,510
And there were those who on the recast hit and I'm simply going to call this hit.
33
00:01:57,650 --> 00:02:01,850
OK, that's going to store the value of whatever hits or whatever character.
34
00:02:02,690 --> 00:02:07,670
Then in our shoot, we can now do all of our work in here.
35
00:02:08,000 --> 00:02:11,590
Bear in mind, this only gets called when we press the button.
36
00:02:11,600 --> 00:02:13,790
It's not getting called every frame she's got.
37
00:02:13,910 --> 00:02:18,470
It's only being called, let's say, less than once a second, which is great.
38
00:02:19,100 --> 00:02:26,150
OK, so we're simply going to say of type Ray that we're simply going to call ray equals camera dot
39
00:02:26,150 --> 00:02:31,760
mean dot screen point to Ray.
40
00:02:33,140 --> 00:02:36,710
And then he's asking knows where is the position?
41
00:02:36,710 --> 00:02:38,520
Where do we want to start that from?
42
00:02:38,960 --> 00:02:41,680
Well, it's from the crosshairs dot position.
43
00:02:43,910 --> 00:02:46,880
It's going to use that position and.
44
00:02:49,190 --> 00:02:49,460
Oh.
45
00:02:51,720 --> 00:02:55,200
Of course, is because my CROSSFIRE is a game objects.
46
00:02:55,670 --> 00:03:00,060
It only knows position if I'm using it as a transforms or transformed up position.
47
00:03:00,180 --> 00:03:00,690
There we go.
48
00:03:01,530 --> 00:03:09,420
OK, so now that I'm actually firing that ray from the crosshairs, then what I need to do is do some
49
00:03:09,420 --> 00:03:10,020
physics.
50
00:03:10,020 --> 00:03:16,350
So I'm going to say if physics starts recast.
51
00:03:19,410 --> 00:03:27,990
And then expect in the ray, which we called Ray, and then out it, OK, it's going to shoot the ray
52
00:03:28,000 --> 00:03:31,160
out, it's going to store whatever it hits in variable.
53
00:03:31,680 --> 00:03:33,180
And then we've got a distance.
54
00:03:33,390 --> 00:03:33,690
All right.
55
00:03:33,690 --> 00:03:35,130
I'm going to say five, four about.
56
00:03:35,820 --> 00:03:37,170
Don't even need a thousand.
57
00:03:37,290 --> 00:03:38,190
That's way too far.
58
00:03:38,940 --> 00:03:44,520
I'm going to say 200, because we know that scene only lasts about 100 pixels.
59
00:03:45,360 --> 00:03:49,200
So it's going to fire out around 200 meters.
60
00:03:49,650 --> 00:03:54,870
And whatever it hits, it's going to get stored in the variable the way it's used in this this one that
61
00:03:54,870 --> 00:03:56,730
comes directly from the camera transform.
62
00:03:57,750 --> 00:04:14,220
So what we need to do now is say if hit the heart, transform and compare tag side of bracket's as a
63
00:04:14,220 --> 00:04:14,760
string.
64
00:04:14,910 --> 00:04:16,110
Let's say we're a zombie.
65
00:04:16,860 --> 00:04:17,080
OK.
66
00:04:17,340 --> 00:04:18,600
Simply call it zombie.
67
00:04:19,260 --> 00:04:22,560
If that's the case, then it's going to do something.
68
00:04:24,060 --> 00:04:26,880
Now, at the moment, we don't have a tag of zombie.
69
00:04:27,420 --> 00:04:29,160
So let's sort that out, first of all.
70
00:04:30,630 --> 00:04:32,220
Let's just go back into unity.
71
00:04:34,830 --> 00:04:36,090
And wait for that to compile.
72
00:04:38,590 --> 00:04:46,770
So let's get close to a zombie, and at the moment he's got this slight little cylinder around him,
73
00:04:46,770 --> 00:04:53,910
but what he now needs is a box collider, something to click add component of type box collider.
74
00:04:54,750 --> 00:04:59,580
It's a little bit small at the moment, so I'm just going to raise it up on the way I want.
75
00:04:59,940 --> 00:05:00,360
All right.
76
00:05:00,780 --> 00:05:03,750
And then make it a little bit bigger on the way to.
77
00:05:04,110 --> 00:05:04,620
All right.
78
00:05:05,040 --> 00:05:06,600
There's the box that surrounds it.
79
00:05:07,200 --> 00:05:08,370
Make sure that's the trigger.
80
00:05:09,030 --> 00:05:09,410
All right.
81
00:05:09,420 --> 00:05:12,270
This is what the recast is going to hit.
82
00:05:12,360 --> 00:05:14,190
It's going to hit this box collider.
83
00:05:14,730 --> 00:05:16,560
Then we have our tag at the top.
84
00:05:16,920 --> 00:05:23,270
So when the other tag clicked plus and call it zombie, make sure I spelled exactly the same as whatever
85
00:05:23,340 --> 00:05:24,540
you've got in your script.
86
00:05:25,920 --> 00:05:27,960
And then I can now say zombie.
87
00:05:28,290 --> 00:05:32,970
So whenever we add this character, that's then going to initiate that.
88
00:05:33,270 --> 00:05:33,600
All right.
89
00:05:33,600 --> 00:05:35,340
It's going to it's going to return something.
90
00:05:36,000 --> 00:05:42,480
Well, what we can do is double check it before we do anything else by using the debunked log, simply
91
00:05:42,480 --> 00:05:46,560
going to give us a message, the message inside the string references.
92
00:05:47,130 --> 00:05:49,410
I hit a zombie.
93
00:05:50,760 --> 00:05:51,120
All right.
94
00:05:51,120 --> 00:05:52,080
Just to double check that.
95
00:05:52,080 --> 00:05:52,830
It does work.
96
00:05:54,510 --> 00:05:56,160
So I'm going to save this.
97
00:06:00,090 --> 00:06:02,250
Wait for this to update.
98
00:06:03,000 --> 00:06:12,660
Double check that there are no missing aspects in the main script and in the simulator, I'm now going
99
00:06:12,660 --> 00:06:14,100
to try and hit that zombie.
100
00:06:22,470 --> 00:06:27,160
At the moment, it's not returning any messages.
101
00:06:27,970 --> 00:06:33,880
OK, so if you find it's not returning any messages, it's simply because there is no physics elements
102
00:06:33,880 --> 00:06:34,570
in the game.
103
00:06:34,960 --> 00:06:39,760
Physics element only happens when you have a rigid body component somewhere.
104
00:06:40,300 --> 00:06:49,480
OK, so on that character and where effectively wanting to access the physics elements, there's no
105
00:06:49,990 --> 00:06:52,390
object this handling physics inside of us.
106
00:06:52,990 --> 00:07:00,580
So if we add a component to this and we add a rigid body, this is what handles all of the physics.
107
00:07:01,330 --> 00:07:03,390
OK, we want to make sure there's kinematic.
108
00:07:03,760 --> 00:07:06,880
If it's not, the character could easily fall throughout the ground or something.
109
00:07:07,990 --> 00:07:13,420
This basically means that it doesn't have real world gravity, but the rigid body component is now going
110
00:07:13,420 --> 00:07:14,950
to handle all of the physics.
111
00:07:15,580 --> 00:07:16,750
OK, so let's try it again.
112
00:07:31,830 --> 00:07:34,950
So it's actually the target that's getting in the way.
113
00:07:35,010 --> 00:07:36,600
The target as a box collider.
114
00:07:37,500 --> 00:07:41,030
So it's actually it's in that target first before it's anything else.
115
00:07:41,040 --> 00:07:48,090
If I were, for example, to switch that off, and I should then be able to actually hit the zombie.
116
00:07:49,200 --> 00:07:50,670
Here we go, a zombie.
117
00:07:51,330 --> 00:07:54,660
So we simply have to exclude the target.
118
00:07:55,180 --> 00:07:55,530
All right.
119
00:07:56,970 --> 00:08:01,330
So the way that we would actually exclude something is we'll place it on its own layer.
120
00:08:01,890 --> 00:08:02,330
Moment.
121
00:08:02,340 --> 00:08:08,370
We have this many layers so we can actually add the new layer, let's call it and play.
122
00:08:08,370 --> 00:08:10,820
It's OK.
123
00:08:10,820 --> 00:08:13,260
It's OK.
124
00:08:13,260 --> 00:08:17,350
And then when we click on our target, we can choose play a target.
125
00:08:17,380 --> 00:08:18,930
So it now sits on that layer.
126
00:08:19,620 --> 00:08:29,850
Now, in our script, we can simply now call a layer mask so we can basically say layer mask and and
127
00:08:29,850 --> 00:08:31,080
we'll call it
128
00:08:33,450 --> 00:08:36,210
target exclude.
129
00:08:38,280 --> 00:08:38,560
OK.
130
00:08:38,840 --> 00:08:43,980
And I do want this to be public as well so that I can access it in the editor.
131
00:08:44,790 --> 00:08:45,360
There we go.
132
00:08:46,800 --> 00:08:56,010
And then under our actual raee whereby were firing at our right, we can simply say int layer mask and
133
00:08:56,010 --> 00:09:04,260
avoid which layer must we want to exclude exclude target or other target exclude.
134
00:09:04,290 --> 00:09:04,860
There we go.
135
00:09:06,100 --> 00:09:10,320
And now it will completely ignore the object that sits on that layer.
136
00:09:10,830 --> 00:09:11,220
OK.
137
00:09:11,820 --> 00:09:14,550
And so if I now save this.
138
00:09:17,020 --> 00:09:17,710
Compile.
139
00:09:20,330 --> 00:09:25,310
Switch back on my box collider for the target that's important on the ground.
140
00:09:26,210 --> 00:09:31,070
We should now find that we've got a little drop down target to exclude and we're going to choose play
141
00:09:31,070 --> 00:09:32,570
a target, maybe go.
142
00:09:34,690 --> 00:09:40,090
So now when we play, we should now be able to hit the zombie without it in the target.
143
00:09:47,410 --> 00:09:48,220
That's the theory.
144
00:09:56,090 --> 00:10:08,920
He's only a player targets, and the ground is definitely only excluding play at Target and let's.
145
00:10:09,200 --> 00:10:11,700
Oh yeah, I didn't Avedisian exclude sorry.
146
00:10:12,080 --> 00:10:13,220
Should just be on default.
147
00:10:13,970 --> 00:10:21,230
Since the characters are on default, I didn't use it in the next cloud layer mask only doesn't include
148
00:10:21,230 --> 00:10:21,920
layer mask.
149
00:10:22,640 --> 00:10:23,390
Let's try it again.
150
00:10:27,750 --> 00:10:28,320
There we go.
151
00:10:28,680 --> 00:10:31,020
All right, now we can hit the zombie.
152
00:10:35,360 --> 00:10:37,760
Multiple times, five times.
153
00:10:38,270 --> 00:10:44,540
And if you want it collapsed, you can do or you can choose to see each and every one of them are excellent.
154
00:10:44,610 --> 00:10:46,010
So we've got that sorted.
155
00:10:46,550 --> 00:10:51,320
And of course, when we shoot the zombie instead, just report in a message, we actually want to kill
156
00:10:51,320 --> 00:10:51,860
the zombie.
157
00:10:52,640 --> 00:10:58,880
Now in mobile games, and you wouldn't necessarily destroy an object and then spawn a new object.
158
00:10:59,150 --> 00:10:59,450
All right.
159
00:10:59,450 --> 00:11:01,310
Because that's quite an expensive operation.
160
00:11:01,970 --> 00:11:08,240
And you're you're spawning in lots of new characters and he's going to take memory and processing power.
161
00:11:08,690 --> 00:11:11,030
So instead, what you do is use appalling system.
162
00:11:11,660 --> 00:11:16,040
What this means is you have a whole bunch of characters loaded when the game starts.
163
00:11:16,370 --> 00:11:17,630
Simply switch them off.
164
00:11:18,080 --> 00:11:19,610
And then when you need them, you switch them on.
165
00:11:19,880 --> 00:11:24,760
And if we kill a character, all we do is you switch the character off until it gets respond.
166
00:11:24,770 --> 00:11:26,090
And he's switched back on again.
167
00:11:26,630 --> 00:11:26,980
All right.
168
00:11:27,260 --> 00:11:28,970
It's really, really easy to do.
169
00:11:29,810 --> 00:11:37,130
So, first of all, when we shoot the character, we actually want the character to die at the moment
170
00:11:37,400 --> 00:11:42,360
and just go to Project Window so that I can find the animal.
171
00:11:43,040 --> 00:11:46,000
There is a death animation for these characters.
172
00:11:46,000 --> 00:11:52,010
So let's just write this in here and I want to give a parameter.
173
00:11:52,020 --> 00:11:53,060
It's going to be a trigger.
174
00:11:53,480 --> 00:11:56,810
Simply call it well, die.
175
00:11:59,730 --> 00:12:06,360
Right click from and then walk over to him death and we're simply going to fight or die on the motion
176
00:12:06,360 --> 00:12:07,620
without in the exit time.
177
00:12:08,100 --> 00:12:14,550
So the character is kind of walking when he gets shot is just simply going to fall down and die.
178
00:12:14,730 --> 00:12:15,090
All right.
179
00:12:15,990 --> 00:12:20,850
And this takes roughly from one second to three seconds.
180
00:12:20,910 --> 00:12:23,430
It roughly takes two seconds for the character to die.
181
00:12:24,150 --> 00:12:27,480
So after two seconds, we want to switch off that character.
182
00:12:27,990 --> 00:12:28,830
OK, that's fine.
183
00:12:29,430 --> 00:12:30,270
We can do this.
184
00:12:31,470 --> 00:12:38,730
So in our script, what we want to do, we know this works now so we can take the book when the access,
185
00:12:38,730 --> 00:12:40,470
whatever object got hit.
186
00:12:41,340 --> 00:12:44,170
Now, if you've got multiple zombies, we don't want them all to die.
187
00:12:44,190 --> 00:12:46,290
We only want the one zombie to go hit.
188
00:12:46,740 --> 00:12:54,240
So what we can say is hit doc transform dart game object.
189
00:12:54,990 --> 00:12:56,970
So that goes right up to the top level.
190
00:12:57,600 --> 00:13:00,330
And we're going to get the component.
191
00:13:02,670 --> 00:13:04,860
The component is going to be it's on the meter.
192
00:13:08,260 --> 00:13:15,520
Open and close brackets, and then we're going to give it a chance of going to say set trigger and the
193
00:13:15,520 --> 00:13:17,170
trigger we want to call is done.
194
00:13:18,430 --> 00:13:20,200
It's basically going to play the animation.
195
00:13:20,300 --> 00:13:25,660
OK, so if we now save this and then we're going to test this.
196
00:13:27,990 --> 00:13:28,620
Compile.
197
00:13:33,730 --> 00:13:34,960
OK, so I was I play.
198
00:13:39,180 --> 00:13:39,830
There we go.
199
00:13:39,900 --> 00:13:40,320
OK.
200
00:13:40,440 --> 00:13:41,880
And the zombie dies.
201
00:13:42,240 --> 00:13:43,980
He's playing the animation over and over again.
202
00:13:44,700 --> 00:13:49,560
So what we need to do actually on the animation is make sure that it doesn't run multiple times.
203
00:13:50,880 --> 00:13:52,680
So it's not in a looping state.
204
00:13:52,800 --> 00:13:58,620
So just click on in death, click on the main character where we can see loop time.
205
00:13:58,770 --> 00:13:59,610
We don't want it to look.
206
00:13:59,610 --> 00:14:00,930
We only want it to play once.
207
00:14:01,200 --> 00:14:04,200
So just check the box and choose apply.
208
00:14:04,770 --> 00:14:05,120
All right.
209
00:14:05,130 --> 00:14:05,400
Good.
210
00:14:05,520 --> 00:14:06,450
Long play wants.
211
00:14:07,530 --> 00:14:13,290
It's what we need to do now is simply switch this character off when the animation is finished.
212
00:14:13,830 --> 00:14:16,680
But if we if we simply switch it off here, what's going to happen?
213
00:14:16,680 --> 00:14:18,420
It's going to switch it off in the same frame.
214
00:14:18,630 --> 00:14:24,060
So what we need is a pause in the script can do this by using the curtain.
215
00:14:24,450 --> 00:14:30,570
So if I type start coroutine, we have to give it a name, let's call it something bigger,
216
00:14:33,660 --> 00:14:41,280
and then open and close brackets, because it is technically a function and semicolon and that will
217
00:14:41,280 --> 00:14:50,820
remain red until we create a kurti coroutine is effectively an eye enumerator yield's and it's command
218
00:14:51,030 --> 00:14:54,780
back to a different function until it's ready to execute them.
219
00:14:55,140 --> 00:14:56,610
So we can actually either pause.
220
00:14:57,060 --> 00:15:03,270
So right down at the bottom, still above the curly bracket, by the way, asked to belong inside of
221
00:15:03,270 --> 00:15:03,780
the class.
222
00:15:04,200 --> 00:15:08,400
We're going to type I numerator and then give it its name.
223
00:15:08,580 --> 00:15:12,840
So some be open and close brackets.
224
00:15:12,870 --> 00:15:14,790
Notice the red line has now disappeared.
225
00:15:15,440 --> 00:15:21,480
OK, red line on this one, because any kind of an early enumerator always expects some kind of a yield.
226
00:15:22,080 --> 00:15:25,500
So we have to put that in the yield return.
227
00:15:27,270 --> 00:15:27,720
OK.
228
00:15:27,840 --> 00:15:29,520
And then obviously the red line disappears.
229
00:15:30,570 --> 00:15:34,470
And then what type of yield do we want?
230
00:15:34,470 --> 00:15:37,740
We want to wait four seconds.
231
00:15:38,700 --> 00:15:45,930
OK, and let's say we wait for two seconds and then basically we switch off the object.
232
00:15:46,200 --> 00:15:51,780
So what we'll say is hit the transform dart game object.
233
00:15:53,220 --> 00:15:57,450
Don't set active to false.
234
00:15:59,430 --> 00:16:03,480
OK, so that will basically just switch that object off so we don't see it.
235
00:16:04,950 --> 00:16:07,020
Whatever got hit there?
236
00:16:07,050 --> 00:16:07,440
OK.
237
00:16:07,950 --> 00:16:14,790
And I'm not sure if two seconds is too long a period because you might have a different character and
238
00:16:14,790 --> 00:16:15,930
you might switch that one off.
239
00:16:16,530 --> 00:16:18,030
But let's find out.
240
00:16:18,760 --> 00:16:23,940
And so in the simulator, we're going to make sure it does switch off the zombie character.
241
00:16:24,840 --> 00:16:28,920
Now, if you keep an eye on this character, it should gray out when we hit him.
242
00:16:36,670 --> 00:16:37,010
There we go.
243
00:16:37,150 --> 00:16:38,770
Disappeared and that's gone great.
244
00:16:39,070 --> 00:16:40,030
OK, excellent.
245
00:16:41,440 --> 00:16:47,380
And now, obviously, there might be multiple zombies written in a multiple times.
246
00:16:47,770 --> 00:16:55,450
The issue with doing this this way is that this is creating a new temporary variable of wait four seconds
247
00:16:56,170 --> 00:16:58,030
each and every time that zombie dies.
248
00:16:58,330 --> 00:16:58,660
All right.
249
00:16:58,660 --> 00:17:03,490
And they all got loaded into memory and then eventually they have to be flooded out of memory by using
250
00:17:03,490 --> 00:17:05,110
something called garbage collection.
251
00:17:05,800 --> 00:17:12,280
Every time that garbage collection gets called, there's a spike in the performance of your processor
252
00:17:12,550 --> 00:17:14,530
that puts extra pressure on it.
253
00:17:14,830 --> 00:17:21,460
And that's when you start to see your frames per second really drop not not constantly all through the
254
00:17:21,460 --> 00:17:22,780
game, only at certain points.
255
00:17:22,780 --> 00:17:26,620
And then you see your frames per second really drop down to avoid this.
256
00:17:26,770 --> 00:17:28,930
You can actually cash this wait for seconds.
257
00:17:29,200 --> 00:17:33,730
So it's only ever initialized once and it's just reused again and again.
258
00:17:34,600 --> 00:17:36,310
So appear at the top.
259
00:17:36,730 --> 00:17:37,600
Its creator.
260
00:17:38,140 --> 00:17:39,250
Wait four seconds.
261
00:17:42,590 --> 00:17:44,690
We'll give it a name, let's call it
262
00:17:47,870 --> 00:17:51,320
I call this one zombie death time.
263
00:17:52,090 --> 00:17:52,370
OK.
264
00:17:52,940 --> 00:18:00,860
And it's going to equal a new weight for seconds of two seconds.
265
00:18:02,000 --> 00:18:02,450
There we go.
266
00:18:02,930 --> 00:18:04,980
So now we've initializer it.
267
00:18:05,120 --> 00:18:08,240
It basically remains in memory throughout the entire game.
268
00:18:08,540 --> 00:18:11,240
All we have to do now is just basically call it by name.
269
00:18:11,960 --> 00:18:18,320
So instead of declaring it new here, we simply say return zombie death time.
270
00:18:21,620 --> 00:18:22,100
There we go.
271
00:18:22,490 --> 00:18:31,520
And so we're no longer creating these unnecessary temporary variables and we're reducing the option
272
00:18:32,030 --> 00:18:34,580
of the zombie, the garbage collection.
273
00:18:34,700 --> 00:18:38,750
So hopefully we won't have any spikes in our performance.
274
00:18:39,590 --> 00:18:39,910
All right.
275
00:18:39,920 --> 00:18:42,320
Simply because you cached that element.
276
00:18:44,480 --> 00:18:45,380
So let's save that.
277
00:18:45,800 --> 00:18:48,980
And that's another bit of optimization completed.
24294
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.