Unity has been learning for 10 days, and has not found any progress, really urgent. Yesterday, imitation of the official demo made a shooting game outline, which need to give each strange to make a blood bar.
Search for some, quite complicated, with Ngui or Ugui, plus a very long code ... But I found a simple one.
But that one put all the things together, not very good, I am here to clean up the separation.
Background:
Official Demo demon Shooter. Each of these monsters has a enemyhealth script, which has a monster's amount of blood, and then a takedamage () function to calculate the amount of blood after the damage.
Begin:
1. Making Pictures:
PS a slender red picture as the amount of blood;
2, make a material ball, named "Xuetiaomaterial", click the material ball, in the Inspector panel to add the red picture, it became the red material ball.
Inspector panel Shader Select Unlit/transparent
3. Create a cube and put it on the strange thing you need to add a blood bar to, cube as a strange sub-object. Drag into the shape of the paper film and drag it to the appropriate position.
4, click the cube, you are willing to change the name of the name. Add the 2 material ball to the cube and it will turn red. Check out the cube's collider, two Shdow. After all, the blood strips do not have to collide with shadows.
5, in your control of blood volume of the script add something, I was added in EnemyHealth.cs, as follows:
The Blood bar map is a new item, and the code is then re-assigned to the cube
Public Texture2d bloodbarred; // Blood bar map Public // cube representing the blood bar
In addition, there are:
void Start () { = transform. Findchild ("bloodbarcube"// get Blood Bar object New Texture2d (+// Create a new map to assign to the cube // Stretch the graph }
Also, to make the blood bar at the moment of the camera:
void Update () { Bloodbar.lookat (Camera.main.transform.position); // facing camera }
The following is a function of adding an updated blood bar:
Public voidUpdatebloodbar () {floatcunrrentred = Currenthealth * bloodbarred.width/startinghealth;//directly except, because two integer type get 0, so first multiply later except for(varx =0; x < bloodbarred.width; X + +) { //for each coordinate point for(vary =0; Y < Bloodbarred.height; y++) {//The loop executes the y-axis starting from 0, and the y-axis is smaller than the width of the blood bar . if(X <cunrrentred) bloodbarred.setpixel (x, y, color.red); //x is less than the length of the blood bar painted red, Elsebloodbarred.setpixel (x, y, Color.gray); //other parts painted black}} bloodbarred.apply (); //Apply the diagramBloodBar.gameObject.renderer.material.mainTexture = bloodbarred;//give the modified map to the blood Bar cube }
Where Currenthealth is the member variable in my Enemyhealth class, Startinghealth is also the total amount of blood.
You can then call this function in a function that changes the amount of blood. Thus updating the blood strips. If necessary, you can also pass the current amount of blood as a formal parameter.
6, drag the script into the corresponding object (need blood bar of the Strange), click on the strange, in the Inspector panel, find the script, drag the red picture into the bloodbarred, the Blood Bar Cube (the strange sub-object) dragged into the Bloodbar.
The operation is OK.
Thanks:
Http://www.manew.com/forum.php?mod=viewthread&tid=33694&page=1
Unity makes simple blood strips