Blood bar creation and movement

Blood bar creation and movement

……

……

Health bars are very common game items in the game.

I record how I make the blood bar and how the blood bar moves with the character.

first.

To dissect the blood bar.

Generally speaking.

The character's health bars are all green.

And once the character loses blood, the part that loses blood will turn black.

For example:

 

so.

Here you need to use the Image in the UI.

The green part is an Image.

And the black part is also an Image.

What the script logic needs to do is to control the scaling of the green Image when the character loses blood.

Some might wonder.

If it is zoomed, wouldn't the green part shrink directly? Why did it shrink to the left?

Pivot has to be mentioned here.

 

As long as the Pivot is set to the coordinates shown in the above figure, the blood bar can be reduced to the left in proportion, and it can be easily controlled in the code.

As shown in the figure below, if the HP of the character is less than or equal to 0, the game will fail directly, and the game failure method will be called. And if the HP is greater than 0, first calculate the HP ratio, and then call localScale to scale down x to realize the control of the blood bar.

 

……

……

So how to make the blood bar move with the character?

As shown in FIG.

As you can see, I called the main camera and converted a HP_pos.position into screen coordinates.

So, what is this HP_pos.position? In fact, this is actually the coordinates of an empty object. Very simple, for example, I need to hang the blood bar on the character's head, then the position of my empty object is on the character's head (this empty object is a sub-object of the character, so that it can move with the character) ,That's it.

Then the following code, hp_Panel is the blood bar (the black Image of the blood bar is the parent object, and the green Image is the child object, here is the moving parent object hp_Panel), move the blood bar to the 3D position set by my first code Represents screen coordinates. That is, move the health bar to the character's head.

And, since it is executed in Update, the blood bar will change as the character changes.

In this way, a simple logic that the blood bar moves with the character is realized.

……

……

Guess you like

Origin blog.csdn.net/oyqho/article/details/129942439