Hello guys.
Welcome to the video.
In this one we're going to make a falling platform.
The script is very easy to do.
And this is basically a practice of all what we did before like sort of a combination of it.
I will explain as much as I can when we doing some new things, but things that we did before, I'm
not going to explain much.
All right.
So let's make the falling platform.
It's going to fly in the air.
Then when a player steps on the platform, it's going to fall.
And that's basically all the functionality.
First we need to make GameObject.
So let's do it.
Graphic traps.
Uh following platform, select them both and set pixel per unit to 16.
Then let's take one of the sprites, drag them into the scene, make certain layer uh, trap.
We also need a rigid body on this one rigid body 2D and let's name it as trap Olin platform.
And let me quickly say, why do we need a rigid body.
So we're going to set it to kinematic.
And it's not going to be affected by physics or anything like that.
And it's going to stand over there for example.
Right.
But when we want it to fall, we're going to switch body type from kinematic to dynamic, and it's going
to fall like that.
So that's the only reason we need a rigid body on this platform.
Let's change collision detection to continuous and interpolate interpolate.
Nice.
Now we also need a box collider so we can stand on the platform just like that.
And we need to set layer to ground.
There are other things we need to do.
But first let's set up animations and we're going to go to asset animations traps controllers make a
new one.
Trampoline platform.
Let's put it on the platform.
Yeah, we have animator.
Nice.
Now we need animations.
Let's create and inotropes trap.
All in platform.
Uh, active.
Can I go to graphics traps following platform?
Take these sprites on.
Put them here.
Sample array 20.
Then we need new clip.
And I'm just here I'm just going to select this one and change active to.
Of save it.
Take the off sprite, put it here on the animator.
We're going to select active by default.
And then we're going to transfer to platform off with a trigger.
Deactivate.
Trigger has exit time and check.
Transition duration zero.
Very nice.
And that's all for the setup.
Now we need a script for this.
I'm going to go to scripts make a new C sharp script trap fallen platform.
The functionality is somewhat similar to the trap soul because we have a movement on this platform,
but I don't see really a reason to make entire inheritance just for one method.
So I think we're just going to write everything from scratch, uh, how it's going to work.
If we just go to play mode right now, it's going to spin the propeller over there, but it doesn't
look like it's flying.
It's standing there flat.
So what I want to do here is to save position above the platform and below the platform.
And this will be the travel distance for the platform and platform going to move between these two positions
real fast.
And it's going to look like it's flying.
So first let's go ahead and save the position just so you understand how it's going to work I'm going
to make public vector three uh waypoints and then we're going to hide it.
But first I want you to see how it's working altogether.
Let's just make it as array.
And let's make serialize field private float travel distance.
And we need serialize field private float, maybe speed just like that.
And I'm going to make it equals to point 75 F by default.
Because it's going to be really small value there.
Now.
In the start over here.
I'm going to do this.
Actually, let's make a method real quick.
Setup way.
Points.
Sorry.
It should be a void over here.
Void?
Yeah.
So how do we set up waypoints?
We're going to have, uh, waypoint array with the length of two because we only need two waypoints
up and down.
Right.
And then I'm going to take waypoint equals to zero.
And I'm going to set it equals to transform position.
Plus the offset for the Y.
So actually first let's decide on the offset.
And this is how it's going to be.
Plot y offset is equals to travel distance divided by two y like that.
Well let's say travel distance of the platform is one.
We want to move it up and down.
But when platform starts it is starting over here.
And if we want to get travel distance of one we just need waypoint on minus five and waypoint on the
five.
Basically we just uh, take the travel distance and divide it by half and then set in one position half
below and half above.
This is how it is.
So waypoint zero is equals to transform.position plus new Vector3 zero y offset zero.
And then.
Second waypoint, with the index one going to use minus y offset.
Very nice.
Let's go ahead to start method and call setup waypoints.
And I just want to show you how it's going to work.
We're going to go back to unity.
Over here.
I want to set the position of the platform to zero.
Just for example.
So it is here.
It has y zero and travel distance.
Let it be one.
Let's go to play mode.
And over here we can see that x position is the same as before.
But y position has zero five and -0 five.
So now when we ask this platform to move to one of the waypoints, it's going to move to minus five.
Then to five minus five, five, minus five five and it's going to look like it's flying.
Basically this is what we need to do.
So now we know how the waypoints are working.
How do we set up them.
I'm going to make it private and I'm going to make update.
And below the update I need method private void handle movement.
Let it be like that.
So here we're going to do Transform.position equals to vector two.
Move towards from Transform.position to a waypoint with a fly speed.
Sorry, we need an index.
I forgot the index.
So let's do private int we point index okay.
Very nice.
So over here we're going to use waypoint index.
And speed will be just speed multiplied by time dot delta time.
Nice.
Now we're going to check if we are close enough to the waypoint.
If uh vector2.
The distance from transform.position to a waypoint index less than 0.1 F, then we are close enough.
We can take waypoint index and do plus plus.
And if waypoint index is bigger or equals to waypoint length, then waypoint index equals to zero is
going to handle movement.
I'm going to call this method in the update handle movement.
And just in case we need to switch it off at some point I'm going to make a boolean over here.
Private bool can move.
Uh yeah.
Let's make it public for now so we can see it later on.
I'm going to hide it.
And over here, if can move equals to false, then return.
Let's save this and let's go see how does it work right now.
So I'm going to set travel distance really low like 0.6 maybe.
And I'm going to set Canmove true.
And we're going to see platform in the action.
And now it's looking nice and like, you know.
Low t reading like how it works if you set lower travel distance to something like 0.4.
Going to be even better.
Yeah.
Now the goal is to land on the platform and detect that we landed on the platform.
Right.
Uh, I'm going to place platform over here and player over here.
So how are we going to detect that player touched the platform?
There are many ways to do that.
And one of them would be to use Trigger Collider.
I think I didn't show you this before, but you can actually add two colliders to the game object and
set one of them as is trigger, and this collider will trigger a method on trigger enter.
We just need to focus on the platform.
Click Edit Collider.
And we need to squeeze it from the sides and from the below.
So this trigger cannot be reached from the sides or from below, because there is a common collider
that will block the movement.
But from the above, it's supposed to be, you know, available.
So over here I'm going to just slightly increase it.
And if you're not sure which collider you are changing, you can see values over here in the collider.
And by dragging them I can see that I'm changing values of the Trigger Collider.
Okay.
So now this collider cannot be entered from the sides or from the below, but it's supposed to be available
from the above of the platform.
So we're going to take the collider and just slightly increase it.
Like so really like just by a bit.
If you're not sure which collider you are changing, you take a look at the variables over here and
if they are changing means you're changing the trigger.
You can see that right?
Okay.
So I'm going to make it somewhat like this.
And let's move this collider over here.
Now we can snap them like so.
And I just want to make sure it is working.
So I'm going to go ahead make on trigger.
Enter on trigger enter to D.
Sheila, I know it's going to work, but I want to.
I want you to see it.
Let's do.
Player.
Player equals to collision GameObject Getcomponent.
Our player.
And if player not equals to null, then debug log.
Platform should fall.
Okay.
Gonna save it.
Go back.
And let me jump on the platform.
Boom.
Platform should fall.
Very nice.
So how do we achieve the falling of the platform?
I think it should fall with a delay.
Not immediately.
And for that we could use a coroutine.
Or we could use, um, invoke.
I never showed you what is invoke, but it also can be used.
So I think it's the time.
Let's go ahead and quickly make a header platform.
All details something like that.
And over here we need serialize field, private float.
Uh whole delay.
I'm going to make it 0.5 by default.
Nice.
Now we're going to make a method.
Private void drop off platform.
Oh let's use switch off platform better.
And in the switch off platform we're going to switch body type of the rigid body.
And also going to increase gravity scale.
So it falls faster I guess and animation should be triggered I guess for that we need a bunch of components.
So let's go ahead and get components over here.
Private Animator, anim Revit Rigidbody, 2d, ARB and we need colliders on the object so we can switch
them off.
And player would not be able to stand on the platform anymore.
So I'm going to do Private Box Collider 2D, but I'm going to make it as array because we have two colliders
and I'm going to just call it colliders then in the wake.
We're going to do anim Getcomponent of animator Richard by the getcomponent of rigidbody.
And for colliders we're going to do equals to getcomponent with the plural over here.
All right.
With the letter S get components of box Collider.
So they all will be in the variables saved.
Now we can go back to the.
Switch off platform over here.
We're going to do anim set trigger off deactivate.
Then RB is kinematic equals to false.
Also I want to set gravity scale to something like 3.5 and RB drag equals 2.5.
This will add a bit of.
The resistance of the air will make it more natural.
And also we want to take let's do for each loop.
And here we're going to use Box Collider as a variable box collider 2D.
So for each box Collider Collider in colliders that we collected before.
We're going to do collider enable enabled equals to false.
So this will switch off the collider.
Very nice.
Now as I said before we should call this method with a delay.
This is how you call method.
With a delay you can type invoke.
Then in a quotation marks, you need to type the name of a method like this switch of platform.
And then after a comma, you give it time as a delay and we have something drop delay.
What's the variable?
Full delay.
Okay, uh, before we go and test this, I want to say something here.
It is not very good practice to type methods in quotation marks.
And this is what I learned with experience.
Because you can make a mistake over here.
It will not work.
Also, you may change methods name later on and it will not change it over here.
So it's really difficult to keep track of this.
So instead we can type name of parenthesis switch off platform.
I'm gonna work as well.
But now if you change method it's going to change it here as well.
Okay let's save this and let's go and see how platform is falling.
So it is flying over there.
I'm going to jump on the platform.
Boom!
And it falls.
Not that bad.
Yeah, I like it.
Was sort of jittering, I think.
So I just need to see it once again because felt glitchy.
I'm going to just take the platform.
Also, you'll see that all of the platforms are flying together.
Now we're going to fix it with a randomizer.
But first let's see it.
Oh, you see how it's struggling to fall.
And I know why it's struggling to fall.
Because it's affected by gravity.
But also it is trying to move with a this handle movement.
So we need to go to the switch off platform and make and move equals to false.
Yeah.
Uh, let's go ahead.
Double check this.
Yeah.
Better, I like it.
Now there are a couple of things I want to do to improve this mechanic.
One of them is to.
Make some sort of impact when you touching the platform.
And second one is to add difference for the movement.
So different platforms flies with a different, you know, cycle because as of now they all fly in synchronically
and I don't like it.
And let's go back to the script.
So regarding the delay for different platforms we're going to go ahead.
And, uh, make a method private void.
Activate platform.
In which we're going to give it a delay.
So let's do float.
Random delay equals to random range.
Range from zero to let's say 0.6 F, so it's about half of the second.
And it can be widely between 0 and 0.5 F actually 0.59 F.
Yeah.
And that will give it different movement for each platform.
And then over here I'm going to do oh wait a second.
This should not be here.
This should be here in the start.
Yeah.
And this one will just do.
And move equals to true just like that.
And we're going to invoke.
Name of activate platform with a random delay.
Yeah.
Also, I have a trick how to make it all cleaner, but I'm going to show it to you later on.
Uh, now we need to make the impact when you touch the platform.
Let's make a variable sterilized field private float impact speed.
I'm going to make it equals to three by default because I know it's going to work.
And also we need serialize field private float impact duration.
And also I know 0.15 is a good value.
So this is how fast the platform will move when you touch it.
And this for how long platform will move when you touch it.
And we also need a timer private float impact timer.
Now this is how it's going to work.
We're going to decrease timer all the time.
And then when we set value to a positive value then we're going to be decreased from a positive value
to negative.
And a while value is positive we're going to move the platform.
Let's go and make a method.
Somewhere here.
Private void handle impact in which we're going to do this.
If impact timer is less than zero, then return.
Also, we're going to do.
Actually, let me just show you how it's going to work.
I'm going to do impact timer minus equals time dot delta time.
Since delta time is sort of a time in seconds, we're going to decrease this timer all the time while
it is bigger than zero.
If it's less than zero it's not going to work.
I'm going to do debug log.
Move platform.
Now let me make this value public so I can show you how it's going to work.
Let's go to unity.
We can see we have impact timer.
And if I set it to a positive value like three.
Huh?
Oh, I forgot to call method in the update.
I'm so sorry.
Did I not?
Yeah I did not okay.
Update.
Handle impact okay.
So I have a bug timer and if I set it, let's say to three you're going to see how it's decreasing itself.
And we can just move platform down while the timer decreasing itself.
And if we set it to 0.1 while this is happening, we're going to move the platform or another value.
Basically, while value is positive, we're moving the platform down.
So we're going to go over here.
And do transform position equals to vector to move towards from, transform position to and we just
need a position below the platform like current position minus ten on the Y.
Something like that.
So I'm going to do to uh transform position plus vector three dot down multiply it by some number like
ten.
You just need a vector three below the current one we have.
And it's going to have speed that we use to do impact is impact speed.
Impact.
Speed multiplied by time.
Delta time.
I'm going to press enter over here so you can see it.
And that's all what we need to do for the impact actually.
Nice nice, nice nice.
Now we only want to start the timer every time we enter in the trigger.
So I'm going to do this.
Impact timer equals to impact duration.
And also we want to trigger this only once because if we do not switch off possibility of triggering
the impact then you will just push platform down all the time.
So let's go.
Make this private.
Here I'm going to do space.
So it's different and let's do privatebool.
Impact, uh, happened?
Maybe impact happened.
Okay.
Impact happened.
So it's going to be false by default.
But then.
Over here.
Actually, maybe over here, if impact happened, then return cannot be impacted anymore.
And on the enter we're going to do impact happened equals to true.
So we can trigger it only once.
Let's save this.
Let's go back to unity.
I'm going to duplicate the platform.
Hey, duplicate.
Yeah.
And let's maybe place a couple of more here.
So we have platforms.
They line weight, they line together.
Why?
Why they are flying together.
Want them to fly differently.
Let me double check.
Now, because this can move is set to true by default.
Let's select the platforms and make them false.
Yeah.
Now they're flying differently.
I'm going to jump on the platform.
There is impact and it's falling.
Very pretty impact.
I really like it.
Feels natural.
Boom!
And it's falling.
Yay!
So fallen platform is finished.
Took a bit of time, but I hope you like it.
How it works.
I'm going to switch.
I mean, delete this and let's go.
Here.
Hide.
This can move.
Boolean.
Private.
Also, let's make sure it is false by default.
And then.
Oh, I promise to you how to clean this up.
So actually, you can turn a start method into a coroutine and unity will start method anyway.
So if it's a coroutine, coroutine of the start method will be started as well.
So we can do I enumerator over here.
Then we can do calculation of delay.
Then we can do yield return new waitforseconds of random delay.
And then we can make can move equals to true.
I don't know if it's a good idea, but there is a possibility to do that.
And I wanted you to know about this one okay.
Let's save this.
Let's go ahead and make prefab out of the fallen platform.
If you want to recreate the platform after the fallen, you can do it the same way as we did with the
arrow.
You already know how to do that, so I'm just going to make it as a prefab.
Gonna test it once again.
And I'm going to say thank you for paying attention.
Let's go to the next video.