Hello guys.
Welcome to the video.
In this one I want to make another trap.
Not so much of a trap, more like a movement mechanic, but still.
It's very cool one I think.
Let me switch off the trampoline.
And let's go to asset Graphics traps arrow.
And this is the, uh, thing that I want to make this arrow, you need to select both of them, make
pixel per unit 16.
And then let's take one of them and put it into the scene certain layer.
Trump and let's rename it to Trap Arrow.
It's not so much of a trap, more like a, you know, movement mechanic, but nevertheless it's very
cool one and it's going to be extremely easy to make.
First of all, let's set up animation for this one.
And then I'm going to show you what we need to do to make it look even better.
Okay.
And let's go to animations traps.
Over here I want to make folder for controllers.
Nice.
Now we're going to take these two.
Drag them here and make another controller inside.
Uh, trap arrow AC.
Let's put it on the object.
Also, we need a circle collider.
We're going to make it as is.
Trigger.
And let's go to animator and make two animations.
Gonna go to traps make.
This was from my trial.
Okay, so it's going to be arrow, uh, idle.
Let's save it.
Actually.
Let's do.
Trap.
Arrow.
Idol.
All right.
We're going to go to Graphics Traps arrow.
And let's take this sprites.
Put them here.
Sample rate 20.
Then we need one more trap.
Arrow activated.
You're going to take these and put them here with a sample rate 20.
Nice.
Now let's make a trigger.
Uh.
Activate.
And we're going to transfer to this animation with a trigger over here.
Exit I'm uncheck transition duration zero.
That's good for now.
Now let's go over here I think it should be a bit bigger because it's kind of small now.
Barely can see it.
So I'm going to make it as 1.5 over here.
Very cool.
This is a bit better.
Now this object will push player and in the same way as a trampoline does.
But also I'm going to rotate the object with the time like so, and because of that, player will have
to catch the right moment and only jump on the arrow when it points to the direction where he needs
to go.
Okay, that's going to be the idea of it.
And also I want to destroy the arrow once it is used, and then I want to create a new arrow after some
amount of time, like with a cooldown.
Yeah.
So first let's make jumping mechanic for this one.
And actually it's going to be very easy to do if we use inheritance.
This is a good example of how we can use inheritance.
And this is going to be just a brief explanation.
I'm going to explain some things, but not too much because we're going to discuss inheritance more
later on in the enemies section there.
I'm going to pay lots of attention to the inheritance, but we also need to use it now.
So I'm going to give you a quick example how it can be used and just, you know, just in case, I'm
going to say that if you don't quite understand how inheritance work after this video, just wait for
the enemies section.
We're going to discuss it more.
Okay.
So we can go to scripts and we can make new C sharp script trap.
Arrow.
Trap arrow over here instead of Monobehaviour, we can type trap trampoline.
And now this will mean that trap arrow inheriting properties and methods from a trap trampoline over
here.
This is what it does.
Basically, it's like this object over here.
Trap trampoline has a script of a trap trampoline.
But Trap Arrow is inheriting.
Let's put it on the object.
We can see it has same variables.
It is inheriting from a trap trampoline.
And because of that, it sort of has two scripts in its possession.
You know, this one has all the functionality that trap trampoline has over here.
And because of that, we do need to do anything else here.
We can just go to unity, give it push power of 25.
And when I enter the trigger area.
It's going to push me up just like that.
Because it inherits the functionality from a trap trampoline.
This is how it is.
There is something that you should know.
As of now, these methods are available to Trap arrow, but only because these methods are part of Monobehaviour
like awake for example.
Also on trigger, enter is triggered when something enters the trigger collider.
But because these variables are private, as you can see and methods are private as well, we cannot
adjust these variables.
We cannot do anything with them on a trap arrow over here.
As a example, if I type here awake, it's going to make a new method that has nothing in common with
this awake over here.
And also, even though we have anim and this trap arrow inherits from a trampoline, we cannot use anim
here as well.
We cannot use push force.
If we want to use this variables, we need to make them as protected because private means these are
available only to this class.
But if we make them protected.
Now it will be available to this class and to the class that inherits from, uh, trap trampoline.
And now I can use any movie here, but still it is not public.
So public is available to everyone.
Private is available only to the class and protected is available to the class.
And those classes that are inheriting from a main class.
Simple as that.
We do need to override any functionality from a trap trampoline for now, so I'm going to keep it as
it is.
Let's just leave it at that and let's go and make rotation for the arrow.
It's going to be not that difficult to do.
We want to have some sort of, uh, serialized field.
Let's make a header, header, arrow or maybe additional additional info just like that.
It's fine.
I'm going to do private float rotation speed.
Let's make it equals to 120 by default.
And let's make serialized field private bool rotating.
Right.
Because we want to decide if we rotate arrow to the right or to the left.
Um, then we need to open update method.
And over here we're going to type transform rotate zero on the x zero on the y.
We want to rotate only around z axis.
If you go to unity uh select Rotation Tool and rotate the object.
Over here, you'll see that Z changes.
So we need to change the Z.
Rotation.
Let's go back here.
I'm going to use a rotation speed.
Multiply it by time dot delta time because I want it to be frame rate independent.
Nice.
Now let's go back and see this in the action.
I'm going to go to play mode.
We can see it's rotating.
And I need to guess the right time to jump into the arrow.
Because it always will push player to the up axis, and because object is rotating, this axis is always
changing itself.
For example, if I jump now, I'm going to be pushed back.
Okay.
Very cool.
Let's go and make use of this boolean over here.
I feel a bit lazy so I'm going to do this if rotating right.
Then rotate with a minus speed.
Else transform.
Or actually, maybe we can do.
Yeah.
Let's be smart here.
Private int direction equals to minus one by default.
And then over here I'm going to do direction.
Equals to rotating rate.
Then use one, else use minus one.
I hope I'm right because I might be wrong here.
And then we're going to rotate with a rotation.
Multiplied by direction, multiplied by direction.
Let's put it into parentheses and multiply that with a time Delta time.
Yeah.
A bit cleaner.
Very good.
And also I want to destroy the arrow once it is activated.
So I'm going to make Private Void destroy me.
And it's going to just destroy the object.
And let's do destroy GameObject just like that.
Let's save this.
Let's go back to unity.
Over here on the animation of activated after the last frame over here I want to place.
Trap.
Arrow methods.
Destroy me.
Good.
Now let's go and see the rotation.
So now it says, okay, we need to do an opposite.
Yeah.
I'm sorry.
Let's quickly change it before I forget.
This should be minus one and one.
Okay.
Yeah.
Uh, let's go now.
And we can see it's rotating to the left.
If I click the boolean, it is rotating to the right.
Now I'm going to jump into an arrow and it was destroyed very well.
Now we need to create a new arrow.
Once this one being used, uh, we need to make a prefab of the arrow.
So I'm going to just drag it here.
And we could make functionality of an arrow creation in the arrow itself and somehow disable it and
then enable it.
But I don't really want to make object that will create itself, because then it will create clone of
a clone, and then clone of a clone of a clone.
And it's going to be like that endlessly.
So I think I have no better choice for now.
So I'm going to use Game Manager.
Maybe later on we're going to have another object, another manager.
We can use that.
But for now I'm just going to use game manager.
I'm going to make a header.
Or traps.
And I'm going to make public GameObject arrow prefab.
Yeah just that and let's should we make a cool down here.
Now I'm going to do cool down on a trap arrow, because we might want to set up cooldown of the arrows
differently for different levels.
So I'm going to just do prefab and let's go ahead all the way down here.
And I'm going to make a method that will start a coroutine.
So first I need private Ienumerator create object coroutine.
In this quarantine.
We want to pass game object because this can be used later on, not only for the arrow, but for something
else.
Like we don't know what is going to happen in the future, but it would be nice to set up things and
be ready in the future.
So game object prefab and we need float delay.
We want to create object with a delay.
Then I'm going to do yield return new.
Wait for a seconds of delay.
This should be delay.
And then game object.
New object equals to instantiate.
Um, prefab.
I guess we need position.
Something that I forgot.
So I'm going to do vector three position just like that.
Now let's pass transform here.
Transform target.
And over here I'm going to make a vector three new position equals to target dot position.
Yeah.
Because otherwise we might destroy the object with the transform.
And we would not be able to use it.
That's why we store in transform in a vector three position.
Over here we're going to use new position and quaternion identity for the rotation.
Very nice.
Now let's make a public method that will call this coroutine public void.
Um create object.
We're going to pass GameObject prefab.
Then we're going to use uh transform target and Float Delay.
And I want to give it a default value of zero.
So if you don't pass any delay it's going to have no delay.
If you pass some value it's going to have delay.
Now here we're going to do start coroutine of create object coroutine with a prefab target and delay
okay.
Nothing new.
We did all of this before just you know in different combination.
Let's save this and then let's go to Trap Arrow.
Open curly brackets here.
And before we destroy the object, let's go ahead and make a reference to the arrow prefab.
And I'm going to pull it from a game manager dot instance.
Arrow prefab.
Of course, I could make method specifically for the arrow on a game manager, but I want this method
to be versatile.
That's why I'm just passing game object here.
And if I need arrow, I'm just pulling it from a game manager here.
Okay.
So that's what I'm doing.
And then game manager dot instance create object of arrow prefab.
We're going to pass transform of existing arrow.
So it will replace itself with a new arrow.
And for the cooldown let's make a variable serialize field private float cooldown.
And we're going to pass it over here.
Cooldown.
All right.
There is something else I want to do to improve it.
But later on, let's just go ahead and try this for now.
We're going to save it.
Save everything actually.
And let's go back to unity.
On a game manager.
We want to load the prefabs, and on the game manager we're going to assign this trap arrow.
Then trap arrow over here.
Let's do rotating rate.
And let's say cooldown is 0.5.
I just want to be sure it's working.
So I'm going to create new arrows a lot.
It's rotating and I'm going to go there.
Ah!
What is it?
The object of type unity engine transform has been destroyed.
But you still trying to access it?
And this is what I was talking about, that we might destroy the transform and we're still trying to
use it.
Okay.
Over here, we just need to move this line of code above so it will save vector three immediately before
the delay, and then it's going to use it later on.
Yes.
Let's go now.
Have the arrow going to jump into the arrow and it's going to create new arrow again.
Very nice.
Let's put higher cooldown on a prefab over here.
Uh.
It's zero.
Let's do one and let's try.
Yeah, okay.
Very cool.
It's working.
I like it.
Now there is something else I want to do to improve the arrow.
And let's do a cooldown of 1.5 and let's do overwrite apply all so it will be applied to this arrow
over here.
I want to do something else here I want to gradually increase the scale of the arrow when it appears.
So it's going to appear very small just like this.
And then it's going to grow to the size it should have, and it's going to look real nice like it's
popping out.
Uh, for that we're going to use lerp.
And you might never use lerp before.
I'm going to explain briefly, but if you want to know more about lerp, I'm going to attach a video
about lerp that I have.
It's really a nice one.
So if you want to know more, just check the video.
Let's add a space over here and then let's type serialize field private float grow speed.
And I'm going to make it equals to ten by default.
And then also I want to have serialized field private vector three target scale okay.
And then at the start we're going to do transform local scale equals to new vector three.
And with this we changing scale of the transform.
With a new vector that's going to be 0.3 f, 0.3 f, 0.3 f.
Then in the update, let's take these two lines of code and rub them into a method.
Handle rotation.
Yeah, this one probably better be scale up speed.
Yeah.
Better.
Now in the update, we cannot check entire vector like entire scale, but we can use scale X for example,
and then use it to decide what we need to do next.
So I'm going to do if transform local scale dot x is less than target scale, dot x means we need to
scale up the object, and if we need to scale up the object, we're going to do transform local scale
equals to Vector3 dot lerp and lerp is quite confusing in the beginning, but it's very easy to use.
Uh, when you use an lerp, it's going to give you a value.
You can usually say what you want to lerp, like you want to lerp from position A to position B.
In our case, we want to lerp from a transform local scale that we have to the target scale.
That we want to have.
And then you need to decide, like the step you want to have sort of the percentage of the value you
want to have.
If you want to move from point A to point B and size of your step will be 30%, then you just set 0.3
F, for example.
Yeah, lerp always takes um, the percentage of value.
Uh, look at this.
Let's say I need to increase value gradually from 1 to 10.
And size of the step will be 0.5.
Okay, so the first time it's going to take half and it's going to be.
Are removed.
The next time I'm using lerp is going to take half again.
The next time I'm using lerp is going to take half again because step size is 0.5.
And like that huff huff huff huff.
Because of that, it's going to look smooth, slow and nice.
So over here I'm going to use scale up speed multiplied by time dot delta time.
Okay.
Very cool, I love it.
Let's take these two lines of code.
The extract method.
Handle scale up.
Just like that.
Then we're going to go to the unity.
Let's find prefab and make target scale 1.51.51.5.
Now let's go destroy the arrow and see how it's going to.
Yeah, that is very nice.
You can see that.
I'm going to you and it's growing like that.
Really smooth and nice.
Lerp can be used for many things.
This is just one of the examples.
Yeah, like how it turns out.
Really cool mechanic.
Need to wait.
And then I need to bounce there.
Yeah.
All right.
That's good for now.
Let's save this and let's go to the next video.