📜 ⬆️ ⬇️

The book "Unity in action. Multiplatform C # development. 2nd int. edition"

image Hi, Habrozhiteli! The second edition of “Unity in action” has been completely redesigned to introduce you to new approaches and ideas that make the most effective use of Unity for game development. More attention is paid to the design of two-dimensional games, the fundamental concepts of which the reader can put into practice and build a complex two-dimensional platformer. This book can be called an introduction to Unity for professional programmers. Joseph Hawking gives people with development experience all the necessary information to help them quickly master a new tool and start creating new games. And to learn best on specific projects and practical tasks.

Unity is often presented as a set of components that do not require programming, which is fundamentally wrong. To create a successful game, a lot is needed: great work by an artist, programming skills, an interesting story, fascinating gameplay, friendly and well-coordinated work of the development team. And we must not forget about the impeccable visualization and quality work on all platforms - from gaming consoles to mobile phones. Unity combines a powerful engine, professional programming and creative designers, allowing you to implement the most incredible and ambitious projects.
Get to know Unity and start creating your own games faster!

Book structure


Chapter 1 introduces Unity, the cross-platform game development environment. You will learn the basic system of components that underlies Unity, as well as learn how to write and execute basic scripts.

In Chapter 2, we will move on to writing a program that demonstrates motion in three-dimensional space, while at the same time examining topics such as typing with a mouse and keyboard. The definition of the position of objects in three-dimensional space and the operation of their rotation are explained in detail.

In Chapter 3, we will turn the demo program into a first-person shooter, demonstrating the method of emitting a beam and the basics of artificial intelligence. Beam emission (we create a line in the scene and see where it intersects) is required in all versions of the games.

Chapter 4 deals with the import and creation of game resources. This is the only chapter in the book in which code does not play a central role, since each project requires (basic) models and textures.

Chapter 5 teaches you how to create two-dimensional games in Unity. Although initially this tool was intended solely to create three-dimensional graphics, it now perfectly supports two-dimensional graphics.

Chapter 6 continues the explanation of the principles of creating two-dimensional games on the example of a platformer. In particular, we implement controls, an imitation of the physical environment and animation for the character.

Chapter 7 introduces the latest Unity GUI functionality. The user interface is required by all games, and the latest versions of Unity boast an improved user interface creation system.

In Chapter 8, we will create another program that demonstrates motion in three-dimensional space, but this time from the perspective of an outside observer. The implementation of third-party controls will give you an idea of ​​the key mathematical operations in three-dimensional space, in addition, you will learn how to work with animated characters.

Chapter 9 shows how to implement interactive devices and elements in the game. The player will have a number of ways to use these devices, including direct touching, touching the starting devices inside the game or pressing the controller button.

Chapter 10 teaches interaction with the World Wide Web. You will learn how to send and receive data using standard technologies, such as HTTP requests to receive XML data from a server.

In Chapter 11, you learn how to add sound to games. In Unity, both short sound effects and long musical phonograms are remarkably supported; Both soundtracks are critical to almost all video games.

In chapter 12, we will put together fragments from various chapters to get one game as a result. In addition, you will learn how to program controls that are manipulated using the mouse and save the game.

about the author


Joseph Hawking lives in Chicago and develops software for interactive environments. He works at InContext Solutions. The first edition of this book was written during his work at Synapse Games. In addition, he taught game development at the University of Illinois, at the Art Institute of Chicago and at Columbia College. His website is www.newarteest.com .

Excerpt 6.4.2. Push up


Let's teach the character to jump. To do this, when you press the Jump button, the character must push up. The speed of moving horizontally is edited in the code directly, and with vertical movement we will act differently, so as not to interfere with the action of gravity. The fact is that an object can be influenced by several forces simultaneously, therefore we add to it a force directed upwards. Paste this code into the motion script.

image

Pay attention to the AddForce () command. Upward force is added to the Rigidbody component in a pulsed mode; impulse is a sudden impulse unlike a continuously acting force. In the code, such a push occurs after pressing the Space key. In this case, the character continues to act by gravity, so that after the jump, he describes a beautiful arc.

But, perhaps, you have already paid attention to one drawback, the elimination of which we will now deal with.

6.4.3. Surface recognition


The jump control has one small drawback: the ability to jump in flight (when the character has already jumped or when it falls). Pressing the Spacebar even in this case will give an impulse upwards, although this cannot be done. The jump control should only work for a character standing on the platform. So, you need to learn to determine whether a character is worth on the surface.

image

image


This code deprives the character of the ability to jump in the air. It checks if there is a collider under the character, and considers the result of the check in the condition for making the jump. First of all, the boundaries of the primitive collisions of our character are determined, and then in the area with the same width, overlapping colliders are found directly under the character's feet. The result of the check is stored in the variable grounded and used in the conditional operator.

6.5. Additional features for the platformer


So, we figured out the nuances of the implementation of movements, walking and jumping character. Let's add demo version of our game with new functionalities of the environment.

DESIGNING LEVELS WITH TILEMAP_ ______ TOOLS
Within the project, the floor and the platforms are made of empty white rectangles. The final version of the game requires more elaborate graphics, but it is unlikely that the computer will be able to process an image the size of a whole level. Most often, this problem is solved using the tilemap tool. It allows you to design images on the grid principle. Here's what it looks like.

image

Please note: the map is entirely made up of small cells. This is what allows you to cover the entire screen with an image without resorting to images that are too large.

The official tilemap tool has been added to the latest Unity application. You can also use external libraries, such as Tiled2Unity, a system that imports maps created in the popular (and free) Tiled editor.

6.5.1. Inclined and one-sided platforms


Now in our game there is only a normal floor and a block through which you can jump. But there are other options for platforms. Use them to diversify the scene. Let's start with the slope. Create a copy of the Floor object, give it the name Slope, enter the values ​​0, 0, -25 in the rotation transformation fields and move it to the left, assigning the displacement coordinates to –3.47, –1.27, 0.

Play the scene. In the process of movement, the character correctly slides up and down. But due to gravity, it begins to slide down and in a static state. To solve this problem, the imitation of gravity should be turned off in cases when the character (a) is on the platform, (b) is at rest. Fortunately, we have already taught him to recognize surfaces and can use this in a new code snippet. You need to add just one line.

image

After editing the movement code, the character began to behave correctly on inclined surfaces. Now add the so-called one-way platform. The character can not only stand on it, but also jump through it. When you try to jump through the usual platform character will hit her head.

One-way platforms are often found in games, so Unity has the means to implement them. If you remember, earlier when adding the Platform Effector component, we deselected the Use One Way checkbox. And now we need it! Once again, create a copy of the Floor object, give it the name Platform, enter the values ​​10, 1, 1 in the scale fields and place the resulting object above the floor, specifying for it the coordinates -1.68, 0.11, 0. And be sure to check the Use One Way box in the section Platform Effector 2D.

The character will be able to fly through the platform when jumping up, but the movement down it will interfere. Need to eliminate only one drawback. It is illustrated by rice. 6.11. In Unity, the platform's sprite may appear on top of the character (to make it easier to see, assign the variable jumpForce to value 7. You can edit the character's Z coordinate, as was done in the previous chapter, but this time we will act differently. The component responsible for visualization sprites, there is a sorting order that determines which sprite will be displayed above.To highlight the Player object and set the Order in Layer parameter in the Sprite Renderer section to 1.

image

Now in the scene there is an oblique fragment of the floor and a unidirectional platform. But there is another unusual option that is implemented a little more difficult than the platforms you already know. Let's see how this is done.

6.5.2. Moving platforms


The third variant of an unusual floor, common in platform games, is moving platforms. In order to add such a platform to our game, it will be necessary, first, to write a new script controlling its movements, and second, to edit the character's movement script. In the new scenario, the platform will move between two points: the starting point and the ending point. Create a C # script named MovingPlatform and add the following code to it.

image

image

DRAWING CUSTOM GIZMOS _________________________________________
Most of the code, which is written in the process of reading a book, programs what is happening in games. But Unity scripts allow you to influence the built-in editor. Many do not know that additional menus and windows can be added to Unity. Moreover, with their help, auxiliary objects are created on the Scene tab, which are called overall containers (gizmos).

You have already encountered large containers. Remember green parallelepipeds denoting colliders? They are built into Unity, but no one bothers you to write a script for your own dimensional container. For example, on the Scene tab, create a line corresponding to the trajectory of moving the platform. It is shown in the figure.

image

The code that draws such a line is very simple. At the top of the code that affects the Unity interface, add the line using UnityEditor; (because most of the functions of the editor are in this namespace), but in this case it is not required. Just add this method to the MovingPlatform script:

... void OnDrawGizmos() { Gizmos.color = Color.red; Gizmos.DrawLine(transform.position, finishPos); } ... 

About this code it is important to understand the following. First, all the action takes place inside the OnDrawGizmos () method. This is the system name, as are the names of the Start and Update methods. Secondly, we add two lines of code to this method: the first one sets the line color, and the second one forces Unity to draw a line from the place where the platform is located to the target point.

Drag this script onto the platform. Start playback of the scene, and you will see that the platform is now moving from side to side! It remains to edit the script that manages the movements of the character, in order to link it with the moving platform. Make the following changes.

image

Now the character who has jumped onto the platform will move with it. In fact, it all came down to the transformation of the character into a child object in relation to the platform. I hope you remember that after the formation of a hierarchical relationship, the child object begins to move after the parent. The GetComponent () method in Listing 6.7 checks whether the recognized surface is a moving platform. In the case of a positive test result, she becomes an ancestor in relation to the character; otherwise, the character is disconnected from any ancestor.

But one problem arises. The character inherits the scaling transformation from the platform, which affects its size. The situation allows to correct the inverse transformation (reduction of the character scale, compensating for the platform-induced increase in size).

image

image

Mathematically, scaling compensation looks very simple: the player is assigned 1 divided by the scale of the platform. And when a little later the player’s scale is multiplied by the scale of the platform, the result is 1. The only difficult point in this case is to take into account the sign that determines the direction of movement; You may remember that depending on him, the character turned from side to side.

So, we have added a moving platform to the game. Only one finishing touch is left, and the demo version will be completely ready ...

»More information about the book can be found on the publisher's website.
» Table of Contents
» Excerpt

For Habrozhiteley a 20% discount on the coupon - Unity

PS: 7% of the cost of the book will go to the translation of new computer books, a list of books submitted to the printing press here .

Source: https://habr.com/ru/post/440292/