Hello guys.
Welcome to the video.
In this one I want to show you one of the traps we are going to make.
This is more like a moving trampoline rather than a trap.
But still it is nice.
And I want to show you how to do that.
Okay, so I think I'm going to place this trampoline over here while we're testing it.
So I'm going to just delete this trap from here.
I don't need it.
And I want to take this block over here, select it, and I want to move it to the left just a bit.
So I have sort of a space to jump up okay.
Now let's go to Asset graphic.
We need traps.
And there is this trampoline.
So this has an idol and jump animation.
We're going to select both of them dual pixel per unit 16.
And I'm going to drag one of them here.
Let's change certain layer to.
Uh, trap.
I think we can have all of the components on one game object for this one.
So I'm going to do trap trampoline.
All right.
Now let's go to Assets Animations here I think it's time to create folder for traps.
Sorry.
This should be.
Trumps.
Let me take this.
Put it into a folder and inside I want to make an animator controller for Trump.
Trampoline.
AC animator controller.
Then I drag it on the object.
And also we need a box collider for this one.
But this one going to be a trigger.
Let me focus on a trap.
Yeah.
And uh, we're going to just.
Place it somewhere here and trigger Collider for this one will be somewhere here.
Okay, this is good.
Now let's go and make some animations.
It's over here.
Create.
I'm going to do trampoline idle.
We're going to go to graphics.
Trump's trampoline, could the ideal one then create a new one?
Trumbo.
Lane activated.
Okay.
And for this, we need these sprites over here.
Sample rate going to be.
20 and let me see the animation.
Yeah, this is good, I think.
You okay?
Let's select this animation.
Uh, traps.
Trampoline activated.
And I'm going to uncheck loop time because I need it to be activated only once.
Just to be sure.
Then on the animator, I think we just need a trigger.
Activate.
And we're going to make transition to activate it with a trigger.
Activate has exit time and check transition duration zero, but transition back.
Going to have an exit time of one with the transition duration zero.
Nice.
Now we need a script that will detect player and push it up when we need to.
For that, I'm going to make a new method on the player script because we need to push player from outside
of a script, but functionality can be inside just like a knockback and we are something similar.
Let's go.
Somewhere.
Maybe, I don't know.
Over here.
I'm going to do public void push.
And for the push we're going to need vector two direction.
So this will define the power that will say how much we're going to push the player.
And we're going to need to pass float duration, because I want to take away control from the player
while we're pushing him.
We're not going to use this only for the trampoline.
We're also going to use this for another trap.
So this will be multifunctional.
And if you don't want to have duration, we can simply pass maybe a zero by default.
So if you don't pass any value to this method it will be zero.
Then we need a private eye enumerator.
We need a coroutine.
Push core.
Core.
Teen.
And this.
Will also take two parameters vector two direction and float duration.
And then we're going to do can be controlled equals to false.
Then we're going to push RB.
But before we do that, I want to reset velocity that player had before we pushed him.
So I'm going to do RB velocity equals to vector 2.0 which is zero zero.
So we're going to stop any velocity we had.
And we're going to give it a new velocity.
And this time I want to use RB add force into direction.
And we're going to type force mode.
Impulse.
So impulse force mode will add an instant velocity to the player, and it's going to be a bit more,
I would say natural because force will be applied by physics engine.
So it's going to be like real nicely.
It's going to feel physically correct.
Yeah.
And then we're going to do yield return new wait for a seconds of duration.
And then we're going to do can be controlled.
True.
All right.
So this is a push coroutine.
And we're going to start it over here.
Start coroutine push coroutine.
And we're going to pass direction and duration.
Very well.
Nothing else for now but.
Sorry.
Maybe you need to see this before we do this.
I also want to go to the update.
And here, if we return from a method and player cannot be controlled, we still need to update his
animations.
But I don't want to break execution order over here, so I'll just call this handle animation.
And I will call handle collision.
Just in case.
So maybe if he'll be pushed into the wall, he can just do a wall slide.
Okay.
Very nice.
That's all the code we need here.
Now let's go back to unity.
And in the scripts we want to make C sharp script trap, trampoline.
It's not really a trap.
It doesn't do anything bad, I guess, but let's keep it within one, you know?
Uh.
Spectrum, I don't know, like all together, all of them will be traps.
Yeah.
So on this trap, we need to detect if player entered the trigger.
So on trigger enter 2D, um player.
Player equals to man.
Now I wish I would use just tag.
I think it would be more.
I think it would be faster to type.
Yeah this one then if player not equals to null then player push.
And here we need to pass a bunch of variables.
So one of them will be, uh, serialized field private vector two push direction and serialized field.
Private float.
It's going to be, um.
Duration, and I want to make it 0.5 by default.
Yeah.
Also, I have an idea.
But before I tell you about this idea, let's just try push direction and duration here.
Yeah, let's just save this and let's go back to unity.
Now there is this trampoline.
Oh, something else here I forgot.
Let's go to script.
Make a private animator anim.
And over here I'm going to do anim set trigger.
Activate.
And we need to get component of that activate.
I think we can just do return.
Why not?
Yet to come.
Wait.
This will be.
Yeah.
This can be activated a couple of times, not just once.
So I'll do.
Awake.
And you get component of animator.
Nice.
Now let's save this.
Let's go back to unity.
I'm just going to put the script on the trampoline like so, and push direction will define how far
it is pushed to the left or right on the x.
So this is x velocity and y velocity will define push up I think for this one it's not supposed to be
pushed uh, to the left or right only up.
So I'm going to put maybe 25 on the Y.
Let's go to the game window.
And.
A subway.
Oh is trigger.
Let's do is trigger.
And now I'm supposed to be able to walk into this trampoline and I'm going to be pushed up.
Yay!
Check this out.
Man, this is so fun!
Hahaha!
I love it so much and you still can control the character after the push.
Yeah.
This is so good.
I love it so much.
Now this object, by the look of it, supposed to push the player only into direction.
It is like going up right?
And we can see the animation is going into this direction.
Uh, luckily for us, we can see that up vector of this object is also going up.
That's the transform of it.
And we can see the yellow arrow or green green arrow.
Sorry.
I'm colorblind.
It's difficult for me to define colors.
I think it's green.
So green arrow pointing up.
That is a y of the object.
Yeah.
If we go here and click 2D now, it's going to be 3D.
And we can see that green is responsible for the arrow up.
So we can go to the script.
And instead of using push direction like that we can use vector dot up and we can multiply it by power.
Let's do serialize field private.
Load.
Push power.
Nice.
Now I don't need a push direction because anyway, we're not controlling X over here.
We only controlling y.
So by typing vector two dot up we're going to have zero and one.
And we can just multiply it by push power just like that.
Let me remove this one.
And sorry, this one should be multiplied by push power.
Now I can save this.
And because this arrow is pointing up and we push in player according to the place where arrow is pointing,
let me do 25.
Push power over here.
Yeah.
We can.
Sorry.
We can take this trampoline and we can rotate it, maybe like that.
And now when we rotate in the object, and now once object is rotated just a bit, his up vector goes
to the left.
If yours looks different, you need to go here and change from global to local.
Okay, so we can rotate object let's say by 90 degree.
And now the up arrow goes here.
And because we push in player with the vector dot up multiplied by push power play are going to be pushed
to the left.
And we do need to adjust vector over here.
We can just rotate the drop.
And that will do everything for us.
Now let's go ahead and try this.
Oh, no.
Didn't work for some reason.
Wait, let me check that.
Supposed to work.
I just tested this.
I'm doing something wrong.
Vector two dot app.
Oh, sorry.
Not vector.
I am so sorry.
Transform app.
We need to use transform app because we want to have up direction of this object.
Yeah.
My bad.
So now this is how it's going to be.
And because this is transform up for this object, this is going to be multiplied with a push power.
And now we go here and queue.
And very cool I love it so much.
So this is it for the trampoline.
Let me make it as a prefab and let me try something.
I'm going to make it as a prefab and let's say this one will be zero over here I'm going to apply.
Oh it's not needed okay.
And another trap I want to place over here.
This is this feels, um, wrong because we don't have any platform here.
Well, let's say we have a platform there.
Why not just quickly?
Just like that.
And now we can make some sort of chain of movement.
You.
And this is so fun, I love it.
Yeah.
Cool.
Right?
And if you want to increase the push power, you can just change the variable over here.
Now I'm going to remove this I'm just going to place this somewhere.
Like at someplace interesting.
Maybe just here.
Why not?
Yeah.
Let it be there.
Now I'm going to, uh, save the project, and we're going to go to the next video.