We will create floating text.
Basically, once this player GameObject attack on this enemy GameObject and this enemy just takes damage,
we want to show up the floating text.
So we're going to create that.
So before we create we're going to create an empty game object.
And we're going to make that text a child because we're going to have the animation.
Firstly the text will go up and over time it's going to fade out.
So that's why firstly we need to create an parent game object.
So let's go to hierarchy.
Right click or hit this plus icon.
And we're going to create an empty game object.
And I'm going to name this game object to something like floating text floating floating text floating
text.
And I'm going to choose an icon for that something this one.
Or we can simply choose any.
And now I'm going to drag it top up in my hierarchy, this game object, so I can see all the time.
And what we're going to do now.
We're going to create an empty game object and into that child game object we're going when I add the
tags, uh, because we're gonna have the animation and we're going to create the animation in the game
object, or it's kind of gonna be weird if you just don't create an parent game object.
So make sure you just do that because we're gonna have the animation in our child game object.
So that is the reason, or else we're gonna get some weird, uh, animations.
So that's why.
So let's select the floating text game object.
Let's right click on it and let's create an empty game object.
And I'm gonna name this game object to something like text uh text.
And what we will do, we're gonna add a text mesh component to this text in order to be able to see
our text.
So select the text game object in the hierarchy.
Go over to Inspector, click on Add Component and we're going to add a text message text mesh.
Don't add this text.
Instead you need to attach this text mesh.
So just click there in order to attach.
And we're not able to see our text because this takes over here.
Field is totally blank.
So we're going to put something like 100.
As soon as we have chosen 100, we can see we are now able to see our text.
But it's kind of bit weird.
So what we need to do, we need to move over to this character size.
And we need to make it as much small we can.
So I'm going to make it something like 0.1.
And after doing that make sure you just go over to this font size and increase the font size.
So I'm going to increase something like 100.
And now we are able to see and font style I'm going to choose bold.
And this is fine.
And we need to center it.
This text uh in this floating text game object.
So what are we going to do now.
We're going to choose alignment to center as well as anchor to upper left.
From this upper left we're going to choose middle center as soon as we done.
Now that text is now centered as you can see.
And if you have any font then you can just drag it over to this font field.
Or you can just click over here in order to choose a font.
So I'm going to drag a font right over here.
A monochrome, uh, font.
Uh, of course I will have a link in the resources.
You can go and check that out.
So I'm going to move over to my project tab assets and then uh fonts folder I have many fonts.
So let's suppose I'm going to select the first one and drag it over to this font filled in Inspector.
As soon as we drag it now we able to see our font.
The text just gets with our, uh, gets changed with our font.
And I'm gonna do with this one as well.
It's basically up to you which font you just want to choose.
And I'm going to make the font size a little bit up, something like one, two, five.
And it's pretty much cool.
So now let's make the animation.
So make sure you select the tile game object, not the parent one.
So that's why we just created a parent game object first.
And then we just created a text game object.
And we just attach the text into this game object because we're going to have the animation in this
text game object, not in this floating text game object, or else the animation just gonna be weird.
So that's why we have to do so.
We're going to make the animation by now.
So let's select the text game object underneath our floating text in the hierarchy.
Let's move over to animation tab and let's click on this create button in order to create the animation.
So I'm going to move over to my assets folder basically where I have the animation folder.
So I'm going to open that up.
So let's double click on it.
And I'm going to move over to this add stop animation stop underscore animation sub folder.
And we have to name the animation what we want to name it.
So I'm going to call it something like floating floating floating text.
Or we can simply call floating underscore text.
And let's click on the save button.
And we are ready to create the animation.
But before you create make sure you just hit this red button in animation tab in order to record your
animation.
And I'm going to move over to something like 160 frames or this, uh, over here in one second.
And I'm gonna move this a little bit up this text.
So select the child one, not the parent one.
Select the child and we're going to move it up something like this much.
And also I just want to make this one transparent.
So it's gonna seems like it's fading out over time.
So let's select the text and let's go to inspector then mesh renderer.
Mesh renderer not sorry not mesh renderer.
Basically we need to go over to this text mesh.
And in the bottom you can see color field.
So just click there and then you can just drag this a in the bottom you can see 255.
So I'm going to make it zero.
Or we can just drag it all the way to the zero.
And now if I just click on this play button in animation tab we can see our animation.
But uh we get some error.
So it's kind of bit weird.
So what we're are we going to do?
We're going to leave that instead.
So I'm going to move over to my color field once again.
And I'm going to move this all the way to 100.
Something like that.
And we can just go over to this materials.
I guess it's fine.
Uh, so we can just ignore this thing as well, but you can just rotate, uh, as well.
So if you just move over to once again, basically 60 frame, and let's suppose you just want to rotate
this text a little bit in any direction.
Then you can just go over to transform rotation.
And you can just play around with z axis as well.
So now if I just click on this play button, as you can see it's going up and it's just rotating a little
bit.
So what I'm going to do now basically I don't want to do this thing.
So I'm going to set back to zero instead.
I just want to uh basically this takes uh to be small, something like that.
So by default we just want this text to be, uh, in size, something like 125.
So let's hit this red button in order to capture the animation.
And by default, what I'm going to do, I'm going to make this font size a little bit small.
And then once this takes pop up, then over time I'm going to just increase the size of this text as
well.
So let's select the text in our hierarchy.
Make sure you just hit this red button.
So make sure you just out of the record of your animation by hitting this red button.
And then you have to do it.
So I'm going to select my text game object underneath my floating text.
And let's make the font size something like, uh, ten I guess.
Or we can simply make 50.
And once we create the animation, basically it's going to go up and over time it's going to increase
the size itself.
So when I hit this record button, red button in order to capture the animation and I'm gonna move uh,
60 frames, something like that.
And now what I'm gonna do, I'm gonna increase the font size.
So we will go to text and the font size.
I'm going to make it something like 125.
And if we just click on this play button by now, we can see it's just going up.
And over time the font just increasing as you can see.
So you can just play around with.
And once you set it satisfied then you can just click on this red button to capture the animation.
Or you can just leave it like that.
It's automatically gonna capture the animation.
So what are we gonna do now?
We're gonna make this floating text game object a prefab.
So we're gonna have the copy and we can spawn it anywhere we want.
Something like, once this enemy game object takes damage, then we're gonna spawn this.
So let's make this one a prefab.
So we're gonna have the copy.
So we're gonna select our floating text game object.
And let's drag it over to this prefabs folder into this project app, firstly project app, then assets.
And you can just drag it over here as well.
But it would be nice if you just create a prefab folder and just drag it there to keep your project
clean.
And what we will do, we're going to create a script because we don't want this number to be 100 always.
We just want to choose a random number.
And then we're going to show that up.
So we're going to create a floating text script in order to do it.
So let's move over to this project tab then assets and then script folder.
Let's right click there create and then Monobehaviour script.
And you can name that script what you like.
I already have created a script for that and I just name it Floating text as you can see.
So now what we're gonna do, we're gonna attach this script to our floating text game object, basically
to the parent one.
So let's select that, drag it over to this Add Component section.
And let's double click this to open it up in Visual Studio.
So what we're going to do we're going to remove this uh update function as well as I'm going to remove
this comment.
We don't need uh instead we need a reference of this text message.
So if I select the text game object and then go over to text Mesh.
So we need a reference of this text mesh.
And we can see over here 100.
So we need a reference of this.
Then we can change this number any number that we would like to go with.
Or we're going to choose a random one.
So firstly we need a reference of this.
So let's go to script and let's grab the reference first.
So I'm going to make a public.
And the component we're looking for is a text mesh.
So we need to type the component which is text mesh.
And we can call this one to something like with non capitalized t letter text mesh.
And let's close that up with semicolon.
And we're going to press down Ctrl S in order to save our script.
And let's move over to unity.
So underneath our floating text script we're going to able to see a text message F is just going to
appear.
So what we need to do there we need to drag this text into that slot.
So we're going to have this text mesh reference R a reference text mesh component reference.
Then we can just change the number with a random number, so just wait for it to compile.
And now if I select my floating text game object, we can see underneath our floating text a text message
appear and it says none text message basically empty.
So what we need to do now, we do know the text mesh is attached to this text game object.
So we need to select this text and then drag it right there.
Basically this text game object.
And we can see now we have text and the text mesh component as you can see.
And we are ready to do that.
So let's open up our script because we have the reference.
And let's move over to this start function.
So in this start of the game we're going to choose a random number.
For a random number we're gonna use this random dot range function.
So we're going to use this random dot range function.
And this over here.
So make first bracket.
It's going to take the minimum inclusive and the maximum inclusive.
So I'm going to pass or I want to start from one.
So that's that's why I'm going to pass as a first argument.
One.
And.
As a second argument I'm going to pass one zero, one and then close that up with semicolon.
So this random dot range function over here I'm going to pick up a random number between 1 and 1 zero
one.
And we definitely definitely know that this 101 will not be included.
But the 100 will be and this one as well.
So this number can be between 1 and 100.
It can be 99.
It can be 100, it can be 54.
Anything like that.
So this random dot range function is just going to pick up a random number between 1 and 1 zero one.
So what we're going to do whatever we get.
Or this random dot range function pick up.
We're going to store that number into a another variable.
Or you can say into a another container.
And that's going to be an integer.
Why.
Because this one we do know integer zero, one, two three, four and 100.
And all of these are integer numbers.
So that's why we need to put it in a container called integer container or integer variable.
So I'm going to create an int for referencing the type of the variable.
And we have to name it.
So I'm going to call it something like random number.
Or we can simply call it a number as well.
And let's set it equal to this.
So this number over here going to be a random number between between 1 and 1 zero one.
Or it's going to depend upon what this random dot range function pick up.
So this let's suppose this random dot range function pick up 60.
So this over here number just going to be 60.
As you can see by directly we setting it.
And after doing all of this thing what we have to do we need to update the text as well.
Basically we need to change this 100 text with 60 because that number is going to be 60.
So what we're going to do over here, we're going to move over to that text message because we have
the reference as you seen.
And then we're going to access the text and we're going to set the text equal to this number, and we're
going to close that up with semicolon.
And we get some error.
We just get a little bit error.
And it says in true in string.
Basically this over here number is an integer value and this one is a string.
So we need to convert this integer.
This number is an integer value.
So we need to convert this one into a string.
And in order to convert that we need to use this dot two string function.
And then make first bracket.
And we can just press down Ctrl s in order to save our script.
And we can just move over to unity.
So just wait for to compile.
And let me explain one more time.
So firstly we grabbing the reference as you can see and I really apologize.
Then we're just going to take space and then accessing the text.
And we're setting sitting equal to number.
Basically we're just going over to this text mesh component.
And then we just accessing this text which is 100 as you can see.
And we're setting equal to number.
And let's suppose this number or this number just be something like 65.
So this gonna be over here 65.
So that's why this text dot text just gonna be 65 right away because we're setting it as you can see.
So this text right over here just gonna be 65, but it's just gonna be a random number, as you can
see.
Uh, basically as you seen, we have used that random dot range function in order to do that.
So let's have a look by now and make sure you just drag the reference.
And we will go to overrides and then click on apply all as well.
So now let's click on this play button in the top in order to see that.
So just wait for to compile.
And as you can see, it became 42.
And the animation just looping.
Basically it's just going over again and again.
The animation clip is playing.
So let's get out of play mode and we need to uncheck the loop time of this animation clip.
So to do that let's go to project tab assets and then animation.
And I have the floating text.
So let's select that and I'm going to uncheck this loop time in Inspector.
And now if we just click on this play button you will see the animation will be played just one time.
And the number is just going to be a random.
As you can see as soon as it started it's now 23.
And if we just get out of play mode and then click on this play button in the top, you're going to
see a different number by now because we're using that random dot range function.
So just wait.
And as soon as the game started now we can see 52.
But we also need to destroy this number after some period of time.
And we do know that we're just completing the animation clip of this floating text after one second.
And let me show that.
So I'm gonna get out of play mode.
And if we select Floating Text and move to animation tab, basically the child one, and we can see
after one second we're just completing our 60 frame.
We just completing the animation clip as you can see.
So after this period of time we need to destroy this floating text game object as well.
So we need to destroy that.
So to destroy let's move to script.
And we're going to move over to this start function.
So in this start of the function we're going to destroy.
So we're going to use this destroy function in order to destroy that as a first argument.
It's going to take the game object that you want to destroy.
So we're going to pass this dot game object basically means it's just referencing itself.
And the second thing we need to specify the time in after what x amount of time that you just want to
destroy.
So I'm going to pass for something like one app, basically one second.
So I'm going to pass one app and let's close that up with semicolon.
And so after this amount of time this game object just going to destroy itself as you can see.
And we can just put this line of code in the bottom by holding down alt and using down arrow key.
Or you can just use up arrow key in order to move this up.
But I'm gonna move this down.
So that's why let's press down Ctrl s to save our script and let's move to unity.
So after one second you will see this takes this floating text will also be destroyed.
Now let's have a look.
So I'm going to click on this play button in the top to play our game.
So after one second it will be destroyed.
So as you can see, it does get destroyed once it's, uh, the time is over.
So let's get out of play mode.
And what we're gonna do instead of showing by default, we're gonna delete this one from our hierarchy
as well as in the scene view, because we only want to spawn once this enemy game object takes damage.
So we're going to select that and we're going to destroy it or delete it now.
And let's open up our enemy script where we're taking damage from our player game object.
So let's select the enemy one game object.
And let's open up the enemy script by double clicking on it.
Uh, firstly, in order to spawn that, we need a reference to our floating text game object as we know.
So we need a reference.
Uh, so let's go to the top of our class and let's grab the reference first.
So I'm going to make it somewhere over here.
So let's make a public.
And that floating text game object itself a whole game object.
So we need to type this game object because that itself a whole game object.
and we can call this one to something like floating text game object or prefab.
So let's call it prefab instead.
And now let's close that up with semicolon.
And make sure once you move to unity drag the reference in this current corresponding slot.
And now let's go over to take damage function basically where we're taking damage.
So we're taking damage over here as you can see.
So after taking damage we also want to spawn that floating text.
So to spawn we do know we're going to use this instantiate function and make first bracket.
So as a first argument it's going to take the game object that you want to spawn.
So in our case that floating text game object or that prefab.
So we're going to pass as a first argument floating text prefab comma.
Second thing it's going to take the position.
So we're going to pass transform dot position.
It basically means the center position of our enemy.
And comma third argument is going to take the rotation.
So we're going to pass quaternion dot identity basically means no rotation.
And let's close that off up with semicolon.
And now we're going to press down Ctrl S to save our script.
And let's move to unity.
So uh, so once this anime GameObject takes damage you will see that floating text just gonna spawn.
And we do know it's gonna be a random number between 1 and 1 zero one.
So now we need to drag the reference.
And we can see underneath our anime script floating text prefab just pop up and it says none game object
basically empty.
So what we need to do it over here.
We basically need to drag the floating text prefab game object here.
So let's go to project App assets and then prefabs folder basically where we drag it.
And I have the floating text game object over here.
So I'm going to select that and drag it over to this corresponding slot.
Now we can see we have the floating tags.
As you can see underneath your anime script.
And let's click on this play button by now and let's have a look.
So once this anime game object is gonna take damage, that floating text just that's going to spawn
right away, and it's just going to be a random number between 1 and 1 zero one.
So let's have a look on this.
So I'm going to attack on this enemy.
As you can see it's became 100 then six and then 58.
And we can just get out of play mode.
And we can click on this play button to play our game.
And we can take a look on that one more time, basically if it's working fine or not.
So we're going to take a look on that.
So let's click basically shoot on this enemy game object.
And we can see 1441 and 20.
So we can see a random number.
So adding floating text makes our game a lot of cool.
So I'm going to get out of play mode.
So you can just use that same principle in order to anything that you would like to go with.
And you can just choose a different color of that font as well before you spawn it.
You can do that.
Uh, I hope you get the idea that how you can do this.