Unity3d blood strip

Source: Internet
Author: User


The Avatar and blood strip are generated in the 3d world, so there is a real relationship between vertical depth and occlusion. If you don't talk much about it, see how I implement it.


Step 1: Create an avatar and a blood strip in the UI Root.

The procedure is basically the same as that described in the previous article about how to create a blood strip. I will cut a picture below.


It is HeroPanel above. It is very simple. Create a main panel HeroPanel and create a Slider in it to show the blood bar, and then create a sprite to show the Avatar, here I also have a sprite that is the background frame of the Avatar, and then adjust the position and the color of the blood strip. I don't know how to read the article about the blood strip in front of my avatar. It looks like this:



Step 2: Make the Avatar and blood strip into prefab.

Drag the created HeroPanel to the Resources folder of the Project (Project-> Assets-> Resources), and create one without the Resources folder for future loading. In this way, the prefab of HeroPanel is generated, and then the prefab under the UI Root can be deleted.


Step 3: Enter the program.

After the above preparation work is OK, it is the programming stage. Each of my roles has an Npc script, and the prefab of HeroPanel is loaded in the Start of the Npc script.

[Csharp]View plaincopy
  1. GameObject heroPanel = Resources. Load ("HeroPanel") as GameObject;
  2. _ HeroPanel = Instantiate (heroPanel, transform. position, transform. rotation) as GameObject;
  3. _ HeroPanel. transform. localScale = new Vector3 (0.006f, 0.006f, 0.006f );
  4. _ BloodSlider = _ heroPanel. GetComponentInChildren <UISlider> ();
  5. _ HeroHeight = gameObject. collider. bounds. size. y;

The above code is very simple. First, load the prefab of HeroPanel, then generate the instance _ heroPanel, and then adjust the size. This must be adjusted to the desired size. Otherwise, you can't bear to look directly at it, then obtain the UISlider script and assign it to the variable _ bloodSlider, which will be obtained later. If you want to change the Avatar image, you can click here. I will not change it. Finally, obtain the height of the role, use the collision box to obtain it, and use the coordinates calculated in the subsequent Update.

Then the Update part is:

[Csharp]View plaincopy
  1. Vector3 pos = new Vector3 (transform. position. x, transform. position. y + _ heroHeight + HEAD_OFFSET, transform. position. z );
  2. _ HeroPanel. transform. position = pos;
  3. _ HeroPanel. transform. rotation = Camera. main. transform. rotation;

In the Update, You need to obtain the coordinates of the role at any time. The most important thing is the y coordinate. Because you want to place the coordinates on the top of the role, the previous _ heroHeight, A HEAD_OFFSET is added later, which is the offset. I am 1.0, and then assign the coordinates to _ heroPanel. In this way, the blood bar of the Avatar will follow the role, but don't forget, it is the orientation of the camera. You need to keep this panel at all times. This is the role of the third code.

Now you have implemented the basic functions. It's easy. The rest is the slider of the blood strip. This is the same as the usage of the article I introduced earlier, here, the value of _ bloodSlider is changed, which is the percentage. I will not introduce it here. Haha, close the job.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.