Hello, guys.
In this video, we're going to make a move and trap the trap that is kind of patrolling from left to
right.
Or if I could say it in another words, moving from one move point to another.
Let's do this.
Let's create two D sprites and I want to choose Hexagon pointer top.
Yeah, this one let's name is moving Trap I guess certain layer platform minus one.
And let's reset position to zero.
And color should be red because it's dangerous.
Now we have visuals for this trap.
What I want to do next is create a script.
And I'm going to name it as moving track.
Now, what would be your first instinct to make this trap?
I guess you need to make a damage.
And for that you could go and create on trigger enter.
Right?
Yes, that is absolutely correct.
But we use an object oriented programming.
And for there, we need to reuse code as much as possible.
We can copy code from here, just copy and paste like this.
But it's not a good practice If you can make it in a different way and you can make it in a different
way, you can use inheritance.
It's one of the main principles of object oriented programming, inheritance, how you do it.
So as of now, this script is inherited from a behavior, and this script is inherited from monarch
behavior.
But we can go here and instead of modern behavior, we can just type trap.
And now whatever information is in the trap can be passed to move in trap.
For example, I'm going to create public float damage.
All right.
Now I have here variable damage and if in the start I would like to assign it to like 50 or something
day much 50.
I could and I could go to move in trap.
Create the start function and assign damage to 50.
And move in trap would have its own variable damage.
Not like in the trap.
So basically it's another object with the properties and functions of this object.
But what if we don't want to make it public?
We want to make it private.
There is a way to do that.
But this time, instead of private, you type protected and now it is available only to this script
or for any script that is inherited from the script.
But if you make it private, you can see in the movie Trap.
Now I don't have access to damage.
It shows me an error.
All right, so what are we going to do here?
We're going to delete this.
And instead of private void, we're going to do protected virtual void on trigger enter, blah blah.
And I guess I want to make it a bit prettier like this.
I like it more.
Now we go to move in trap, delete this, and I'm going to type override.
And I can choose from the list on trigger enter to the.
So this one line of code contains all of the stuff that is written here and you can adjust it and add
some other stuff.
So you would get damage and you would debug a message and so on.
Let's try and do that.
Divide log damaged by moving trap.
And here I would write the backlog.
Damaged by regular trop.
And I need the curly brackets for that.
Sorry.
Okay, let's go and see the difference.
Going to enter play mode.
And now look, if I'll enter my player into the regular trap.
I'll see.
Message damaged by regular trap.
All right.
Let's try again.
And this time I'll use moving trap.
And wait.
We need on trigger enter right circle Collider.
Is trigger.
So.
Boom.
Damaged by regular chop.
And damaged by moving.
Chop.
So you got the idea.
Now let's actually give it a collider.
And I want to make it a bit smaller than it should be.
Again, just to make it fair game.
Yeah.
And let's go back.
So now you have the idea how it works.
Can I delete the bug from here?
And in the moving trap, I'm going to delete the.
And now we're going to make functionality of the moving trap.
Exactly.
How are we going to move it?
Very simple.
First, we need a speed.
How fast is going to move So serialize field private float speed.
Also in the rotated crop.
So it looks pretty.
So I'm going to create serialize field, private float rotation speed.
And now we need a position where are we going to move to?
So the destination for us for this trap, how are we going to create it?
With the help of transform, serialize, field, private transform, move point.
Right.
So if you would like to have a couple of move points, you could do like move .1. to 1.3, but that
is actually a wrong way to do that.
What you can do is use an array.
So after type of variable, you just put square brackets and now it will be an array of transforms.
Let me show you.
We actually did this with a little part, right?
I forgot, so I didn't have to really explain again.
So now I have more endpoints.
Zero.
If I'll create a couple of them move point.
I'll give it the gizmos over a dot.
I'll duplicate it a couple of times.
And now.
One, two, three.
I move points and I can get access to that move points with a number.
So I'm going to create an int for the number private int and I'll just give it a name of an I.
And then in the update I'm going to move transform of my trap with the help of Vector Tree move towards.
So we do this transform position.
Equals to vector three.
Move towards.
And it allows us to move from current position to target position by choosing vector three.
We're going to choose from current position, transform position to move point with the I.
That position.
And now we need to choose speed.
And I want to type speed multiplied by time.
Don at the time.
So it will move, not accordingly to the frame update, but accordingly to time the delta time.
Let's go and see this first.
So currently my eye is zero by default, so it's going to choose zero.
Move point.
First one.
Let me take it and move it away.
Just like this.
I'm going to set speed for.
I'm going to play mode.
We can see it's moving there.
Let's click.
This is Trigger then.
Now it's not here.
Now how we could change that, how we could make it move between points, we could just change the number
of the AI.
So let's check this if vector to the distance and it allows us to check distance between two objects.
So if distance between transform position and move point with the eye position is less than let's try
point 25 F, then we're going to do a plus plus.
So we're going to do plus one and we're going to check if I.
Is bigger than 1.. lengths.
Then we're going to do I equals to zero so it won't exceed the limit.
Let's go and see that.
I'm going to place this more points in the different places just like that.
Boom cycling between them.
Oh, an exit bounce.
One, three.
Wait a second.
Why is that?
Oh, yeah.
Let's put bigger or equals here.
Yeah.
And cycling between.
Very cool.
Now I want to make it rotate.
How we could do a rotation, we could change Z with the time on Delta time, so our rotation on the
Z would be like that and it will behave like it's rotating.
Let's put zero here.
Go back to script.
Over here in the update.
We can check if our position is bigger than destination on the X.
If transform position that x is bigger than move point a dot position means.
We are from the left of the moving point means we move into the right.
And actually, it doesn't matter.
We can just rotate to any side.
It will look pretty.
Transform.
Rotate.
And we need an oiler here.
I'll just do a new vector 300.
And for the Z, it's going to be rotation speed multiplied by time, dot, delta time.
Position that X and let's do another one else transform rotate new vector 300 minus.
So it's going to be another side rotation speed multiplied by time to time.
Let's save it all and go set some rotation speed.
Let's try to 100.
Look.
Let's see the unemployment actually.
Can I move them here?
Oh, my God.
It looks pretty, right?
I like it.
You can place as many move points as you like.
What I want to do here is kind of inconvenient to drag four objects together.
What I want to do here is to create an empty pattern for them.
And I'm going to name it as moving up again.
So now they are all together and I can move them like this.
I also want to delete one of the move points.
I don't like it.
So just two of them.
And I'll place one I know here and one here.
Kind of last touch here.
I want to set position of the trap equals to first move point so it will be spawned on the move point.
And it will look pretty and kind of predictable.
So just in the start, I can return just one line of code transform position equals to move .0. position.
So now, no matter what, it's going to be found at first position.
Let's go and test.
First point.
Second point.
And.
Yeah.
But it's not necessarily honestly, just something I want to do.
All right.
Good enough.
I think it takes long to make this video.
So let's go to prefab.
I kinda carry the weight by explanation of different points.
Let's just drag moving track to the prefab folder and we almost there in the next video, we're going
to change colors of the platform and then we can build level parts.
See you there.