Unity3D game development-damage value display, unity3d Game Development

Source: Internet
Author: User

Unity3D game development-damage value display, unity3d Game Development

As we all know, numerical planning is the most important part in RPG Game planning. Numerical planning is a concept about game balancing. It is a very profound concept. In a sense, a game is a combination of multiple options. Therefore, if a game loses its balance, it will reduce the choice and affect the game's pleasure. Numerical planners usually need to adjust the related values of the entire game system to a suitable range according to certain rules and methods to ensure that players can have more options to enhance game playability. Numerical planning is a concept derived from the foreign gaming industry from the domestic gaming industry in recent years. In the process of learning game design, apart from focusing on technical implementation, we should also have some knowledge about the relevant theories in game design. For details about numerical planning, refer to http://baike.baidu.com/view/1756429.htm.

Now let's start today's content: the damage value of Unity3D game development ., It is a game called "blood rain: A mirage". Although this game is out of the previous two forms of integration, it enters the player's field of view again in the form of an ACT Horizontal Pass, however, I believe that only those who have really played this game know that this is a ground-based RPG Game. The strange and unique image style and the ancient martial arts Narration Style have brought infinite vitality to this evaluation. In recent years, the yuxue series is a rare game that has gained a great reputation abroad. This game is gorgeous in all ways, and it is a great blow. As a PRG game, the most important thing in the battle is the comparison of the life values of both sides of the enemy and me. Therefore, it is very important to show the damage that players cause to the enemy in real time in the battle. Legend of the legend. Now, let's continue with our Unity3D learning journey. In the following article, we will use the OnGUI () method in Unity3D to display the damage value.

Since it is based on the OnGUI () method, I believe everyone will be familiar with it, so let's give the code directly:

[Csharp] view plaincopyprint?
  1. Using UnityEngine;
  2. Using System. Collections;
  3. Public class DamagePopup: MonoBehaviour {
  4. // Target location
  5. Private Vector3 mTarget;
  6. // Screen coordinates
  7. Private Vector3 mScreen;
  8. // Damage Value
  9. Public int Value;
  10. // Text width
  11. Public float ContentWidth = 100;
  12. // Text height
  13. Public float ContentHeight = 50;
  14. // GUI coordinates
  15. Private Vector2 mPoint;
  16. // Destruction time
  17. Public float FreeTime = 1.5F;
  18. Void Start ()
  19. {
  20. // Obtain the target location
  21. MTarget = transform. position;
  22. // Obtain screen coordinates
  23. MScreen = Camera. main. WorldToScreenPoint (mTarget );
  24. // Convert screen coordinates to GUI coordinates
  25. MPoint = new Vector2 (mScreen. x, Screen. height-mScreen.y );
  26. // Enable Automatic Thread destruction
  27. StartCoroutine ("Free ");
  28. }
  29. Void Update ()
  30. {
  31. // Make the text produce an offset in the vertical direction.
  32. Transform. Translate (Vector3.up * 0.5F * Time. deltaTime );
  33. // Recalculate coordinates
  34. MTarget = transform. position;
  35. // Obtain screen coordinates
  36. MScreen = Camera. main. WorldToScreenPoint (mTarget );
  37. // Convert screen coordinates to GUI coordinates
  38. MPoint = new Vector2 (mScreen. x, Screen. height-mScreen.y );
  39. }
  40. Void OnGUI ()
  41. {
  42. // Ensure that the target is in front of the camera
  43. If (mScreen. z> 0)
  44. {
  45. // Use GUI coordinates internally to draw
  46. GUI. Label (new Rect (mPoint. x, mPoint. y, ContentWidth, ContentHeight), Value. ToString ());
  47. }
  48. }
  49. IEnumerator Free ()
  50. {
  51. Yield return new WaitForSeconds (FreeTime );
  52. Destroy (this. gameObject );
  53. }
  54. }
In the above Code, we need to grasp the following points:

1. Obtain the location coordinates based on the Transform component and convert the coordinates into screen coordinates and GUI coordinates.

2. Four Common coordinate systems in Unity3D:

A. World coordinates: Coordinates of objects in a scenario, obtained using transform. position.

B. Screen coordinates: defined in pixels, the lower left corner of the Screen is (0, 0), and the upper right corner is (Screen. width, Screen. height), the position of Z is measured by the unit of the camera's world. For example, Input. mousePosition is the screen coordinate.

C. View coordinate: The view coordinate is standard and relative to the camera. The lower-left corner of the camera is (), and the upper-right corner is (). The Z position is measured in the unit of the camera's world.

D. GUI coordinate: the coordinate system is based on the (0, 0) Point in the upper left corner of the Screen and (Screen. width, Screen. height) in the lower right corner ).

3. In the code, we first convert world coordinates into screen coordinates, and then convert them into GUI coordinates.


Now, we will bind this script to an empty game body and create a preset. In the following example, we will use this preset.

When the role attacks the red capsule, the damage caused by the player to the capsule is displayed in the game scenario. How can this problem be solved? We can add a collision tool to the model and capsule, and check IsTrigger to make it a trigger. We set their tags to Player and Enemy respectively. Next, write a script for Enemy:

[Csharp] view plaincopyprint?
  1. Using UnityEngine;
  2. Using System. Collections;
  3. Public class Enemy: MonoBehaviour {
  4. Public GameObject PopupDamage;
  5. Void OnTriggerEnter (Collider mCollider)
  6. {
  7. If (mCollider. gameObject. tag = "Player ")
  8. {
  9. // Clone the injury pop-up component
  10. GameObject mObject = (GameObject) Instantiate (PopupDamage, transform. position, Quaternion. identity );
  11. MObject. GetComponent <DamagePopup> (). Value = Random. Range (20, 40 );
  12. }
  13. }
  14. }
Here we set the damage to the enemy to 20 to 40. Run the program and we will get the following results:

Because the exact collision is used here, the role of the program is collided with the capsule body at the beginning, while the OnTrigger () method can only capture the collision at the beginning of the collision, therefore, only one damage value is displayed. Theoretically, only when a player attacks the capsule will the damage value display be triggered. However, we can ignore this issue because we are concerned with the display of the Damage Value. So far, this problem has been successfully solved.

Some may ask: why is the damage effect displayed in a game so dazzling, while your program can only display plain text? We provide two ideas for you to explore this question. The first method is to create a GUISkin in the project, and then add a member variable of the GUIStyle type mStyle in the DamagePopup script. With this variable, We can reference the GUISkin created in the project. In this way, we can define the overall GUI style. In this case, modify the OnGUI method:

[Csharp] view plaincopyprint?
  1. GUI. Label (new Rect (mPoint. x, mPoint. y, ContentWidth, ContentHeight), Value. ToString (), mStyle); More exciting unity3d technical articles please click http://www.gopedu.com/article

In this way, we can implement custom text effects. The second method is to use the texture, that is, first prepare a 0-9 digital image, then we extract the numbers on each digit of the Value, and draw the Texture Based on the screenshot result, in this way, you can achieve custom results. Well, today's content is like this.


After Unity3D games are published on android phones, Chinese characters cannot be displayed. How can this problem be solved? Source code

I remember there is a solution for inserting advertisements in the U bar.

Can I use RPG MAKER XP to display the effect of the damage value when the damage is caused? What should we do? High score (append)

Meow ~ On the same floor, I want to score ......
Use "Damage Value beautification script"
The image used is as follows:
(This is your own image, not the original image)
Name the image "damage" in \ Graphics \ Pictures.
#===================================================== ========================================================
# This script is from www.66RPG.com. Keep this information for use and reprinting.
#===================================================== ========================================================
#===================================================== ============================================
# Damage beautification v1.0 by SailCat
#===================================================== ============================================
# Script instructions:
# 1.copy damage.png to the Graphics/Pictures directory of your game during use
# 2.Damage.png file format:
#180x96
# (0, 0)-(179, 31) indicates the number of damage values. Each number is 18 in width and 32 in height.
# (0, 32)-(179, 63) is the number table of the reply value (negative damage value), where each number is 18 in width and 32 in height.
# (0, 64)-(89, 95) Pictures marked with a single blow, with a length of 90x32
# (90, 64)-(179, 95) indicates a picture without hitting the mark, with a length and width of 90x32
#===================================================== ============================================
Module RPG
Class Sprite <: Sprite
#--------------------------------------------------------------------------
# ● Damage Value profiling
#--------------------------------------------------------------------------
Def damage (value, critical)
# Releasing damage
Dispose_damage
# If the damage value is a numerical value
If value. is_a? (Numeric)
# Convert absolute values to strings
Damage_string = value. abs. to_s
Else
# Convert to string
Damage_string = value. to_s
End
# Initialize bitmap
Bitmap = Bitmap. new (162, 64)
Bitmap. font. name = "Arial Black"
Bitmap. font. siz ...... remaining full text>


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.