Next
will be creating the HUD so to start there’s a 2D button located at the top of
the scene view which if you click on will change the view to that of 2D which
is helpful for setting up the HUD. So, what we want to do is create a canvas
which can be done by going the game object tab mouse over UI and clicking on
canvas which will then place a canvas in the hierarchy after which rename it
from canvas to HUDCanvas (heads up display). Then add a component called Canvas
group which allows UI elements to have an alpha and whether their interactable,
then uncheck the Block raycasts box.
Then right click on HUDCanvas in the hierarchy tab and click create empty which will create a child and rename the gameobject to HealthUI, the benefits to doing it this way instead of creating an empty then linking it to the HUDCanvas is that if you create the child directly unity knows that your trying to add a new HUD element and gives it the rect transform instead of the normal transform. Now to make sure that the icon for health is located in the bottom left of the screen click on the icon that looks like a square target in the rect transform which will then display a tab named anchor pre-sets in which holding shift sets the pivot location and alt sets the position, while holding those two keys click on the lower left pre-set which should then in the scene view show a box in the lower left after that set the width to 75 and height to 60.
Then right click on HealthUI in the hierarchy and go down to UI and click on image which is a way to get a texture or sprite to be displayed on the HUD, rename it from image to whatever you want in my case Heart because I’m using a heart to represent my health you could always use a cross or whatever. After that in the Hearts rect transform make sure the positions are set to 0 and change the width and height to 30 which should then display a white square in the box in the scene view.
Next on the image component use the circle select next to the source image tab and choose what image you want displayed in my case Heart. Now its time to add in a slider for the health to do this right click on HealthUI and mouse over UI and select slider and rename it HealthSlider then in the HealthSliders rect transform change the position X=95, y=0 and z=0 also since were using this slider in a non-interactable manner we can delete the handle Slide Area from the hierarchy and the slider should be displayed in the HUD in the scene view.
Then in the slider component set the transition to none as well as setting the max value to 100 which will be equal to the players starting health and change the value slider to 100 also. Next to make sure you actually notice when the player is taking damage reselect HealthUI in the hierarchy and attach another UI image then rename it DamageImage and to make sure it covers the entire screen using the anchor pre-sets, select DamageImage and drag it up and drop it on top of HUDCanvas after which go into the rect transform and in the anchor pre-sets select the pre-set on the lower right which should then give a white image covering the canvas in the scene view and to make sure it doesn’t completely block out the screen later we need to change the alpha to 0 which you can do by clicking on the colour block and in the slider listed A dragging it to 0 the colour will be handled by a script later.
Then right click on HUDCanvas in the hierarchy tab and click create empty which will create a child and rename the gameobject to HealthUI, the benefits to doing it this way instead of creating an empty then linking it to the HUDCanvas is that if you create the child directly unity knows that your trying to add a new HUD element and gives it the rect transform instead of the normal transform. Now to make sure that the icon for health is located in the bottom left of the screen click on the icon that looks like a square target in the rect transform which will then display a tab named anchor pre-sets in which holding shift sets the pivot location and alt sets the position, while holding those two keys click on the lower left pre-set which should then in the scene view show a box in the lower left after that set the width to 75 and height to 60.
Then right click on HealthUI in the hierarchy and go down to UI and click on image which is a way to get a texture or sprite to be displayed on the HUD, rename it from image to whatever you want in my case Heart because I’m using a heart to represent my health you could always use a cross or whatever. After that in the Hearts rect transform make sure the positions are set to 0 and change the width and height to 30 which should then display a white square in the box in the scene view.
Next on the image component use the circle select next to the source image tab and choose what image you want displayed in my case Heart. Now its time to add in a slider for the health to do this right click on HealthUI and mouse over UI and select slider and rename it HealthSlider then in the HealthSliders rect transform change the position X=95, y=0 and z=0 also since were using this slider in a non-interactable manner we can delete the handle Slide Area from the hierarchy and the slider should be displayed in the HUD in the scene view.
Then in the slider component set the transition to none as well as setting the max value to 100 which will be equal to the players starting health and change the value slider to 100 also. Next to make sure you actually notice when the player is taking damage reselect HealthUI in the hierarchy and attach another UI image then rename it DamageImage and to make sure it covers the entire screen using the anchor pre-sets, select DamageImage and drag it up and drop it on top of HUDCanvas after which go into the rect transform and in the anchor pre-sets select the pre-set on the lower right which should then give a white image covering the canvas in the scene view and to make sure it doesn’t completely block out the screen later we need to change the alpha to 0 which you can do by clicking on the colour block and in the slider listed A dragging it to 0 the colour will be handled by a script later.
Comments
Post a Comment