Hello guys.
Welcome to the video.
In this one I want to show you how to make a trap with a spiked ball.
Just want to make a trap that would swing in the air from left to right.
And there are many ways to do that, but the way I want to use is more like a physically accurate.
I would say.
I want to push the trap once, and it's going to swing by itself the rest of the time.
So how it's going to be let's go ahead to graphic traps.
Spiked ball.
Let's take these two set pixel per unit 16 and then hit apply.
And I think we need some sort of a base for this trap where we can attach it.
And let's go to.
Terrain.
And over here we have a bunch of things.
I think we can use this sprite over here just to make it, um.
Look like a base of a trap.
So I'm going to drag.
This fall.
Square to the scene it's going to be.
And maybe this one could be a bit smaller.
Um, let me think.
Yeah, let's make it as my 0.5.
Maybe.
Sorry we need like this.
And then point five.
Yeah.
It looks good to me.
Just so we can place it somewhere in the middle of the level as well.
Now this is going to be the base for this trap.
Let's say it's going to be here.
Then we're just going to call it as, um, base.
Then let's make a parent for this one.
It's going to be trap.
Spiked ball.
Nice, then.
This object over here should have a bunch of components.
One of them would be distance.
Join 2D and rigidbody for this one should be static.
So this rigidbody will not move and it will serve as a base for the swing trap.
Now let's go to graphics again.
Trap spike ball.
Let's take this ball.
Drag it into the scene.
Let's make it as traps.
We want to put Circle Collider on this so it can damage the player is trigger.
I think it's okay if we make this collider a bit smaller so it's easier for player to play and maybe
like yeah, point 65 looks good.
Now on this object over here we just need to add a rigid body.
And it should be dynamic.
Let's change collision detection to continuous interpolate.
Then we're going to make it as a child of a spiked base like so.
And on the base we have this distant joint.
We can take the spiked ball and drag it to this field connected rigid body.
Now, believe me or not, this will work as it is even now.
It's a swing trap already.
I can just move it here and it's gonna hang like that.
Very cool.
If we wanted to move automatically without interference of developer, we can give it a script.
Let's go to scripts and make.
The sharp script.
Sharp spiked ball.
Oh it's okay.
Then I'm going to select the parent.
Put the script on.
Then inside we can do something like reference to the rigid body we need.
So private RB.
Uh, region by the.
Spike RB and then we want to have serialized field, private float and maybe push force.
Then in the start method we're going to do this.
Let's just do RB.
Actually, let's make vector two push vector equals to new vector two with a push force, and for the
y can be just zero.
Then we're going to do spike rb.
Add force of push vector with a with the force mode impulse.
Just like that.
Now we can save this.
Go back to unity.
We can assign this ball over here.
Come on, boy.
What's up?
Oh, this is rigid.
Body should be rigid body 2D.
A salmon.
Or smooth impulse.
Okay, I'm going to save it.
Go back.
We can drag this to the field and push force can be something like, I don't know, uh.
Ten.
Let's try ten.
And now we can go to play mode and you'll see it will be pushed.
Then it's going to swing like that automatically and it's already dangerous to touch.
Oh.
No damage.
Why not?
Because I forgot to add damage.
Trigger.
Okay.
And I got damaged.
Nice.
Now I want to improve it.
And we can easily do that by adding one more element.
Let's go to graphics traps.
Spike the ball.
There is this chain.
We can take the chain, drag it into the scene somewhere.
Here.
Let's make it as trap.
Going to place it maybe here, and I'm going to make it as child of a drop spike ball.
Now we need to join the chain with the base.
And also we need a couple of more chains.
So I'm going to make one more here and one more over here.
Yeah.
Let's place them with an even distance.
Yes.
Something like this will do.
Okay, I like it.
Should we make bigger distance?
I don't know, looks looks good to me.
So I'm going to keep it this way.
Yeah.
And we need to join these chains with a hinge joint to D.
We're going to add hinge join to D on this game object.
And we're going to go to chain.
Let's select all of them actually and add component of a hinge joint.
Now it's going to have rigid body and hinge joint as well.
Then we're going to go to base.
And as a hinge joint over here as a connected rigid body we're going to choose chain.
Then we're going to select chain and drag chain one to this field.
Then chain one going to be connected with the chain two and chain two going to be connected with a spiked
ball.
And now when you go to play mode you'll see that all of them are connected.
And it swings like that.
And I really like how it looks.
So that's all what we need to do now.
We have a dangerous trap that moves left and right.
Yeah, so that's good.
If you want to make it longer, then you need to add more chains and connect them.
Other than that, I think that's all what we need to do.
Let's go to prefab make it as a prefab GameObject.
And now we can take it, place it somewhere else if you want.
Maybe here you can change push force for this object like maybe uh minus ten.
So it's going to be pushed to the left and that's it.
Now we have a dangerous level.
That is not very easy to beat.
Need to wait for the soul.
Then need to jump here.
Wait for the swing trap.
Take the banana.
Jump, jump, jump.
And a very cool one.
Love it.
Okay, see you in the next video.