📜 ⬆️ ⬇️

Unity - Conceptual ideas and tips for newcomers igrodeva. Simple procedural generation of models for 2D games

Introduction to the problem

Hello dear readers!

My thoughts about creating a series of simple lessons “conceptual ideas for newcomers igrodev” appeared spontaneously in me, somewhere at 2:00 am Moscow time during the creation of my current project. Well, no, it seems to me that it is spontaneous, but, probably, this is my subconscious mind that reacted so. I reacted because more and more young (whom I myself am) and very young programmers decide to start creating games. It seems to me that the general trend of youth programmers (and people interested in programming) has smoothly moved from creating websites and fashion blogs to creating entertainment products. I am beginning to notice this among my acquaintances, who at one time said that, they say, games are not serious programming, but now they are showing a real interest in this environment.
')
I explain this to myself in the following, obvious way. Any informational directions are acquired by mass tendencies to study under the fulfillment of two conditions: the presence of a simple and adequate tool for carrying out specific tasks in the chosen information sphere and the ease of making a profit when selling your completed tasks. As an example, you can again cite the sphere of web technologies (once websites were created ultra-hardcore, and now, probably, even my mother can put a blog on WordPress or its analogs. Again, why? Because WordPress has opened the opportunity to create your own blogs to all moms around the world, and because websites have opened access to fast popularity, fame, money and rum).

But the era of entertainment has come - I will not mention how hard the first games were made, but decades passed, paid game engines began to appear, after some time they became practically free (Unity, Unreal Engine4), and not the engines from Vasya from the next entrance in which you can only make “robbery of cows”, and mastodon engines that are complex development environments that can realize your dream projects. However, there is a downside to these popular technologies. Namely, a large number of low-quality products. Too lazy to invent something of their own ... “I’d better make a flappy bird!” Thought Vasya.

And I decided to do a series of lessons consisting of simple ideas that people can use in their very first projects, people who, maybe not so long ago, began to learn programming in general.

Random generation of 2D models (Unity)

When you make a game alone, it is not always possible to draw a large number of sprites for your characters, ships or buildings, objects in general.
Here procedural (random) generation comes to the rescue. What does this term mean in two (six) words? "Modelku creates a computer, not you."

On the Internet you can find millions of procedural generation algorithms in the field of gamedev (maybe one of them is even described by my mom in my own blog). Some of them will be simply complex (such as the generation of water surfaces using Nvidia Physics), others will be extremely complex, such as segmentation of the relief using fuzzy logic and fuzzy sets (Fuzzy Sets).

So what? I don’t think that the person who opens the unit for the second time will start thinking about how to understand how to implement it and where to apply the algorithm found in the game.

Easier to implement your own! Let it be simple, but understandable to you. And this is the most important step in all programming.
I offer you my version of the procedural generation of the model (applicable to any 2D game). I warn you, the code is extremely simple! Two lines! But first, what you need to prepare before implementing the code:

1. Understand which model (or object) you want to create procedurally - this could be your knight, a building, a spaceship, and so on.
2. Break into as many elementary parts as you want (for example, for a knight it could be a helmet, sword, shield, breastplate).
Note: the smaller the individual elements (for example, the sword you break into a handle and a blade), the more obvious that your generated model will look more natural and unique.
3. Draw on each element as many different sprites as you see fit.
4. Build your model in the Unity editor, the way it should look. I will use the model ship from my current project as an example.

image

Here are my sprite parts scattered on the stage, of which I will assemble the model geometrically necessary to me.

image

And it is time for the code!

using UnityEngine; using System.Collections; public class ChangeTextureF : MonoBehaviour { //   Sprites        public Sprite[] Sprites; void Start() { //       ChangeTextureFunc(); } public void ChangeTextureFunc() { //      0  //   ,  Random.Range //     int ChooseForTexture = Random.Range (0, Sprites.Length); //    SpriteRenderer,    //        , //    . GetComponent<SpriteRenderer> ().sprite = Sprites[ChooseForTexture]; } } 

The ChangeTextureFunc () function contains only two lines of code, as promised.
Hang the script on each element of your object as shown in the screenshot. Drop each element's sprites into an array right in the editor, and enjoy the result.

image

The spectacular result:

imageimage

PS Try to make sprites for your model of the same size so that there are no shifts in the model, however if you still want to make some particularly sophisticated sprite that is radically different in size from the main set, then simply move the point of rotation to the sprite editor'e Pivot point), which will determine the center of the sprite, as much as you need. Sometimes in Unity forget about this opportunity.

image

Conclusion:
Of course, I deceived you a little by saying that you will get procedural generation of models, rather, it can be called a quick combination of the model from the given sprites. But here I want to say in defense of this approach:
1) Well, there is no code, you draw three ships, divide each into three identical elements and get 27 ship models at the output. Hooray.
2) You will say that you are a cool brain and will be able to write an algorithm that will choose and color itself and the contours of the ships and you will have 1,000,000 combinations, well, I’ll tell you that every model you will have is a cracking because you don’t forget game development including art. And 10,000 of your tasteless cracks will not be remembered by anyone.
3) For newbies, this will be an impressive result that can push them to develop this idea in breadth and depth.

Ideas:
By the way, the examples that I came up with right at the time of writing this article are interesting to use:
1) Hang on each element a script containing parameters that fit into the setting of your game, for example for a spacecraft these can be parameters: the speed of movement of the ship, its damage, the number of hp or even abilities that will be acquired only from this type of part. Next, you throw on the main parent object, which holds all the details in itself, a script that will collect parameters from each part, and, for example, summarize them and thus you will get the general characteristics of the ship.
2) The idea from point 1 can be used gameplay even deeper, for example, in some RPG game where you can evaluate the enemy from afar not by the inscription “level 80 mobile” but by his clothes, because you have already seen similar elements before armor on the previous mob, which was immune to fire and lightning. That is, without using any text prompts the player will be experienced by finding out the influence of certain things on the characteristics.

So, what am I talking about? In creating a game, it’s not the main thing to chase after a super-complex code that realizes a million possibilities; in creating a game, the main thing is to extract a new and interesting idea with the minimum possible expenditure of energy. Firstly, it will boost your resourcefulness and ingenuity, and secondly, you will save energy to implement further new ideas. And then you can be able to change the modern market of monotonous crafts ... Maybe get fame, money, or just self-satisfaction. You started making games for this, didn't you?

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


All Articles