📜 ⬆️ ⬇️

An example of creating a simple 2D game for Android using the Unity game engine

Introduction




First of all, I want to immediately note that I am not a professional developer. In this article I will try to explain my experience in creating the game “Feel Speed ​​Racing”. This material will most likely not be of interest to those who already have extensive experience in game development, but I think it will be interesting for novice developers who have worked with Unity at least a little.

Design


The concept of the game is that the car must pass, as much as possible while on the road dynamically appear obstacles they need to go around otherwise the game is over, you still need to monitor the fuel scale and collect fuel tanks on the road, after which the game also stops .

Development


The game consists of 2 scenes: the main menu and the game scene itself:
')


Where “menu” is the main menu and “1” is a game scene.

Main menu




To create such a simple menu, we need a GUI control, which is standard in Unity.



As a background, I used a sprite with the name “background” filled with gray. You can choose anything.



Next, create the “menu.cs” script (Right click-> select Create-> C # Script) and hang it on the background.

Script content:

using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization.Formatters.Binary; using System.IO; public class menu : MonoBehaviour { public GUIStyle mystyle; //      GUI (,  ...) string score; //     void Start () { StreamReader scoredata = new StreamReader (Application.persistentDataPath + "/score.gd"); //   score = scoredata.ReadLine (); //  scoredata.Close (); //   } void Update () { } void OnGUI(){ GUI.Box (new Rect (Screen.width*0.15f, Screen.height*0.8f, Screen.width*0.7f, Screen.height*0.1f), "MAX DISTANCE:"+score,mystyle); //       if (GUI.Button (new Rect (Screen.width*0.15f, Screen.height*0.25f, Screen.width*0.7f, Screen.height*0.1f), "Start game",mystyle)) //      { Application.LoadLevel(1);//   } if (GUI.Button (new Rect (Screen.width*0.15f, Screen.height*0.4f, Screen.width*0.7f, Screen.height*0.1f), "Exit",mystyle)) //      { Application.Quit();//   } } } 

The result should be something like this:



You can change the font, color, and size of GUI elements using MyStyle.

Creating a game scene




The main elements on this scene are the road, the car and the fuel scale.

1. Road:

Due to the fact that the race is endless and stops only when the car hits an obstacle or gasoline runs out, the road is moving. That is, the car can move left or right, and the road creates the illusion of driving in a straight line.



We throw the sprite with the road to the game scene and adjust the size of the camera.



Then we add 4 blocks with obstacles inside the road, a fuel tank and don’t forget to add a Box Collider 2D to them. Another thing to note is Is Triger for crossing with the car.



Now create the moveroad.cs script and hang it on our way.

Add the following code to it:

 using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Runtime.Serialization.Formatters.Binary; using System.IO; public class moveroad : MonoBehaviour { public GUIStyle mystyle;//  int f,fuelst; float score=0,speed=-0.2f,data,fuelpos;//    ,    public GameObject block;//      public GameObject block1; public GameObject block2; public GameObject block3; public GameObject fuel; bool turbotriger=false; void Start () { StreamReader scoredata = new StreamReader (Application.persistentDataPath + "/score.gd"); data = float.Parse(scoredata.ReadLine ());//      scoredata.Close (); } void Update () { transform.Translate (new Vector3 (0f,speed,0f));//      score = score + (speed*-10);//   if (transform.position.y < -19f) //         " "  { transform.position=new Vector3(0f,33.4f,0f);//   block.transform.position=new Vector3(10.15f,block.transform.position.y,block.transform.position.z); block1.transform.position=new Vector3(8.42f,block1.transform.position.y,block1.transform.position.z); block2.transform.position=new Vector3(6.62f,block2.transform.position.y,block2.transform.position.z); block3.transform.position=new Vector3(4.95f,block3.transform.position.y,block3.transform.position.z); fuel.transform.position=new Vector3(11.86f,fuel.transform.position.y,fuel.transform.position.z); //     () f = Random.Range (0, 5);//    1-  4-      switch (f) { case 0:block.transform.position=new Vector3(2.40f,block.transform.position.y,block.transform.position.z); break; case 1:block1.transform.position=new Vector3(0.90f,block1.transform.position.y,block1.transform.position.z); break; case 2:block2.transform.position=new Vector3(-0.80f,block2.transform.position.y,block2.transform.position.z); break; case 3:block3.transform.position=new Vector3(-2.35f,block3.transform.position.y,block3.transform.position.z); break; case 4: fuelst=Random.Range(0,4); if(fuelst==0){fuelpos=2.40f;} if(fuelst==1){fuelpos=0.90f;} if(fuelst==2){fuelpos=-0.80f;} if(fuelst==3){fuelpos=-2.35f;} fuel.transform.position=new Vector3(fuelpos,fuel.transform.position.y,fuel.transform.position.z); break; } if (score>data)//                { StreamWriter scoredata=new StreamWriter(Application.persistentDataPath + "/score.gd");//       scoredata.WriteLine(score);//     scoredata.Close();//   } } } void OnGUI(){ GUI.Box (new Rect (0, 0, Screen.width, Screen.height*0.05f), "Distance(m): " + score,mystyle);//     } } 



It should be something like this. If everything is left so then after the road passes to the end, there will be empty space and in a circle, the road will disappear.



To solve this problem, you need to create a duplicate of the finished road and slightly modify the script.



It should turn out like this.

2. Car:



We throw the sprite of the car on the scene and set it in any place on the road. Then we create the carcontroller.cs script and hang it on the car.

Contents of carcontroller.cs:

 using UnityEngine; using System.Collections; using UnityStandardAssets.CrossPlatformInput; public class carconroller : MonoBehaviour { void Start () { } public void Update () { if (transform.rotation.z !=0) //     ,      { Application.LoadLevel (0); } } } public void OnGUI() { if (GUI.RepeatButton (new Rect (Screen.width*0.1f, Screen.height*0.9f, Screen.width*0.2f, Screen.height*0.08f), "L")) //     { if (transform.position.x > -2.4f) { transform.Translate (new Vector3 (-0.05f, 0f, 0f)); } } if (GUI.RepeatButton (new Rect (Screen.width*0.7f, Screen.height*0.9f, Screen.width*0.2f, Screen.height*0.08f), "R")) //     { if (transform.position.x < 2.4f) { transform.Translate (new Vector3 (0.05f, 0f, 0f)); } } } } 

Now the car can move.

3. Fuel tank:



To create the scale, it took 2 sprites of the same size, but different colors (red, green). And make one of them a child (green).

Next, create a script fuelscript.cs, hang it on fuel and add the code to it:

 using UnityEngine; using System.Collections; public class fuelscript : MonoBehaviour { public GameObject fuelall; float mytimer=100f;//    // Use this for initialization void Start () { } void Update () { mytimer = 100f; mytimer -= Time.deltaTime;//     if (mytimer/mytimer==1f) //     1  { fuelall.transform.position=new Vector3(fuelall.transform.position.x-0.0011f,fuelall.transform.position.y,fuelall.transform.position.z); fuelall.transform.localScale = new Vector3(fuelall.transform.localScale.x-0.001f, 1, 1); //             } if (fuelall.transform.localScale.x < 0) //         { Application.LoadLevel(0); } } } 



My road is road183 and its duplicate road183 (1). In its child object fueltrack, you need to add a script to detect the intersection with the car and replenish the fuel.

Create a triger.cs script and hang it on fueltrack on both roads and mark it as Is Triger. Code:

 using UnityEngine; using System.Collections; public class triger : MonoBehaviour { public GameObject fuel;//  greenfuel // Use this for initialization void Start () { } // Update is called once per frame void Update () { } void OnTriggerEnter2D(Collider2D col) { if (col.gameObject.name == "playercar") //     fuel { fuel.transform.position=new Vector3(0,fuel.transform.position.y,fuel.transform.position.z); fuel.transform.localScale = new Vector3(1, 1, 1); //   fuel   } } } 

Total


At the time of the release of the game on Google Play, I was not particularly engaged in its promotion, and of course there were no downloads.

In the absence of a professional artist, I had to work with the icon myself:

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


All Articles