[Classic Game] Tank Battle Unity2D Project Combat (Nanny Level Tutorial)

main content:

1. Basic settings in the Unity3D engine.

2.2D scene construction, prefab production.

3. Production of 2D animation.

4. Knowledge about picture atlas.

5. Colliders, triggers, collision detection and trigger detection.

6. Some knowledge of 2D game rendering.

7. Compilation of enemy AI.

8. UGUI-related content, scene switching, etc.

Required resource pack link: https://pan.baidu.com/s/199wuwMiucKDQfZR54Skwxg?pwd=a6pa 
Extraction code: a6pa

--------------------start-------------------

1. Create a new project and import resources

Create a new 2D project and change all image texture modes to 2D ANDUI

 2. Scene construction

1. Set the main camera: size (position from the screen) is set to 8.5

2.Background is set to black game display

3. The window is set to 5:4

4. Atlas processing: Adjust the Texture of the imported material to Sprite (2D and UI), click apply.

5. Sprite Mode: Multiple (the default attribute of the picture is single, which means that the picture is treated as a single sprite here).

5.Sprite Mode: Multiple (the default attribute of the picture is single, which means that the picture is treated as a single sprite here)

Cutting Atlas (need to cut): Click Sprite Editor——Slice——Automatic——Click Slice (resize, get size)——Grid By Size (cut according to a fixed size)——Click Slice——Cut by yourself to find the appropriate size , W28H28, just fill in the size of the cutting size.

3. The production of prefabs in the game and the production of 2D animation

1. Drag Play1 into the Hierarchy panel

2. Unity is developed in meters, and the tank scale is adjusted to 3, 3, 3. Renamed to Player

3. Under the Project panel, create a new folder Prefabs to store the prefabs, drag them into the Player

4. Then drag the stones, walls, water, grass, etc. under the Map into the Hierachy panel

5. Solve the problem of resizing each picture: the Sprite under the Sprite Render component controls the displayed picture, Ctrl+D to copy several same objects, and then drag the picture to the Sprite.

 

 

6. Create folders Animation and AnimatorControllerBorn to store animations and animation controllers

2D animation: Click the mouse to select the first picture and hold down Shift to select the last picture, drag it into the Hierarchy panel, name the animation in the pop-up window, name it Born, and change the name to Born in the Hierarchy panel. Resize to 3, 3, 3

 

7. Create folders Animation and AnimatorControllerBorn to store animations and animation controllers

2D animation: Click the mouse to select the first picture and hold down Shift to select the last picture, drag it into the Hierarchy panel, name the animation in the pop-up window, name it Born, and change the name to Born in the Hierarchy panel. Resize to 3, 3, 3. Rename the controller to BornController, and drag it into the corresponding folder with the animation.

Create a Map folder under the prefab Prefabs folder to store maps and Effect folders

In the same way, create the explosion effect Boom and the invincibility effect Born2, cancel the display of the camera Gizmos for the river Water, and fork off the camera next to the Camera

 4. Control the player's movement (Player script)

 1. Create a new folder Script to store all scripts.

 2. Create a (Player) script and add it to the player tank

3. Control the movement of the game object. In the Update function, it needs to be changed in the FixedUpdate function due to the collision and jitter problem.

 

 4. Control the player's movement - picture switching in different directions (Player script)

1. Get the Sprite component and change its property value

2. Get the reference of the renderer:

 3. In the Awake function (obtained at the beginning), before the Start function

4. Get the reference to switch pictures: and drag the corresponding pictures into Unity

 

 5. The addition of colliders solves the problem of tank rotation and shaking

1. There should be colliders on the colliding objects, and one of them (preferably the moving one) should have a rigid body.

Add Box Collider 2D (2D game collider) and Rigibody 2D to the Player game object, and change the gravity to 0

 2. Add Box Collider 2D (2D game collider) to all objects in the Map, and the grass is not used, because the grass can pass through

3. Solve the rotation problem: select the Player game object, check the upper z axis in the Constraints under Rigibody 2D

 

4. Solve the jitter problem: Unity's built-in life cycle function (fixed physical frame)

 

Sixth, the addition of mobile priority, 2D rendering level problem (Player script)

1. Solve the problem of pressing two keys at the same time and walking diagonally. Vertical priority. First determine V

 

 2. Then encapsulate the code that controls movement into a method and call it in FixedUpdate

 

3. Rendering priority: The higher the Order in Layer level, the later it will be rendered. Born, Grass set to 1

4. Add bullets, set the level to 1, and drag them into the Tank folder to make a prefab. The explosion effect level is also set to 1

 

 7. Tank attack method and bullet direction (Player script)

1. Get the reference of the bullet prefab and define the bullet rotation angle

2. Instantiate a bullet when pressing the space

3. Solve the problem of bullet orientation and write the moving method. Rotate picture - rotate Z axis

8. Added bullet movement script and attack CD

1. Create a bullet script (BulletFly), set the bullet movement in Update

 

 

2. Set the bullet CD (Player script)
to set the timer and add the bullet instantiation method to make the time reset to zero after each shot

 

3. Add a trigger to the bullet, add a Rigidbody2D to the bullet, and set the gravity to 0.

 

 Nine, the production of the air wall, the addition of labels

1. Find the rocks that cannot pass through and be eliminated, and delete their textures to become an air wall

2. Add a corresponding Tag (tag) to each prefab

3. Add perfect collision detection in (BulletFly script)

 

 10. Compilation of player invincibility method and death method (Player script)

1. Declare explosion effects, protection effects, whether to revive, invincible time

2. Tank Death Method

 3. Tank invulnerability method

 4. Place the invincible effect as a sub-object of the tank, and change its rendering level to -1

 11. Distinguish between player bullets and enemy bullets

 1. Add a judgment statement in (BulletFly script), whether it is a player bullet

2. Add a statement when generating bullets for the player tank (Player script)

12. Make player birth special effects (Born script)

1. Added Born script for the initial generation of players

2. Drag the player tank prefab into Unity

 13. Enemy birth effects (Born script)

Randomly generate two types of enemies with the same special effects, rewrite the Born script code, and drag the prepared two types of enemy prefabs into Unity

 14. Compilation of enemy AI (Enemy script)

1. Create a new (Enemy script) to write the code for the enemy's automatic attack and movement, copy the Player script into it, and modify it.

Modify the attack method, you don't need to press the space bar to attack, it will attack automatically

2. Modify the movement method 

 

 15. Generate different elements on the map (MapCreation script)

1. Create a new empty object named "MapController", and assign a new script "MapCreation".

2. Use an array to store all elements, drag and drop in Unity

 

3. Write a method to instantiate the object (you can place the instantiated object as a sub-object under the object where the script is located)

 4. Find the location of the hometown, the location of the air wall, and instantiate it through the method of instantiating the encapsulated object

 5. When randomly generating walls, water, enemies, obstacles, and grass objects, it is necessary to use an array to record the positions that have been used to prevent two objects from being instantiated in one position

6. Write a method to randomly generate a position

7. Determine whether there is a duplicate with the generated position in the position list

 8. Instantiate the elements, players, and enemies in the game

16. Player rebirth and scoring (PlayerController script)

 1. Create a new script "PlayerController" and instantiate the instance

 2. Ctrl+R+E can be quickly deployed

 3. Instantiate a singleton

 4. Declare life value, score, and death

 5. The method of rebirth (if the life value is 0, return to the start interface)

 6. Call the regeneration method in Update

 7. If the player dies in the "Player Script" , find the death method and add the property value calling isDead

 

 8. If the enemy dies in the "Enemy Script" , find the death method and add the attribute value calling Score

 Seventeen, UI production

1. Use the background color in the resource  as the 5:4 background color, and adjust the color of the main camera to gray

2. The game window ratio is set to 16:10

3. Use Image, Text to make score and life value

4. (PlayerManager script) exposes two text attributes and assigns them in Unity

 5. Let the text change with the score and life value in Update

 6. Make a picture of the game failure in Unity and assign it 

 7. Display game failure in Update and stop running

 ————————————————— End of specific development process ——————————————————

In-game image

Guess you like

Origin blog.csdn.net/Y1RV1NG/article/details/125408063