Unity3D game development: Skill Training and unity3d Game Development

Source: Internet
Author: User
Tags dota

Unity3D game development: Skill Training and unity3d Game Development

Next we will start today's Unity3D skills training. We have learned the Unity3D training goal: to allow U3D beginners to quickly master U3D technology, create and modify materials on their own, and develop small-scale 2D and 3D games and web games independently.

Today, we are going to make a very important component in the game: Blood strip. What is a blood strip? Blood is a manifestation of the value of life, just like in the TV series "Legend of the legend, tiantie said that the same amount of life needs to be changed, so the ending in the TV series has become not much time for the stationary to stay in the world, and the snow sees him sitting at the door of Xin 'an, the sky suddenly fell into the snow. After talking about the game, we continue to return to Unity3D. Today we want to develop a blood bar component based on 2D textures. Before the official start, let's first understand the principle: the blood strip consists of two equal-size textures, and the texture above is the volume texture we can see, the next texture is our background texture. By changing the width of the blood volume texture, we can achieve the effect of blood strip. Okay. Let's take a look at today's content after we finish working on the principles!

First, we prepare two pasters of different colors,

More highlights please go to http://www.gopedu.com/


Next, we open Unity3D to create a new project. We first create two GUITexture objects in the scene. We name these two GUITexture objects HPBackward and HPForward respectively. There is a very important attribute PixelInset in GUITexture, which is a Rect value used to describe the position, width, and height in the upper left corner of the GUITexture object. Here we use the default coordinate value to change the width to 64 and the height to 5. The settings of the two pasters must be consistent. :

Here we need to talk about PixelInset. Because GUITexture is located by two-dimensional screen coordinates, that is, the lower left corner is (), and the upper right corner is ), therefore, we cannot directly use 3D coordinates to change the location of the GUITexture object. In Unity, there is a WorldToScreenPoint () method that can convert 3D coordinates into two to the left. So our idea is to obtain the location of the target object and convert it into two-dimensional coordinates, then assign the value to the GUITexture object. Well, based on this idea, we can write the following script:

[Csharp] view plaincopyprint?
  1. Using UnityEngine;
  2. Using System. Collections;
  3. Public class Texture2DHP: MonoBehaviour
  4. {
  5. // Foreground texture
  6. Public Texture ForwardTexture;
  7. // Background texture
  8. Public Texture BackwardTexture;
  9. // Target object
  10. Public Transform Target;
  11. // Horizontal offset
  12. Public float OffSetX = 0.05F;
  13. // Vertical offset
  14. Public float OffSetY = 0.05F;
  15. // Maximum blood volume
  16. Public int MaxHP = 100;
  17. // Current blood volume
  18. Public int HP = 100;
  19. // Blood strip width
  20. Public int mWidth = 64;
  21. // Cord height
  22. Public int mHeight = 5;
  23. // Foreground
  24. Private Transform Forward;
  25. // Background
  26. Private Transform Backward;
  27. Void Start ()
  28. {
  29. // Obtain the foreground and background
  30. Forward = transform. Find ("HPForward ");
  31. Backward = transform. Find ("HPBackward ");
  32. // Set foreground and background textures
  33. Forward. guiTexture. texture = ForwardTexture;
  34. Backward. guiTexture. texture = BackwardTexture;
  35. // Initialize the blood bar Location Based on the target object
  36. UpdateLocation (Target, OffSetX, OffSetY );
  37. }
  38. Void Update ()
  39. {
  40. UpdateLocation (Target, OffSetX, OffSetY );
  41. UpdateHP (HP );
  42. }
  43. // Update location
  44. Private void UpdateLocation (Transform mTransform, float mOffSetX, float mOffSetY)
  45. {
  46. // Obtain the target object height
  47. Float mHight = Target. collider. bounds. size. y;
  48. Float mScale = Target. transform. localScale. y;
  49. MHight = mHight * mScale;
  50. // Convert 3D coordinates into 2D coordinates
  51. Vector3 mPos3d = new Vector3 (mTransform. position. x, mTransform. position. y + mHight, mTransform. position. z );
  52. Vector2 mPos2d = Camera. main. WorldToScreenPoint (mPos3d );
  53. // Update the texture position
  54. Forward. position = new Vector3 (mPos2d. x/Screen. width + mOffSetX, mPos2d. y/Screen. height + mOffSetY, 0 );
  55. Backward. position = new Vector3 (mPos2d. x/Screen. width + mOffSetX, mPos2d. y/Screen. height + mOffSetY, 0 );
  56. }
  57. // Update the blood volume
  58. Public void UpdateHP (int mValue)
  59. {
  60. If (mValue <0 | mValue> MaxHP)
  61. Return;
  62. SetGUITextureWidth (Forward. guiTexture,
  63. (Int) (mWidth * (mValue/(double) MaxHP )));
  64. }
  65. // Set the texture width
  66. Private void SetGUITextureWidth (GUITexture mTexture, int mValue)
  67. {
  68. MTexture. pixelInset = new Rect (mTexture. pixelInset. x, mTexture. pixelInset. y,
  69. MValue, mTexture. pixelInset. height );
  70. }
  71. }

In the above Code, we need to grasp the following points:

1. The height of the target object is obtained based on Collider. Therefore, the object using the blood bar component must carry a collision tool.

2. After obtaining the two-dimensional coordinates through the WorldToScreenPoint () method, divide the two-dimensional coordinates by the x and y coordinates by the screen width and screen height to make the coordinates between 0 and 1, because GUITexture uses such a coordinate system.

3. When the volume of blood in the script is changed and the target object location is changed, the location and volume of blood records are automatically updated. In other words, we only need to change the value of HP to update the value of the blood bar.

Now, we create an empty GameObject in the scenario and name it Texture2DHP. We drag and drop the two GUITexture objects created earlier under this object to make them sub-objects. Drag and Drop the script to the Texture2DHP object. At this time, we should be able to see the following content:

We will import the two pasters prepared before the project and assign them to the script. The red pasters are the blood pasters and the yellow background pasters. Well, so far, a blood bar component has been basically formed. To make it available for reuse in the project, we have made it a preset. The following describes the preset creation method:

Preset, called Prefab in Unity3D, is a reusable game body. First, create a Prefab folder in the Project window, and then create a Prefab named Texture2DHP under the file. Drag and Drop Texture2DHP in the Hierarchy window to the Prefab. The Prefab turns to bright blue, indicating that the preset has been created successfully. Save the project and delete the Texture2DHP object in the Hierarchy window. In the following game development, we will always use this preset file. Well, let's test the results today. First, create a Cube and a capsule in the scenario, and then drag and drop the Texture2DHP preset to the game scenario, set the target object to Cube, and change the volume of HP to 35 and CubeHP. Similarly, we create a blood bar component for the capsule body, set its blood HP to 85, and rename it CapsuleHP. Now let's run the game:

So, how can we dynamically change the blood size of the blood bar in the script? We only need to get the Texture2DHP component with the specified name, then get the Texture2DHP script and modify the HP value. How about it? However, there is a problem with the blood strip implemented in this way, that is, the blood strip of all objects is on a plane, in this way, the size of the blood strip will not show a gradient as the distance from the camera changes. This is a matter of principle and cannot be avoided at present. When you test a model, you may not be able to obtain the height of the model. You have not yet come up with a better solution. If you have a better idea, please leave a message. Is there any better way? Yes, that is NGUI! Okay. I will share with you about NGUI later.


Questions about unity3D Game Development

I have been familiar with virtools and unity, and I feel that the engine has its own advantages. As long as I master all of them, I personally feel that unity is in line with my style. Not very gorgeous, but all game functions can be implemented. There are a lot of resources available for download in the unity store, and it is also very convenient to make 2d games.
You can also export your own game to your mobile phone. If you are narcissistic, you will feel satisfied. You can combine art and programming very well. There are very few talents in this field. It is not difficult to learn about unity. As long as you complete several game production examples with video, you can achieve most of the functions. To be proficient, you must study it.
There are many cracked versions of unity on the Internet, and 3.0 is enough. The cracked version is still stable. Occasional problems.
I feel that about 5000 of my laptop can meet the requirements, and I need to use a desktop computer to make a precision model.

For beginners, click game development. By the way, click unity3D.

If you really want to do this, you have to start from scratch and from the most basic. There are several videos on the Internet that seem good after learning U3D for two months. In fact, they are all plug-ins. This method seems to be fast but basically useless, just to satisfy vanity, what can a scenario without interaction be considered?
I started from scratch, and now it's almost half a year. The most difficult thing I feel is programming, because I have never touched it before, and it's a little basic for Model Materials. Most of the time is spent on learning programming, because this aspect cannot be bypassed. Although there is a plug-in playMaker that can play games without programming experience, it doesn't feel good, but if you don't want to learn programming, you can try it.
For programming, unity often uses Javascript and C #. Although javascript is easy to learn, Unity's javascript is not a standard, but its unique UnityScript, but now there is no systematic UnityScript teaching material, so it is not easy for me to learn. However, I am still reading UnityScript, because I learned it from the beginning and want to learn it and then change it to C #. C # is recommended if you want to learn it, but I don't know if unity's c # is the same as standard c #. You can make a decision in this regard.
If untiy is suitable for personal development, you can watch the video of kotoyev. There is a comparison between unity and several other engines, among which there is a well-known engine such as unreal.
If you say that it has been made in two years, it still feels reliable, but in the first year, you should stick to the foundation. Even if the foundation is ready, do not rush to make large games or try some games.
One of the reasons for using unity is that I have not compiled a map in the Warcraft map editor, but since the editor can make something as classic as dota, can't unity be used? By the way, I know dota but I have never played it. Cheng Hai has been playing it for a while. Compared with dota, I prefer other Warcraft games on the haofang platform, for example, the fire of passers-by in the past, the war of rabbits and sheep, and so on. The Battle of warcraft also has a high level, such as the bloody mission, which can be called the highest in China, while the Open-mode reverse-flow battle can be played as an online game.

In addition, not all things need to be done by yourself. For example, in Outfit7 development, the first model used was bought from a website. If a plug-in is very helpful for game production, you may wish to use it. However, we recommend that you start from the basics and use the plug-in to improve efficiency.
Currently, the active forum in China is the unity holy book.
Other sub-sections, such as tiandihui and cocochina, are not very active and unity3d8 is also good. However, many resources need to be downloaded at a higher level.
It is only a transformation for whether a character is 2D or 3D. The so-called 2D is a series of rendered images. Most of these images are rendered using 3D models, but unity is not suitable for 2D, only plug-ins are available.

Because it is self-taught, there is no systematic thing to tell you, you can only say that this is a very difficult way, do it after making a decision!
It's a bit messy.

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.