With a simple 2D game case to learn about unity 2D Game development, this article is divided into the following 4 sections.
· Create a work layer
· Add a static scene
· Making 2D Animations
· adding Roles and controls
· Add 2D effects
Through this case, we can learn the basic process of unity2d game production, the creation and use of sprites, the use of 2D physics engine, 2D special effects, and other common knowledge points.
The camera in the 3D gaming scene generally uses perspective mode (perspective), and the camera in the 2D game scene is in orthogonal mode (orthographic), if you unfortunately chose Perspective Mode (Perspective) In this case, There will be issues such as the role cannot be moved according to our instructions.
· 1 Creating a work layer
In accordance with the order, we created two working layers background and foreground in the game scene. The priority of the 3 working layers is from high to low in order: Foreground, Background, Default, the higher priority machine is higher, so the first of the 3 working layers is Foreground.
• 2 Add static scene
To import images first, we can import the resources supported by unity in the following 3 ways:
1. Copy the resources directly into the assets folder under the project folder
2. Drag the resource directly into the Assets folder in Project view (one I personally prefer)
3. Open the menu bar Assets--import newasset ... To import.
Create a Sprite
1. In the menu bar, create
2. Quickly create a sprite and drag it directly from Project view to the hierarchy view
Once you have created a sprite, set the order in which it resides and the layer it is in. We created two sprites of grassland grass and sky skies, all in the background layer, but the grass is located in a higher order of work than the sky. So in the game, the grass is shown in front.
We have all the meadows and skies in one game object background to manage, drag them all into background, and become background's child objects. If you want to remove a parent-child relationship, simply move the child object outside the parent object.
• 3 making 2D animations
1. Animating Sprite
In order to do this, we can automatically cut the swan_sheet image into 8 small plots, which are used to display each frame of the animation in 8 small plots.
Click the triangle button on the image to display the cut image, and the slider in the lower right corner of Project view will change how the resource is displayed.
First create a Sprite, named Swan, and select a picture to put in the sprite.
We then use the transformation of the sprite called Swan as the animated frame to create an animated swan flapping its wings. Create an empty animation by selecting Swan in the hierarchy view and then selecting the Window--animation command and clicking the Create button in the. Finally, we'll save the animation to the project.
In the hierarchy view, first select Swan, and then select the Window--animation command to add the transformed sprite in the animation view.
Add a keyframe, right-click on the right side of a keyframe, select the Add Key command, and set the sprite for the animated frame transform. Then repeat the steps a few times, you can set the animation of each keyframe, and finally click on the animation view of the play button to watch the production of the animation.
2. Create a script to achieve the effect of Swan flight
Create a C # script first, and then edit the code as follows. We can run the game and see the animation by dragging the edited script onto the Swan in the hierarchy view.
Public classSwanmove:monobehaviour {Private floatMovespeed; PrivateVector3 startpos;//Swan initial Position//Use this for initialization voidStart () {movespeed=4; Startpos=NewVector3 ( A,3,0);//set the initial position of the SwanTransform.position =startpos; } //Update is called once per frame voidUpdate () {if(Transform.position.x >- A) {transform. Translate (Vector3.right*-movespeed *time.deltatime); } Else{transform.position=startpos; } }}
Create a basic 2D scene (part1)