We want to shoot out the arrow.
So before we shoot out the arrow, we need to create our arrow.
So let's head over to our project app assets and then sprites folder, and I will head over to my anime
folder one folder.
As you can see I have my arrow sprite right over here.
So let's select that.
So we're going to click on this arrow and we will select this one.
Let's drag it over to this in view or into this hierarchy.
So as you can see as soon as we drag it it seems a lot more small.
So we need to make make it a little bit bigger.
So we will select the sprite sprite from that folder.
Then go over to Inspector and pixel per unit.
We're gonna make it less so in order to make this sprite large in size.
So we need to make this pixel per unit a less number and filter mode.
We're going to choose point of filter to get rid of blurry.
And let's click on this apply button in the bottom right.
And now as soon as we have done it we can see our arrow?
Uh, but it's, uh, facing to the right direction.
So we want to flip this arrow horizontally in the left direction.
So we will select our arrow.
Let's go over to Sprite Renderer in Inspector.
And we're going to check the flip in the X basically horizontally.
And let's call it arrow instead of calling arrow underscore zero.
So let's call it arrow.
So we will add some component.
First thing I'm going to add a rigid body 2D for gravity.
And I'm going to set the gravity equal to zero.
So it's not going to fall down at all.
Or we can just set it to kinematic.
So let's click on this add component.
And let's find rigid body 2D by typing.
And let's attach that.
And I'm gonna set this body type to kinematic and collision detection.
We're going to choose continuous and interpolate.
We can choose interpolate to making a little bit more.
And let's let's add another component for collision detection.
So let's click on this add component.
And we're going to add a box Collider 2D.
And let's attach that.
And let's click on this Edit Collider right over here.
And let's edit the collider as the size of our arrow.
Sorry let's click on this Edit Collider.
And this seems pretty much fine.
And once you are satisfied then you are good to go.
And now what we want to do, we want to assign this arrow as a arrow tag.
So we will choose our arrow in the hierarchy.
Let's go over to Inspector and Tag.
As you can see it says tag.
So let's click there.
Let's click on Add tag.
Then click on this plus icon, name the tag whatever you want and then click on save.
I already created a arrow tag as you can notice right over here.
So I'm going to choose my arrow one more time in the hierarchy.
Go over to Inspector and Tag which says untagged.
We're gonna assign with our arrow.
Now we assign it this arrow as arrow tag, As you can see in the inspector.
So I hope you get the idea.
Now let's turn this arrow into a prefab.
So we will have the copy, and we will spawn it via when we need it.
So let's select our arrow in the hierarchy, and let's drag it over to this prefabs folder in our project
tab inside this assets folder.
Now we turn it into a prefab.
So let's select the arrow in the hierarchy and let's delete it from the scene view now.
And we want to spawn it once this enemy GameObject just shooting out basically playing that arrow fire
animation clip.
So we will open up our enemy one script.
So let's double click on it.
And so first thing what we need we need a reference of that arrow prefab.
So if I move over to my project tab assets and then prefabs folder.
As you can see we have the arrow right over here.
And this arrow itself a whole game object.
So we need a reference.
And after that we can just shoot out that.
So let's go over to our anime one and let's go to top of our class and let's make it over here.
So let's make a another serialized field.
Uh, private, uh, private.
Sorry.
This will be private, and the component will look sorry.
Not component.
Are we looking for our game object?
So we're going to type game object.
And the game object is arrow.
So we can simply call it something like arrow prefab.
So let's call it arrow prefab.
And let's close that off with semicolon.
And make sure you press down Ctrl S to save your script and then move over to unity.
So underneath our anime one script a field just gonna pop up says Arrow Prefab.
So what we need to do there, we basically have to select our arrow prefab from this prefab folder,
and we need to drag it over to that slot.
So we will have the reference.
And then we can just spawn it when we need it.
So let's select our anime one in the hierarchy.
As you can see underneath our enemy one we can see arrow prefab and it says non-game object.
So let's choose our arrow prefab from that prefabs folder and let's drag it over here.
Now we can see we have the arrow prefab as you can notice, and we are ready to spawn it.
So let's open up our enemy one script one more time.
And for that we will make a function to spawn our arrow.
So let's make it somewhere around here.
And we're going to make this function public since we're going to call this function through animation
event.
So let me explain.
If I move over to unity, select this anime one and then move over to animation tab.
And then click over here and then choose this arrow one sorry enemy one underscore arrow fire animation
clip.
So we want to call the function somewhere over here as you can see.
So this is just going to be perfect time in order to call.
So we will call the function here through animation.
So that's why we need to mark that function as public as we have done inside this player game.
Object Attack.
Uh, I hope you get the idea that how we can do it, uh, we have done the same thing inside this player
game object once we basically calling the attack function.
So let's move over to our script and let's make this function public and, uh, public void.
And we can call this function to something like spawn arrow.
So let's call it spawn arrow.
And let's make first bracket and some curly brackets.
And this function over here as if to spawn our arrow, we do know that we need to use this instantiate
function.
And we need to make this brackets and this instantiate function as a first argument.
It's going to take the game object that we want to spawn.
So in our case arrow prefab.
And the second thing it's going to take the position from where we want to shoot out.
So we also need to create our shoot point or fire point where from we want to fire.
And the third Another argument.
It's gonna take the rotation.
Uh, so we need to assign those variable.
So let's make this one comment for now by double forward slash.
And let's go to top of our class.
And let's grab those reference first.
So the first thing we need we have the arrow prefab.
So the second thing we need our position.
So let's make a serialized pill private a private.
And for position we're going to type this transform which is responsible responsible for position.
And we can call this one fire point.
So let's call it fire point.
And let's close that up with semicolon.
And let's create another variable for our arrow speed with how much speed.
We just want to shoot out the arrow.
So let's create another serialized field private and we can call this will be float.
And we can call this one arrow arrow speed something like that or arrow fire speed.
And by default I'm going to set equal to ten unit Ten F.
And let's press down control S on our keyboard in order to save our skate.
And now let's move over to unity.
So we will see underneath our enemy one.
Skip those fields.
Just gonna pop up.
So first thing we need to assign our, uh, fire point.
So we basically have to create our fire point from where we want to shoot out the, uh, shoot out,
or want to fire our arrow.
So we need to assign that.
As you can see, as soon as our skip compiles, we can see fire point and it says none transform.
So let's create our fire point.
So let's select our enemy one GameObject in the hierarchy.
Let's create a right click on it.
And let's create an empty game object.
And we can call this GameObject to something like fire uh fire point, uh fire point.
And let's choose an gizmos for it.
So let's go over to Inspector, click here and I'm gonna choose this one.
And let's move it somewhere around here.
And you can check that by selecting your anime one.
Then moving over to animation tab, then choose the Arrow Animation clip.
So I guess Arrow Fire should be right over here.
So this is gonna be perfect point.
So make sure you just adjust that.
And now this seems pretty much fine.
And once you are satisfied, make sure if you turn it your anime one into prefab, then go over to Inspector
Overrides and then apply all.
And we're going to take our fire point and let's drag it over to this fire point slot.
Now we can see we have the fire point as well as the arrow, and we are ready to spawn it.
So let's open up our anime one strip and let's move over to that function.
And let's remove this two forward slashes.
So this instantiate function as a first argument.
It's going to take the GameObject that we want to spawn.
So we will pass our arrow prefab and arrow prefab.
We do know that we just pass our arrow as you seen earlier.
And the second thing it's going to take the point.
So we will pass our fire point dot position.
And we do know that as a fire point we just drag it that fire point as you seen.
And the third argument it's going to take the rotation.
So we're going to put two quaternion dot identity.
It basically means no rotation.
And let's close that up with semicolon.
And we're going to press down Ctrl s to save our script.
And now let's move over to unity.
So we just created that spawn arrow function.
So we also need to call in order to spawn our arrow.
So when we need to call it once our enemy this enemy one basically playing that arrow fire animation
clip.
So we will select our enemy one in the hierarchy.
Then move over to animation tab.
Let's click here and we're going to choose our Arrow Fire animation.
Let's move it over to something over here.
So this is going to be perfect time.
So from here we want to call this function which is our spawn arrow.
And in order to call that in this animation tab in the left side we can see Add event basically right
over here.
So just click that and then select that event.
Then go over to Inspector.
As you can see function.
And it says no function.
So we will click there and we will go to anime script or anime class.
Then methods basically function.
And there as you can see we created spawn arrow function.
So we're going to choose that.
Now uh, whenever this anime is just going to play this animation clip right over here, that uh, spawn
arrow function, just going to call out.
And we do know that what that function is really doing, it just spawning our arrow.
So let's have a look.
As you can notice right over here I'm going to clear my console tab.
And let's click on this play button in the top in order to play our game.
And let's get in attack range.
So when we whenever we're gonna move over to our attack range you will see that.
So just wait.
So if we get in attack range, as you can see, as soon as we get we can see arrow prefab just spawning
from that.
Uh, and you can notice right over here.
But the problem is that it's staying at one position.
Also it's pushing our player to the right direction.
So what we need to do, we basically have to tweak some values.
So firstly it's not moving.
So let's get out of play mode.
Uh before we do it let's add let's deal with uh, that uh, our arrow just pushing our player to the
right direction.
So basically what we have to do, we need to move over to our project tab, then assets and then prefabs
folder.
And we will select our arrow prefab.
Then go over to Inspector, then Box Collider 2D.
And make sure you just check this trigger.
So it's just gonna, uh check for trigger.
So uh, trigger and it's not gonna push, uh, our player, uh, player basically for trigger and now
and that's fine.
And now it's not gonna push our player.
So let's click on this play button in the top.
Basically we just checking for trigger.
And, uh, when we gonna trigger, uh, with anything.
Not, uh, for collision.
So when we're gonna trigger, that just gonna happen.
It's not gonna push our player, as you can see.
And this seems pretty much fine.
When we get outside of this attack range, we can see enemy stops shooting out.
Uh, arrow, arrow.
Once we are out of, uh, once we are out of from our attack range of this enemy one game object.
So these arrows are just staying at one position.
So we need to just we need to move this arrow.
So let's get out of play mode.
And in order to move that arrow, we basically have to grab the reference of this rigid body.
So if we if I select my arrow prefab from that prefab folder and go over to Inspector.
As you can see, we added a rigid body to the component.
And this component is responsible for velocity, adding force and all of kind of things.
So what we need we need a reference of this component which is rigid body 2D of this arrow prefab GameObject.
Then we can just add a add up the velocity.
So let's go over to Inspector Sorry script.
So we spawning this arrow prefab at this position with no rotation.
So after spawning that arrow we're going to put that arrow into another game object.
Or you can simply say into another container and that will be GameObject.
Why?
Because this arrow prefab itself a game object and we have to name it.
So we can call this one arrow prefab.
So let's call it arrow prefab.
And let's set equal to this.
Basically what we're doing.
We just are taking this arrow prefab where wherever we spawning and we just storing that arrow that
we spawn it into this arrow prefab.
And now what we're going to do, we're going to access the rigid body.
So we will use this stem arrow prefab dot game object dot get component.
And the component we're looking for is a rigid body 2D.
So we're going to type rigid body 2D and then make this brackets.
And we can set equal to uh sorry.
We can set the velocity.
So we will type this linear velocity equal to that we want.
So let me explain one more time.
Firstly we spawning this arrow prefab in from this spawn position which is fire point or position with
no rotation.
And after spawning we just taking that arrow prefab and we storing that arrow prefab into this stem
arrow prefab.
And after that what we're doing we're just going over to temp arrow prefab and accessing the whole game
object.
So in our case basically we're just going over to this arrow game object.
And then we're using this dot get component in order to get the component.
And the component we're looking for is a rigid body 2D as you can see right over here.
Basically we're just going over to this arrow prefab and then accessing this rigid body 2D and then
accessing the velocity of it.
Linear velocity as you can see right over here.
Sorry.
Just a little bit.
I really apologize.
Let's go back.
So we just accessing the velocity linear velocity of that arrow.
And now what we have to do we need to set the velocity.
So firstly what we will do we will set the velocity with our uh basically uh we created arrow Spirit
right over here.
So we're going to pass our arrow speed.
So let's pass arrow speed.
Arrow speed.
And let's close that off with semicolon.
Uh so as you can see we're getting an error.
Basically we need to convert this into a factor two.
Basically what we will do right over here we're going to put transform.
Sorry.
Uh transform transform dot.
Sorry not transform dot.
Right.
Basically we're going to use fire point dot right.
Fire point dot right.
So what basically this mean fire point dot right means that which direction our fire point is facing.
So if I choose my fire point underneath my enemy point game object in the hierarchy, as you can see,
our fire point is facing right direction.
So fire point dot right basically means which direction our fire point is facing.
So we want to move our arrow prefab to the left direction.
So that's why right in front of this fire point dot right we're going to put minus.
Now it's going to be left direction.
And we we're just going to multiply that direction with our arrow speed.
So as so our arrow is just going to move.
So what we will do.
We just going to want to multiply this 5.0 right, with our arrow speed.
Arrow speed.
Let's multiply that.
And now that arrow is just gonna move with this arrow speed.
So firstly what we're doing we just accessing the rigid body 2D component of that arrow prefab and then
accessing the velocity.
And we setting the velocity equal to which direction our fire point is facing.
And we just put minus basically now it's gonna be a left direction.
And we just multiply that direction with our arrow.
Speed basically means that that arrow just gonna move with uh, with this much of arrow speed, as we
specified right over here.
So since we just made it serialized field so we can access this parameter underneath our enemy one script.
So there we can just adjust if we want.
So now what we're gonna do, we're gonna press control S to save our script.
And now let's move over to unity and you will see.
So just wait a little moment and you will see that.
So let's have a look.
So I'm going to click on this play button in the top.
So as soon as this anime one game object just going to play arrow fire animation clip then arrow just
going to spawn.
And that arrow just going to move in the direction.
Uh, in the direction uh, fire point is facing.
Basically negative direction in the fire point is facing means to the left direction.
So let's get in attack range.
And we can see by now arrow is just going to the left direction.
As you can see fire point dot right basically means right direction, the direction our fire point is
facing.
And we just put minus uh, to uh, to basically move uh, over to negative direction.
And we can see indeed it's working fine.
But the problem is that as you can see, uh, our, uh, arrow.
Once we are to the right direction, we can see that our arrow is, uh.
You can simply.
I'm going to pause my game.
Arrow is not facing to the, uh, to the right direction.
Once we shooting out the shooting out this way, to the right direction.
So we need to flip this.
Uh, basically, we need to work with rotation of this arrow prefab.
So let's get out of play mode and what we have to basically do their, uh, so instead of typing quaternion
dot identity basically means no rotation.
We're going to pass this fire point dot rotation and fire point dot rotation basically means the rotation
fire point is facing.
Now it should be work fine.
So we're going to press down Ctrl S to save our script.
And let's move over to unity.
So now by by now that arrow just gonna rotate in the direction fire point is rotating.
So it will be okay by now.
But, before we have it, let's take a look on it.
So what we will do, we're going to play our game.
Firstly I'm going to select my enemy one game object in the hierarchy.
And let's press down F in order to focus on it.
So let's click on this play button in the top in order to play our game.
And let's test that out.
So now the rotation just gonna just gonna be find of that arrow because we just type fire point dot
rotation basically means that arrow just gonna rotate rotate with our fire point dot rotation.
So let's get in.
And if we just move.
And now as you can notice, if I pause my game, we can see the rotation of this arrow.
It's now to the right direction.
Once our player once our enemy one shooting out the arrow to the right direction.
Now our arrow just facing to the right direction.
So Direction.
We just work with rotation.
So instead of passing right over here, quaternion dot identity basically means no rotation.
Pass fire point dot rotation means the rotation will be with our fire point dot rotation.
So I hope you get the idea and what we need to do after some period of time.
We need to destroy these arrows.
So let's get out of play mode for it.
We're gonna create a script.
So what we're gonna do and we're gonna attach that script to this, our arrow.
So let's create a script for it.
So we will head over to our project tab assets and then script folder.
Let's right click there then create Monobehaviour script.
Or you can move over to scripting and then click on this Monobehaviour script.
And you can name that script whatever you like.
Just name it something like arrow.
As you can see, I already created a arrow script.
So what I'm going to do, I'm going to select my arrow prefab.
Let's double click on it.
And in order to open that up in prefab mode, and we're gonna attach our arrow script.
So I will head over to my script folder and let's select this arrow script and drag it over to this
add Component section and Inspector.
Or we can simply click on this add component.
And we can find the script by typing the name of the script which is arrow, and we can hit enter in
order to attach that.
I'm not gonna do it twice since I already attached my arrow script.
So I'm gonna go back and let's open up the arrow script by double clicking on it.
Make sure you attach this as arrow script, uh, to that arrow game object.
So we will move over to this start function.
So we do know that start function get call in the start of the game.
So in the start of the game we will destroy.
But we don't want to destroy immediately.
We want to destroy after some period of time.
So as a second argument we can just specify the time.
So to destroy we're gonna use destroy this destroy function.
And this destroy function as a first argument, it's going to take the game object we want to destroy.
So we want to destroy game object itself.
So we will pass this dot game object.
And this dot game object basically means it's referencing our game object itself comma.
Second thing it's going to take the time.
So after five seconds I want to destroy or seven.
So I'm gonna put seven and then close that up with semicolon.
And if you want to adjust then you can just go over to top of your class and you can make a variable
for it.
So let's make a serialize field.
So alias field private and private this will be float.
And we can call this something like destroy time.
So let's call it Destroy and destroy time.
And by default I'm going to set it to something like five.
And let's replace this seven F with our destroy time.
So let's replace this with our destroy time.
And we do know that we just marked as serialized field so we can adjust the time in Inspector and we
will remove this update function.
We don't need it and we're going to use some certain function later.
So let's press down Ctrl S in order to save our script.
And let's have a look and make sure you attach that script to your arrow game object.
Uh, so, uh, so our arrow is just gonna destroy itself after with our, uh, after, uh, after,
you can say with destroy time.
Destroy time, as we specified in our script.
So now let's have a look.
So I'm going to click on this play button.
So you will see as soon as our uh, as soon as, uh, our destroy time just going to be over basically
arrow just going to destroy itself after that amount.
Destroy time.
So let's have a look.
So if I if we get in attack range, we can see it's shooting out the ray.
And you will see this arrow just going to destroy itself after after that destroy time amount.
As you can see, indeed it destroying right over here.
So after something like five seconds it just destroying itself.
So if I get out of play mode and then move over to my prefabs folder and then select this arrow, we
can see our in our arrow script.
In Inspector Destroy Time, we have chosen five.
You can make it less.
You can make it bigger.
It's basically up to you.
So I hope you get the idea that how you can just do it and make sure you attach your arrow script to
your arrow prefab game object.
So it's gonna do that thing, or else you're gonna get some error.
So you need to make sure that and whatever changes we have done now in in this anime one game object.
So let's move over to Inspector Overrides and then apply all.
So the changes we made over here, it's going to be saved into this enemy one GameObject prefab as well.
And I'm gonna press Ctrl S on my keyboard in order to save my project.