Afrikaans
Akan
Albanian
Amharic
Arabic
Armenian
Azerbaijani
Basque
Belarusian
Bemba
Bengali
Bihari
Bosnian
Breton
Bulgarian
Cambodian
Catalan
Cebuano
Cherokee
Chichewa
Chinese (Simplified)
Chinese (Traditional)
Corsican
Croatian
Czech
Danish
Dutch
English
Esperanto
Estonian
Ewe
Faroese
Filipino
Finnish
French
Frisian
Ga
Galician
Georgian
German
Greek
Guarani
Gujarati
Haitian Creole
Hausa
Hawaiian
Hebrew
Hindi
Hmong
Hungarian
Icelandic
Igbo
Indonesian
Interlingua
Irish
Italian
Japanese
Javanese
Kannada
Kazakh
Kinyarwanda
Kirundi
Kongo
Korean
Krio (Sierra Leone)
Kurdish
Kurdish (Soranî)
Kyrgyz
Laothian
Latin
Latvian
Lingala
Lithuanian
Lozi
Luganda
Luo
Luxembourgish
Macedonian
Malagasy
Malay
Malayalam
Maltese
Maori
Marathi
Mauritian Creole
Moldavian
Mongolian
Myanmar (Burmese)
Montenegrin
Nepali
Nigerian Pidgin
Northern Sotho
Norwegian
Norwegian (Nynorsk)
Occitan
Oriya
Oromo
Pashto
Persian
Polish
Portuguese (Brazil)
Portuguese (Portugal)
Punjabi
Quechua
Romanian
Romansh
Runyakitara
Samoan
Scots Gaelic
Serbian
Serbo-Croatian
Sesotho
Setswana
Seychellois Creole
Shona
Sindhi
Sinhalese
Slovak
Slovenian
Somali
Spanish
Spanish (Latin American)
Sundanese
Swahili
Swedish
Tajik
Tamil
Tatar
Telugu
Thai
Tigrinya
Tonga
Tshiluba
Tumbuka
Turkish
Turkmen
Twi
Uighur
Ukrainian
Urdu
Uzbek
Vietnamese
Welsh
Wolof
Xhosa
Yiddish
Yoruba
Zulu
Do the first thing you need to think about whenever you create a new game on
About the jump into changing a gameobject with a script on his case we can I change the health bar with
With a script
First thing you need to think about it
Who needs a reference to this
Who's going to be changing this
Well
We had a reference to coins
In the player script
The new player script double click on their script
But we get the coins text
UI
Referenced here
So when I had a similar reference to the image component
So we can do is just type public
Image
Elf bar
And we can technical go into the start function here
Type health bar
Subtitled by -♪ online-courses.club ♪- We compress knowledge for you! https://t.me/joinchat/ailxpXoW3JVjYzQ1
Equals game object
Define what the name is the name is health bar with a space
Dad
Member
Because it's an image
Variable type
Winner deals getcomponent
Image again weird syntax year
But now it's going to find
Health bar game object
And then get the
Now we can actually adjust
Health bar
Another way of doing this is just to get rid of her entirely
Because there's only one player in the game
We just say hey player
How far
Is this
Dragon on a without of the
Now here's a thing
Weekly dinner health bar
What we just referenced was the image component
Well the image component
Doesn't actually have anything to do with whip
So why are we getting the image component
In that the rect transform
The reason why is because you can actually access the recttransform
Simply by having any other reference to a component
Ethical thing about recttransform in transformed bellies
So what we can do is actually just go like this
Update you I'll say I don't know the update function here in the new player script in Russia's rights
Code I just say what's your test here said health bar
With
2
100
The way that we do there is health bar that wrecked transferred is recttransform
I know this is a little confusing
But there is certain components
That come out of the box with unity
You can just reference whenever you want to
So even though health bar
Does an actually have a variable called recttransform
What is means if we're just going hey find something called recttransform
In that game are checked
Now that we have the recttransform value
We can actually just like that
And it's not actually with your height or even size
Kind of weird with red Transformers actually size Delta
Then you going to say equals
New
Vector
2
Yeah I just said it to you 100 and then
Whatever it's current y valuers or height
So again health bar that recttransform
And I just close down
With a semicolon
Richard set
How far to 100 units wide
So in theory it should end up looking like that
The moment we collect a coin
And this is just a test to see if work setting the width properly so it's a play and see if that happens
Here we go
Alright so now we know how to change the actual width of a bar
We're getting write an equation
So I know one thing
I know we need an original size
Brother health bar
We don't know how big
The bar is
When we start the game
Create a variable is going to be private because we're not really going to need to see it and the inspector and we definitely don't need a
From any other script in the game
Private private
Vector2
Health bar original size
What's this by default we created the variable but by the fault it's just
00009 why or
0 in width and 0 in the height
So we need to make sure that we find out what that original size is
So what we're going to do is going to say
Health bar or Ridge size = the health Bar number that's the image
Variable APR
Dart recttransform size Delta
The health bar original size this variable
Is matching
Finding out with the size delta of the red Transformers
So just so we can see it in the Inspector was just serializability quick
Remove this after we know for sure
How much is go ahead hip play and make sure
We can see what the health bar orig sizes
Ok so we're just updated because we put it in the start function so right when we started
It matched the value
816 in width and 29 in height
Where is perfect it's magic
Cinematic how small I make this
Pronounce 263
When we go to iPlayer on new player skript here when we got a new player script here by selecting player
The nearest that is still 816
That's because it's found the original value when we were in the start function
Mama hit play at SoundAbout value
But now
It's still has their value so it's remembering what the original value was
Now we can find out the proportion
Based on its original size to right now it's almost maybe 30%
Aboriginal
The way that you calculate that
Is simply buy some Division so have you learnt this in high school maybe you're learning this right now in high school or college
Have you forgotten all about this stuff and that's definitely ok we're going to do a little bit of a refresher here
So basically to find the proportion or the percentage
Original value
You just divide so if we write some pseudocode here we go health bar that recttransform
That size Delta
X /
The original value
What is that that's health bar
Norwich
Size
X
What's go ahead in just for a quick maths refresher
ASDA Bangor sell
Debug.log
This actually
Into the debug window
Tell we can write a Little Liars train here
Health bar percentage is
Whenever this number is so it's
With
÷ the original with
The Crown which is 100
The original it was 100
What are percentage
Well this is gonna give us a decimal not a percentage but we can jump in unity and take a look at it play here
Collect something
Alright so looks like the health bar percentage is .12
What does that mean that swell 12%
Number with percentages you just need a x x 100
And I should give us an accurate
Percentage
So currently it's 12% of its original value
That's perfect so that's going to be using
When scaling is health bar
So now we have a general understanding of not only how to change the size or the width
The health bar
We also have a general understanding of how percentages work and how we can find out what the percentage of the health bar is
I'm just going to go ahead and comment all this out for now
To our going to do is actually just going to the collectible script
And we need to start setting some conditions
Michael Dunn appear
Do certain things to various variables
So I know that if it's a coin
I want to add
To the coins collected variable
And I know that it is a health
Item type
I want to add
To another line Sonu player health
Classical or mine is equals what in this case it can be plus equals
Ok
Well
We want to add to a coins in their health
Or let's say a coin collectible type
We actually need to make these conditions
Turn down here
Go ahead and comment out all of these conditions here that We Created earlier and we're just going to come in the mail
Because we actually can just copy and paste them
Copy all of this we don't really need it in our start function
That was just for a tutorial now actually going to put it inside of the ontriggerenter so weird paste all
And we have I'm a coin here
Remove all this debug stuff don't need this anymore that was just for the tutorial now actually making
Subtitled by -♪ online-courses.club ♪- We compress knowledge for you! https://t.me/joinchat/ailxpXoW3JVjYzQ1
So this is if I'm a coin then we want to add
Health
Bad health
NN regardless no matter who I am
I still want to update the UI
Just to get the
Variables to match the actual UI elements
And then just destroy the game object
Destroy the coin or the health object
Don't say that out
And let's see hear back in the Unity
So I know that I don't want to spend too much time creating graphics for health
So she just take this coin here
Call this
Health
Let's make it a bright red
There we go there some healthier for us
A little bit pink
And
What I want to do is actually just drag this to a prefabs
Right there
Creating original prefab
So now we have a health item
In a coin item
Well is this actually going to be havenshealth
Not yet let's go hen
Johnny dough's
And how we have this collectible pony here on her health game object
Let's be sure we said it to health
Alright so if we go back into your script here
You can see here that it says if I am type is coin flip the coin
If item type is health
Well I didn't type is said to help
Therefore it should increase our health value
So nothing's going to happen to our national health bar
Just yet
All that's going to happen is going to add some health
It's going to play
Take a look at our health value here
100 if I collect this
But now it's 101
Well
We really don't want to go over 101 do we
So what we're going to do is just create a simple condition inside of the condition we've only written here
Wheeler Dealers just go if
New player
Health is smaller than a value of 100
Then
Working as an elf
That's great
The now
Go back into unity sofa health is currently 100
Play
We should be able to collect the health
But it's not actually an increase the value
Pretty much Max Tell right
So let's go ahead and set our health by default
250 just for testing purposes
In here play
Now it's 51
Awesome that's perfect
So now that we've actually got the ability to collect these red orbs to get health
Now I want to make it actually reflected
In the percentages
Are the health bars with
Hi so our health
Variable
My actual player
Is it raining right we like to help and it goes up by 1 so from 50 to 51
Nothing happening with the actual
Health bar UI so we need to make that reflective of the actual value
Well you remember we have a function inside of a new player called update UI
The update UI is basically just going to say hey what's the coins value what's the health value what's RAM
Are you going to check for all these things every time you collect something and it's going to try and reflect it in the UI
So what's writing pseudocode here to help us better understand how I'm going to write this equation
Basically wanted to set
The health bar
With
To a percentage
Of its original value
Ok so we're going to do this is something like see here we need a multiplier
We need a x
The original health bar with
Do something like health bar Dad x x x a percentage of a value
Going to be x x something like .45 something like that so it shrinks down to half of its size
How we get that value
What's the apprentices here in try and figure out what this would be
Well it's going to be my current health
/ my Max health
Ok
So what is my maximum health value
When I actually have something like that
Swansea this what's a private integer
Call Max health
Let's make our maxhealth 100 ok
Our health is currently 100 as well that's ok
So now health / Max health is going to be some kind of a decimals of my health is it 50
50 / Max health is 50% or .5
So health bar that x x x 50%
Should kind of do the trick right
Well not exactly
Actually needs to be the original value to health bar or rage
Dad x
So what's the original size what was the original size of the health bar then we're going x x at present
So how do we write this in actual code
It would be something like health bar
Dad
Transform
Recttransform Meadows
Size Delta
Equals
New vector2
Ok here is where we do that equation
Health bar rage size.xl
X x printezis health /
Max health
I know this is confusing
And it seems like I'm smart and I know exactly what I'm doing here
But I actually had it sorted take a breather and puzzle little bit while doing this tutorial evening
Really think about what is equation is
Ok so we've got some squiggly lines looks like we're not actually
That's right we need to put a y value in here what's the weather you that's the height we want it to be identical
Just help bar directions form delta 0.1
Semicolon
So why on earth is this equation
Let's just break it down as simple as we can guys
So was setting the size of our health bar
That's called size Delta
And that is a variable of the recttransform component
We're going to set that to a new vector to
Who is X and Y or in this case with or height
Premier Inn why perimeter ok
What about the experiment are really quick
This is the actual whip
So we're going to set the width to be the original size
Times that percentage
Bet calculator
From my current health
÷ maxhealth
Search my credit is 50
100
That's .5
So it's the original size of the bar
In the wet
X x 0.5
Why you here is the height
When are we doing you're saying don't change it keep it exactly what it is
How far Direct transform that size delta that's why
So nothing is going to be tweet here
Ok so I think they will do it let's test this year
Go hip playing see what happens
Nothing should happen with a health bar yet
Currently with our current script is if we collect a health item so that's right
Oh my goodness
So it is 0 or 21 take a look
Exactly 0
Let's try and diagnose why that happened
Going to happen all out with you guys when you're making your game
White just happened
To my line of code
So I talked about debugging
These kinds of errors
OK well let's go ahead in remove all of this comment stuff we don't need that
Let's go ahead in figure out what is going on here some of you might actually see exactly
Going on in if you do bravo to you
But I'm going to go step-by-step in figure out what's going on here
Well what are health borough Ridge size
What is the current value
Let's go home hip play and make sure we get that value correct when I want to do is go through each portion of my
To make sure everything is the correct value
Securly it's 8:16
Ok
Let's see here
So that's 816
X
Times our health
Which is 50
÷ or Max health
1 x 100
Just go ahead and do a little bit of a debug.log
Health
Percentage
Is
Plus animals copy
Is value here
Ok so we're going to find out what the health percentage is
What's going on here play
0
I think I know what's going on
What we need to do
I'm actually glad it happened because I need to tell you about
Define dividing one integer
Another integer
It's going to output 0
And the reason why is because
These are floats these are integers
How we convert them to a float
Actually I got to do is just type float in front of One
Inflow in front of another integer so now they seem to do is say what's the healthy to do
Convert that to a float
What's the max out integer into a float
Enniskillen divider 2
So let's see if he bugs
The proper calculation
Awesome OK now it's .51 time to do is go back in the net script here and just had the
Float / float
Then we can delete the debug here
And it should
Set our health bar
To be about 51%
Original value
Artem ok sorry how far is adjusting properly
The other thing we got to do is your guys as well when we have play
You notice that a health is 50 here
The Mark self is 100 but why is the bar none A50 it should be that way the moment
Start the game
Well
You probably thought of it before I did are we going to do is make this update UI
Cabin
In the start function as well
So what you going to do it right here update you why
So the moment that unity starts the game then we hit play
It's going to burst
Variables
And just like a speed reader he's going to jump to The Voice start here
Restarting my game
I need to first set the health bar original size variable to match the health bar
And then I'm going to go ahead and update my uy meaning I'm going to take a look at all these calculations here
Make the appropriate adjustments
That's it saving
Tiny unity
How far
The moment we have play the moment the death start function fires
Is it raining down
Perfect awesome
I love it and you can see it to iterate it there so let's go ahead in ads and health
To the game
Sad one here
Maybe some in the sky right here
Periods and healthy and here
Is always really fine and collectables collectables and just going to make your game so fun and we make a little
Play two player on a jump in that direction
Really really cold here so let's go ahead in
Play
Awesome
Well it looks like the health bar
Isn't updating and that's a little strange will take a look and see what's going on
For some reason
Those prefabs that we just added are not adding health to the player
Why is that
I see
Looks like this is a great example
I've just double checking your prefabs
Looks like if we double click on her health prefab
It looks like the item type wasn't set correctly for a health prefab
Let's be sure we said that to health
Go back to the scene
Just double checking m Oliver health
Ok so the item type is set correctly
How much is drag these
Angela one as well
Just having organised
Now when we have play
You can scale up a game window just a little bit to see the iteration of the health bar
Alright good it's going outputs go
Over here
Good ass
Slowly iterating
What's your notice better coins are no longer updating now
What is going on with ticklish here it's go back in your script in
Are you out this bug as well
I'm keeping these bags in this tutorial guys because I want you guys to understand slowly
How to fix bugs
So if you're going or collectible script here if item type is coin or going to add Torpoint
And then we're going to update the UI
Update UI function
Looks like that's probably set up so let's go ahead and jump into unity
BBC what's going on it's like one of these coins here I'll take a scripture ok
Hurricanes
Oh my goodness
Item type is it the ML I have no idea why they happened but that's ok that's why we use
Prefabs
Double click on a coin prefab
In a project folder
Change the item type 2 coin
Now it's a van hopefully everything is updating accordingly
Health
The coin
Yeah ok
Item types are working fine
Perfect
Awesome
There's a quick recap guys
Your variables
For health and ammo
Coins
All that stuff it's not stored in the UI DUI is simply reflect
The variables
Data stored somewhere else
In this case for storing those variables in a
Assistant script that means a script that's going to be running throughout the entire game
In our case it's the new player this player is always going to be in the game so were storing
Inventory variables like health my coins collected
Does are all stored in the Script
In the UI elements
Reflects those
Using a simple function called update UI
We fire this function or call this function
From other scripts so we can call that function from the collectible script and see it's been cold here
Or if we had an enemy scripting when the enemy hit the player
In the health decreased
You just buy that functions again
That's why we have a single function called update by inside of one
Class and that's the new player class
Because I want to give a fire that from all kinds of other script a collectible script
An enemy script maybe a boss script
Maybe there's some traps in the game that can hurt you
Or maybe there's some characters in the game that will give you 3000 coins
No matter what or who is changing the variables of your inventory
All we got to do is just updating you are
With this simple coming and here new player that update UI
And it's always going to go back to this function here
Check the variables check your inventory
And then update the UI accordingly
So that is how you I generally works and games you have one big function here that can update the UI
Based on your inventory so that a ton about the classes can use that single function
Instead of writing of that function inside of every single class
That wants to update the UI
This doesn't make sense don't worry these kind of principles that can be used over and over and over and kind of research
As we work through this tutorial to create our 2D game
Can't find what you're looking for?
Get subtitles in any language from opensubtitles.com, and translate them here.