[Unity3d] Unity3d Game development Damage Value display

Source: Internet
Author: User

Hello everyone, I am Qin Yuanpei, welcome everyone to pay attention to my blog, my blog address is Blog.csdn.net/qinyuanpei. As we all know, the most important part of RPG game planning is numerical planning. Numerical planning is a concept of game balance, which is a very esoteric concept.

The game is in a sense a combination of multiple choices, so assuming a game loses its balance, it will reduce the choice and affect the fun of the game.

Numerical planners usually need to adjust the relevant values of the entire game system to a suitable range according to certain rules and methods. To make sure the player has a lot of other options to enhance the gameplay. Numerical planning is a concept that the domestic game industry has extended from foreign game industry in recent years. Bloggers feel that we are learning game design in the process, in addition to focusing on technical implementation, but also to the game design in the relevant theories have some understanding. About the specific content of the numerical planning, we can understand from here: http://baike.baidu.com/view/1756429.htm.

Well, here's what we started today: Unity3d Game development Damage value display. What I see is a game "blood Yuqian: The Mirage", which bloggers like very much. The game was divorced from the previous two-part turn-based model. In order to clear the act of cross-border mode again into the player's vision, but believe that only the people who have really played this game will know that this is a real RPG game. Strange, unique picture style, Cologne style martial arts narrative style for this evaluation brought unlimited vitality. and "Rain Blood" series is a few in recent years out of the country, famous foreign games.

The game is a very gorgeous, and a refreshing blow to the people can not stop.

As an PRG game, the most important in the battle is the two-side health control, so in combat real-time display of the player's damage to the enemy value is very important. "Chinese Paladin five prequel" is added in the Battle of the injury statistics. Easy for players to grasp the battlefield situation. Well, speaking so much, let's continue our Unity3d learning journey, in the following article we will use the Ongui () method in Unity3d to achieve the display of damage values.


Since it is based on the Ongui () method, I believe you must be very familiar with it. So we give the code directly:

Using unityengine;using System.collections;public class Damagepopup:monobehaviour {//target location private Vector3 mtarget;// Screen coordinates private VECTOR3 mscreen;//damage value public int value;//text width public float contentwidth=100;//text height public float Contentheight=50;//gui coordinates private VECTOR2 mpoint;//destroy time public float freetime=1.5f;void Start () {//Get target location mtarget= transform.position;//Gets the screen coordinates mscreen= Camera.main.WorldToScreenPoint (mtarget);//convert screen coordinates to GUI coordinates mpoint=new VECTOR2 ( MSCREEN.X,SCREEN.HEIGHT-MSCREEN.Y);//Turn on your own initiative to destroy thread Startcoroutine ("Free");} void Update () {//causes the text to have an offset transform in the vertical direction of the mountain. Translate (vector3.up * 0.5F * time.deltatime);//COMPUTE coordinates again mtarget=transform.position;//get screen coordinates mscreen= Camera.main.WorldToScreenPoint (mtarget);//convert screen coordinates to GUI coordinates mpoint=new VECTOR2 (MSCREEN.X,SCREEN.HEIGHT-MSCREEN.Y);} void Ongui () {//ensures that the target is drawn in front of the camera if (mscreen.z>0) {//internally using GUI coordinates to draw the GUI. Label (New Rect (Mpoint.x,mpoint.y,contentwidth,contentheight), value.tostring ());}} IEnumerator free () {yield return new Waitforseconds (FreeTime);D Estroy (This.gameobject);}} 
In the above code, we need to grasp the following points:

1, according to the transform component obtains the position coordinates, transforms this coordinates to the screen coordinates and the GUI coordinates.

2, the common four kinds of coordinate system in Unity3d:

A, world coordinates: the coordinates of the object in the scene. Obtained using Transform.position.

b, screen coordinates: defined in pixels. In the lower-left corner of the screen (0,0), the upper-right corner is (screen.width,screen.height), and the position of z is measured in the camera's world unit. such as input.mouseposition is the screen coordinates.

C, viewport coordinates: The viewport coordinates are standard and relative to the camera. The lower-left corner of the camera is (0,0), the upper-right corner is (the) point, and the position of z is measured in the camera's world unit.

D, GUI coordinates: The coordinate system is in the upper left corner of the screen (0. 0) point, lower right corner for (screen.width. Screen.height).

3, in the code we will first convert the world coordinates to screen coordinates. Convert to GUI coordinates again


All right. Here we bind this script to an empty game body. and made into a preset, in the following demo examples. We will use this preset.


, we want to implement when the character attacks the red capsule body. Shows the player's damage to the capsules in the game scene. How do you do it specifically? We were able to add a collider to the model and capsule body and tick istrigger to make it a trigger. We set their tag to player and enemy, respectively.

Next, write a script for enemy:

Using unityengine;using System.collections;public class Enemy:monobehaviour {public Gameobject popupdamage;void Ontriggerenter (Collider mcollider) {if (mcollider.gameobject.tag== "Player") {//Clone damage Eject component Gameobject mobject= ( Gameobject) instantiate (popupdamage,transform.position,quaternion.identity); mobject.getcomponent<damagepopup > (). Value=random.range (20,40);}}}
Here we set the player's damage to the enemy value of 20 to 40, the execution of the program, we will get the following results:


Because the blogger is using a non-exact collision, it causes the program to collide with the capsule as soon as it starts, and the Ontrigger () method is only able to capture collisions at the start of a collision. So this shows only one damage value at a time.

In theory only when the player attacks the capsule, it will trigger the display of damage value. It is only this problem that we can not pursue here, because we are concerned about the display of the damage value.

So far, the problem has been successfully conquered.

A friend may ask: Why do people's games show the damage effect is so dazzling, and your program can only display ordinary text it? For this question, we give two kinds of ideas here, left to everyone to explore AH.

The first method is to create a guiskin in the project and then add a Guistyle-type member variable Mstyle to the Damagepopup script. With this variable we can refer to the Guiskin created in the project.

In this way, we are able to define the overall GUI style. At this point, we will change the method in Ongui to:

Gui. Label (New Rect (Mpoint.x,mpoint.y,contentwidth,contentheight), value.tostring (), Mstyle);

So that we can achieve our own definition of the text effect. Another way is to use the map, that is, to prepare 0-9 of the digital image, and then we will be the value of the number of digits on each of the numbers are intercepted, according to the result of the interception of mapping, so the same can achieve their own definition of the effect. All right. That's what this is about today. Thank you for your attention to my blog, I hope you can enjoy today's content.


Daily motto: live gracefully, others won't crush you, and the strong inside is really powerful.




Like my blog Please remember my name: Qin Yuanpei, my blog address is Blog.csdn.net/qinyuanpei
Reprint please indicate the source. This article Qin Yuanpei, the source of this article: http://blog.csdn.net/qinyuanpei/article/details/27492689

[Unity3d] Unity3d Game development Damage Value display

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.