Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranรฎ)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
And this video, we're going to shoot a recast out of Atlanta.
But we're going to do so at the position of the crosshairs.
So whatever is in front of the cross area is basically can they get hit?
Now, you might be thinking, how do other people know how to script these things?
You know, how do you know how to write a recast?
You know, where do I learn this sort of stuff?
Well, it's actually part of the script in reference, if you know what you want to achieve.
You can actually go into the script in reference.
And let's say that I just type recast.
Oh, I mean, in Google for some reason.
Oh, it's because I typed it in the top there.
So if I do recast and you basically get all these different elements, that's going to explain our Reekers
works.
And the one that we're looking at here is recast, hit.
OK, this is going to explain all the different things that you can do with a recast in terms of like
whatever it's hit.
Well, the one that we're interested in is collider.
Right.
And it's actually going to show you how to actually script that, how to put it together.
So you need a variable of recast hit or a type or whether it's called hit.
And then you need a type of ray that simply called ray.
You choose where that's going to come from.
So it's from the camera, the main camera.
And then this is the position where the ray is fired from.
And then you need a bit of physics to recast.
And it's done like this.
And then you you basically listen if it's actually hit something.
OK, so let's let's actually.
Right, Nathman.
So in our script, first thing we need is obviously recast.
So off type recast, hit.
And there were those who on the recast hit and I'm simply going to call this hit.
OK, that's going to store the value of whatever hits or whatever character.
Then in our shoot, we can now do all of our work in here.
Bear in mind, this only gets called when we press the button.
It's not getting called every frame she's got.
It's only being called, let's say, less than once a second, which is great.
OK, so we're simply going to say of type Ray that we're simply going to call ray equals camera dot
mean dot screen point to Ray.
And then he's asking knows where is the position?
Where do we want to start that from?
Well, it's from the crosshairs dot position.
It's going to use that position and.
Oh.
Of course, is because my CROSSFIRE is a game objects.
It only knows position if I'm using it as a transforms or transformed up position.
There we go.
OK, so now that I'm actually firing that ray from the crosshairs, then what I need to do is do some
physics.
So I'm going to say if physics starts recast.
And then expect in the ray, which we called Ray, and then out it, OK, it's going to shoot the ray
out, it's going to store whatever it hits in variable.
And then we've got a distance.
All right.
I'm going to say five, four about.
Don't even need a thousand.
That's way too far.
I'm going to say 200, because we know that scene only lasts about 100 pixels.
So it's going to fire out around 200 meters.
And whatever it hits, it's going to get stored in the variable the way it's used in this this one that
comes directly from the camera transform.
So what we need to do now is say if hit the heart, transform and compare tag side of bracket's as a
string.
Let's say we're a zombie.
OK.
Simply call it zombie.
If that's the case, then it's going to do something.
Now, at the moment, we don't have a tag of zombie.
So let's sort that out, first of all.
Let's just go back into unity.
And wait for that to compile.
So let's get close to a zombie, and at the moment he's got this slight little cylinder around him,
but what he now needs is a box collider, something to click add component of type box collider.
It's a little bit small at the moment, so I'm just going to raise it up on the way I want.
All right.
And then make it a little bit bigger on the way to.
All right.
There's the box that surrounds it.
Make sure that's the trigger.
All right.
This is what the recast is going to hit.
It's going to hit this box collider.
Then we have our tag at the top.
So when the other tag clicked plus and call it zombie, make sure I spelled exactly the same as whatever
you've got in your script.
And then I can now say zombie.
So whenever we add this character, that's then going to initiate that.
All right.
It's going to it's going to return something.
Well, what we can do is double check it before we do anything else by using the debunked log, simply
going to give us a message, the message inside the string references.
I hit a zombie.
All right.
Just to double check that.
It does work.
So I'm going to save this.
Wait for this to update.
Double check that there are no missing aspects in the main script and in the simulator, I'm now going
to try and hit that zombie.
At the moment, it's not returning any messages.
OK, so if you find it's not returning any messages, it's simply because there is no physics elements
in the game.
Physics element only happens when you have a rigid body component somewhere.
OK, so on that character and where effectively wanting to access the physics elements, there's no
object this handling physics inside of us.
So if we add a component to this and we add a rigid body, this is what handles all of the physics.
OK, we want to make sure there's kinematic.
If it's not, the character could easily fall throughout the ground or something.
This basically means that it doesn't have real world gravity, but the rigid body component is now going
to handle all of the physics.
OK, so let's try it again.
So it's actually the target that's getting in the way.
The target as a box collider.
So it's actually it's in that target first before it's anything else.
If I were, for example, to switch that off, and I should then be able to actually hit the zombie.
Here we go, a zombie.
So we simply have to exclude the target.
All right.
So the way that we would actually exclude something is we'll place it on its own layer.
Moment.
We have this many layers so we can actually add the new layer, let's call it and play.
It's OK.
It's OK.
And then when we click on our target, we can choose play a target.
So it now sits on that layer.
Now, in our script, we can simply now call a layer mask so we can basically say layer mask and and
we'll call it
target exclude.
OK.
And I do want this to be public as well so that I can access it in the editor.
There we go.
And then under our actual raee whereby were firing at our right, we can simply say int layer mask and
avoid which layer must we want to exclude exclude target or other target exclude.
There we go.
And now it will completely ignore the object that sits on that layer.
OK.
And so if I now save this.
Compile.
Switch back on my box collider for the target that's important on the ground.
We should now find that we've got a little drop down target to exclude and we're going to choose play
a target, maybe go.
So now when we play, we should now be able to hit the zombie without it in the target.
That's the theory.
He's only a player targets, and the ground is definitely only excluding play at Target and let's.
Oh yeah, I didn't Avedisian exclude sorry.
Should just be on default.
Since the characters are on default, I didn't use it in the next cloud layer mask only doesn't include
layer mask.
Let's try it again.
There we go.
All right, now we can hit the zombie.
Multiple times, five times.
And if you want it collapsed, you can do or you can choose to see each and every one of them are excellent.
So we've got that sorted.
And of course, when we shoot the zombie instead, just report in a message, we actually want to kill
the zombie.
Now in mobile games, and you wouldn't necessarily destroy an object and then spawn a new object.
All right.
Because that's quite an expensive operation.
And you're you're spawning in lots of new characters and he's going to take memory and processing power.
So instead, what you do is use appalling system.
What this means is you have a whole bunch of characters loaded when the game starts.
Simply switch them off.
And then when you need them, you switch them on.
And if we kill a character, all we do is you switch the character off until it gets respond.
And he's switched back on again.
All right.
It's really, really easy to do.
So, first of all, when we shoot the character, we actually want the character to die at the moment
and just go to Project Window so that I can find the animal.
There is a death animation for these characters.
So let's just write this in here and I want to give a parameter.
It's going to be a trigger.
Simply call it well, die.
Right click from and then walk over to him death and we're simply going to fight or die on the motion
without in the exit time.
So the character is kind of walking when he gets shot is just simply going to fall down and die.
All right.
And this takes roughly from one second to three seconds.
It roughly takes two seconds for the character to die.
So after two seconds, we want to switch off that character.
OK, that's fine.
We can do this.
So in our script, what we want to do, we know this works now so we can take the book when the access,
whatever object got hit.
Now, if you've got multiple zombies, we don't want them all to die.
We only want the one zombie to go hit.
So what we can say is hit doc transform dart game object.
So that goes right up to the top level.
And we're going to get the component.
The component is going to be it's on the meter.
Open and close brackets, and then we're going to give it a chance of going to say set trigger and the
trigger we want to call is done.
It's basically going to play the animation.
OK, so if we now save this and then we're going to test this.
Compile.
OK, so I was I play.
There we go.
OK.
And the zombie dies.
He's playing the animation over and over again.
So what we need to do actually on the animation is make sure that it doesn't run multiple times.
So it's not in a looping state.
So just click on in death, click on the main character where we can see loop time.
We don't want it to look.
We only want it to play once.
So just check the box and choose apply.
All right.
Good.
Long play wants.
It's what we need to do now is simply switch this character off when the animation is finished.
But if we if we simply switch it off here, what's going to happen?
It's going to switch it off in the same frame.
So what we need is a pause in the script can do this by using the curtain.
So if I type start coroutine, we have to give it a name, let's call it something bigger,
and then open and close brackets, because it is technically a function and semicolon and that will
remain red until we create a kurti coroutine is effectively an eye enumerator yield's and it's command
back to a different function until it's ready to execute them.
So we can actually either pause.
So right down at the bottom, still above the curly bracket, by the way, asked to belong inside of
the class.
We're going to type I numerator and then give it its name.
So some be open and close brackets.
Notice the red line has now disappeared.
OK, red line on this one, because any kind of an early enumerator always expects some kind of a yield.
So we have to put that in the yield return.
OK.
And then obviously the red line disappears.
And then what type of yield do we want?
We want to wait four seconds.
OK, and let's say we wait for two seconds and then basically we switch off the object.
So what we'll say is hit the transform dart game object.
Don't set active to false.
OK, so that will basically just switch that object off so we don't see it.
Whatever got hit there?
OK.
And I'm not sure if two seconds is too long a period because you might have a different character and
you might switch that one off.
But let's find out.
And so in the simulator, we're going to make sure it does switch off the zombie character.
Now, if you keep an eye on this character, it should gray out when we hit him.
There we go.
Disappeared and that's gone great.
OK, excellent.
And now, obviously, there might be multiple zombies written in a multiple times.
The issue with doing this this way is that this is creating a new temporary variable of wait four seconds
each and every time that zombie dies.
All right.
And they all got loaded into memory and then eventually they have to be flooded out of memory by using
something called garbage collection.
Every time that garbage collection gets called, there's a spike in the performance of your processor
that puts extra pressure on it.
And that's when you start to see your frames per second really drop not not constantly all through the
game, only at certain points.
And then you see your frames per second really drop down to avoid this.
You can actually cash this wait for seconds.
So it's only ever initialized once and it's just reused again and again.
So appear at the top.
Its creator.
Wait four seconds.
We'll give it a name, let's call it
I call this one zombie death time.
OK.
And it's going to equal a new weight for seconds of two seconds.
There we go.
So now we've initializer it.
It basically remains in memory throughout the entire game.
All we have to do now is just basically call it by name.
So instead of declaring it new here, we simply say return zombie death time.
There we go.
And so we're no longer creating these unnecessary temporary variables and we're reducing the option
of the zombie, the garbage collection.
So hopefully we won't have any spikes in our performance.
All right.
Simply because you cached that element.
So let's save that.
And that's another bit of optimization completed.
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.