We will shoot out a ray from this anime game object.
So we're going to do the same thing as we have done with this player game object.
So let's open up our anime script quickly.
Let's double click on that in order to open that up.
So we're going to delete some line of code.
Basically setting that line renderer component as using that we have done with this player game object.
So let's go to scape.
But before we do make sure you just go to the top of your class and include this namespace call using
system dot collection.
So it's going to allow us in order to delay some line of code, or it's going to allow us to use coroutine
function.
So we're going to do it somewhere over here.
And for we're going to use this AI enumerator function.
So AI enumerator.
And we have to name the function what we want to call it.
So I'm going to call it something like shoot shoot raycast something like that.
And let's make first bracket and some curly brackets.
So why are we using this function.
Because we're going to delete some line of code.
Basically, we want a line renderer component to shoot out from this anime game object to the left direction.
So we need to enable as well as after a short period of time we need to disable that.
So we just want to delete something after enabling it.
So that's why we're going to use this function in order to delay or in order to do some specific things
after a short period of time.
So this is the reason we're using that.
So first thing what we're going to do we're going to shoot out a ray.
So we're going to use a function called raycast 2D.
So we're going to use this physics 2D.
So we need to use this physics 2D dot raycast function and this function as a first argument.
It's going to take the position from where we want to shoot out the ray.
So we're going to shoot out the ray from this enemy GameObject gun point to the left direction.
And the second thing it's going to take the direction.
And the third thing it's going to take the length of the ray.
And the fourth thing it's going to take the layer mask, basically which game object we want to target.
So we're going to target this player game object.
So as a layer mask we're going to choose player.
So let's go to the top of our class and let's grab those things.
So first thing we need a position.
So let's make a public transform.
And we can call this one to something like shoot point or fire point.
So I'm going to call it fire point.
And the second thing we need the direction.
But we're going to use vector two.
Dot left basically means the left direction.
So we don't need to create a variable for that.
But we need a variable about our length of the ray which is just going to be the distance.
So I'm going to make a another public float.
And we can call this something like ray length.
And I'm going to call it instead distance.
And let's set equal to.
So I'm going to give a default value.
Or you can just simply close that up with semicolon if you just want to set and then underneath your
script.
But I'm going to give a default value something like ten F.
And the third uh, fourth thing we need the layer mask or which game object really we want to target.
So let's create another public layer mask and we can call this one to something like attack layer or
target layer.
So I'm going to call it target Layer instead.
And now let's go to that function where we're shooting.
Also we're doing it over here.
So first thing it's just gonna need the position from where we want to shoot shoot out.
So I'm going to pass our fire point dot position from the.
So make sure you just type this dot position basically accessing the position.
And the second thing we need are the direction.
So we're going to shoot out the ray in the left direction.
So we can simply use Vector2 dot.
It basically means -one inch the x.
So it's going to go to negative and y in the zero.
So we're not going to shoot out the ray in the y.
So that's why it's going to be zero in the y.
So we're going to use that vector2 dot left.
So we're going to use vector2 dot left.
It's basically means -one inch the x.
So the ray just going to shoot out to the left direction and why in this video?
So the ray not gonna shoot out at all in the Y.
So it's gonna shoot out in the left direction.
And the third thing we need the distance or the length of the ray.
So I'm going to pass my distance parameter.
And by default I set a ten unit as you can see.
So we're getting an error.
Basically we already created a target layer as you can see.
So I'm going to remove this.
And instead of we just ignore this parameter.
Or we can simply create a layer for this for this as well.
So in the top over here we can see we already have a target layer variable name.
So that's why we just getting a one getting error in order to create one more time.
Because we already have that.
So you need to some you need to name it something else.
Or you can just simply use this one as well.
Because as a target layer we have chosen that player game object.
We do know and we really want to damage our player game object.
So that's why we can use this as well.
So we can just going to remove this or you can simply create one as well.
It's totally up to you.
But I'm going to use this one because as a target layer I have chosen player.
And we really want to damage our player game object.
So that is the reason I'm gonna use this one more time.
So that's why I didn't create at all.
The second parameter, if you just want then you can create one more layer mask and you can call it
something like Attack Layer and Attack Layer.
You can simply choose your player game object or which game object you just want to damage.
So I'm gonna use that.
So as a fourth argument I'm going to pass this target layer.
And as a target layer I have already chosen the player which I really want to damage.
And then we're going to close that up with semicolon.
So this physics 2D dot raycast it's gonna shoot out the ray from this fire point dot position in the
left direction with this much of length of the ray.
And then it's only going to target our player game object.
So this over here, this ray just gonna return us a true value or false value, depending upon if the
ray is really collide with this player game object or not.
If it's, then it's gonna give us a true value, else it will give us a false value.
So what are we going to do?
We're going to store the result into a raycast hit 2d parameter.
Why?
Because this physics 2D dot raycast it's going to work as a ray.
So that's why we're going to store the result into another raycast hit 2d parameter.
So let's create a raycast.
Hit 2d and we have to call it what we want to name it.
So I'm going to call it something like hit info or hit in hit information.
And I'm going to set equal to this.
So this hit info over here.
It can be true or false depending upon if the ray is really colliding with this player game object or
not.
If it's then this function is just going to be true right away.
So that's why this hit info is going to be true, because we're storing this result into this hit info
container or into this parameter.
So what we do want to do now we're going to check if hit info is true.
Basically means the array really collide with this player game object.
So in that situation what we need to do we need to damage our player game object.
But before we do let's print out a message.
So I'm going to move over to our script.
And over here we're going to check if raycast.
Sorry.
If hit info is equals equals to true if that is the case.
So let's make some curly brackets and if hit info is true, basically means the ray is colliding with
that player game object.
Or we can simply just pass this hit info.
Hit info.
Basically, it also means the same thing means if hit info is true, then what we need to do, we need
to damage our player.
But for now we're going to print out a message.
So that's why we're going to type this debug debug dot log.
And we're going to print out message something like uh the player game object takes damage.
So we're going to use this parameter which is hit info.
So by this hit input parameter we can extract info something like the name of the game object that we
collided with.
And I think that we can access.
So what I'm going to do I'm going to use I'm gonna go over to this parameter which is hit info.
And then we're going to access the game object.
So sorry, we're going to access the collider.
Or we can simply access the transform as well.
And then we're going to access the game object and then name.
And top of that I'm going to add a custom message.
So we're going to use this plus icon and make quotation.
And I'm going to say something like text damage.
And let's close that up with semicolon.
So what we basically doing we just taking this hit info parameter.
So we're using this hit info parameter in order to extract information about the ray collided with.
So let's suppose the ray collided with this player game object.
So what we do what we're doing in in that moment we're just going to that game object transform component
and then access the accessing the game object.
So over here hit info dot transport.
Game object basically means the game object that they collided with.
So the ray is just going to collide with this player game object.
So we basically going over to this player game object Transform.
And by this transform we just accessing the whole game object of whole game object in our case player.
And then we just accessing the name and we can see in the inspector the name is player.
So this over here just going to be player the name of the game object.
And top of that we just added a custom message says take damage as you can see.
So this is going to be a message something like player takes damage.
So this message just going to pop up in the console right away.
So we can simply press down control S in order to save our script.
But before we do that, as you can see we're using this short raycast function.
So what we have to do, we need to use this return because we are getting an error.
As you can see over here in our function.
So what we need to do we need to use this yell return new Waitforseconds new wait four seconds and make
first bracket and close that up with semicolon.
So we're just gonna wait this amount of time.
So you need to pass in.
Wait four seconds over here.
The time after that amount of time, what you want to do.
So I'm gonna I'm gonna do something like after 0.1 or 1 to 4 second, we want to do some specific thing.
So we haven't set up the line renderer yet.
So we're going to print out a message first.
Uh, something like uh debug dot log for printing out message.
And we can simply say uh in quotation, uh, disable something like that.
Disable Ray.
Uh, or we can simply say LR means line renderer.
And we're going to close that up with semicolon.
So we also need to call this function, uh where we're going to call this function.
We're going to call it inside this function.
As you can see, we're just printing out a message in the console.
Say instead of doing that we're going to call this function.
This should should raycast function.
And what this function is doing.
Firstly it's just creating the ray.
And then if the ray just collide with this player game object, then we just printing out the name of
the game object.
As you can see, uh name, sorry name, as well as uh, which is uh, adding a custom message.
So this player takes damage messages gonna be pop up right away.
And after this amount of time.
Yield.
Return.
New.
Uh, wait for a second, then what we're doing, we just setting that disable alert so we don't, uh,
haven't set up the line renderer component yet.
So that's why we just, uh, printing out a message for now.
So we also need to call this function and this function, just gonna do this thing and make sure you
just using this yield return new.
Wait for a second and you can put the time in this brackets.
Uh, the time after that amount of time, you can just do specific things that you want to do.
So I'm just setting I'm just printing out a message says disable error.
As you can see.
So instead of printing out message over here in this shoot function we're going to call this shoot raycast
function.
But we do know in Ienumerator or in order to call a enumerator function we need to use this Startcoroutine.
It's kind of a bit weird, but we have to use and then make first bracket.
And this first bracket you need to put uh the function name.
So in our case this should raycast hit.
So we need to call the function inside this first brackets.
So in our case shoot raycast and then make this first bracket.
And then close that up with semicolon.
So we do know that we're calling the shoot function through animation event.
Basically whenever our enemy game object playing the animation clip which is the shooting one.
Then we're calling the shoot function and we also calling this shoot raycast function.
Once we call this shoot function and what should raycast function is doing?
Firstly, it is creating the ray from this position in the left direction with this much of length.
And then we only targeting our player game object and we checking in the next time.
If hit info means is true means our ray just colliding with that player game object, then we just printing
out a message in the console.
Says player takes damage as you can see.
And after this amount of time we just printing out message disable error as you can see.
So we're going to press down Ctrl S in order to save our script.
And now let's move over to unity.
So we will able to see that message.
So we basically have done the same thing as we have done in our player game object.
What we have done in shooting lecture on this player game object, we basically approaching the same
way or with our enemy game object.
So I'm going to select my console tab and let's click on this play button in the top to play our game.
And you will see once this anime game object is just gonna play the attack animation, it's gonna call
the shoot function.
And inside that function we just basically calling the shoot raycast.
We just creating, but we are we gonna get a point basically error.
As you can see we're getting an error.
So we're going to get out of play mode.
Basically we need to assign those variable one way one by one.
So if I select my enemy GameObject in the hierarchy then go over to Inspector.
We can see Fire Point and it says None Transform.
So we haven't created any fire point yet to this player game object.
So we need to assign as a target layer.
We already have chosen player, so make sure you choose that and fire point.
It says none transform.
So we need to create a fire point or a shoot point from where we want to shoot our way in this anime
game object.
So let's select the anime GameObject in the hierarchy.
Let's right click on it, and let's create an empty game object.
And I'm going to call this game object to something like fire point.
Fire point.
And we're going to move this point somewhere over here.
But before we do I'm going to select my enemy game object.
Then I'm gonna move over to this animation tab and then shooting.
So we need to drag the fire point somewhere over here on top of our gun in this anime game object.
So I'm going to select our fire point and we're gonna drag it somewhere over here.
So this is going to be perfect time.
Sorry.
Perfect position.
Uh, somewhere over here.
So if you just, uh, getting snipping or just moving one, uh, grid, uh, kind of a bit weird.
Then in the top, you can see magnetic.
So you can just click that in order to disable.
And now you can freely move that point anywhere you want, or we, uh, basically we're just getting
an error in order to move this, uh, fire point.
And I'm going to select and gizmos for that.
To do.
So let's go to Inspector.
Click on this box and we're going to select this yellow Christmas, something like that.
And now we're going to select our enemy one game object.
And let's select the fire point from that hierarchy.
And let's drag it over to this fire point slot.
And we're going to move over to overrides.
And then click on Apply All.
And now it should be fine.
But before I'm going to select my console tab and let's click on this layer in order to clear out all
of the messages.
And now let's click on this play button in the top in order to play our game.
So as soon as this enemy game object is going to shoot out on this, uh, our, on our player game object,
that should function.
Gonna call.
And in that shoot function, we just are calling the shoot raycast function, which is just doing the
best thing, which is creating our way.
As you can see, as soon as we just collided with this circle, we can see a message just getting pop
up in the console.
Says player takes damage.
So I'm going to click on this pause button in the top in order to pause my game.
And we can see player takes damage after, uh, after some period of time or short period of time,
we can see a message, gets pop up, says disable error.
Uh, basically, we also are doing a thing.
Basically, after this amount of time, we just printing out a message in the console.
Disable error.
As you can see.
So instead of doing all of these things, what we need to do, we need to, uh, show our line renderer
or the laser that we shoot in the left direction.
So we also need to do that, uh, do the thing as we have done in this player game object, which we,
uh, basically attach the line renderer component.
So we need to do the same thing with this player game object as well.
I'm going to click on this play button in the top in order to get out of play mode and make sure you
just go to uh, select your enemy one game object and make that one first prefab and then go to overrides
in the Inspector and then click on Apply All.
So the changes you made, it's going to be saved into this enemy one GameObject prefab as well.