Unity makes games from scratch

1. Tile map for scene building

  1. create -> 2D object -> TileMap
  2. At this time, a Grid/TileMap GameObject will appear in the scene view. Click on TileMap to see a bunch of grids. This is where we need to draw.
  3. Window ->2D -> Tile palette Call up the Tile palette panel, create new palette, and then drag the image given by the artist into this panel. This is the material we used to brush the scene. Select the material and use the brush to brush in the scene. Come out the scene you like

2. Layer Relationship

  • When you open the background GameObject, you will find that the background appears in front of the scene you just made. At this time, you need to adjust the hierarchical relationship of the sorting layer. The lower the layer is displayed, the higher it is in the scene.
  • add layer adds two levels of background and frontground

3. Make the characters move

  • Project Settings -> input -> Check the code name of the left and right, or up and down movement respectively
Input.GetAxis("horizontal") 返回值 -1~0~1 之间的数 
rb.velocity = new Vector2(horizontalFloat * speed * Time.deltaTime, rb.velocity.y); 刚体的移动
  • The difference between Update() and FixedUpdate()
    Update(): refresh according to the frame rate. For example, fps=60 means refreshing 60 times in 1 second.
    FixedUpdate: refresh according to a fixed time. This function defaults FixedTime = 0.02s

4. Add animation to characters

  1. Add the Animator component to the character
  2. Create a new character's animation folder Animation, add an Animator in the folder and drag and drop the assignment to the component Animator added in the first step
  3. Window -> Animation brings up the Animation window. The most important point is that you need to click your player GameObject to create a new Animator. If you simply create Animation in the folder, you cannot add any pictures . Finally, drag the frame pictures given by the artist Drag to the Animation window to adjust the animation speed.
  4. Window -> Animation Call up the Animator window
    image-20210312112821520
  5. The switching of various states is based on the value of Parameters. Set the value of Parameters to float. When the running speed is greater than 0.1, switch to the running state
  6. State switching
animator.SetBool("***",defeault value);
  1. Collision detection + layerMask The collision detection of the character sets the layer of the ground to ground and then uses coll.isTouchingLayers(Layer) to determine whether it collides with the ground

5. Bug fix when running

The reason is because the added collider is a collider with a rectangular frame, and the ground is also a collider with a rectangular frame. Therefore, it simulates the relationship between the ground and human walking in reality, and sometimes small stones or obstacles will block the real person. And because the Freeze Z axis is checked, it will cause the illusion of being unable to run. The solution is to use the box collider on the upper body of the character and there is a circle Collider on the lower body.

6. The character follows the camera

  1. Use the plugin cineMachine, search for cinemachine in packageManager and import
  2. Set Follow to player
  3. Add an extended collider to the cinemamachine camera to prevent the characters from jumping beyond a certain range

7. Item collection

  1. Add cherry animation (same as step 4, add animation)
  2. Set the tag of the item to Collection. Determine whether it has collided with the object to be collected according to the Collider.tag of the collider. If it collides, the GameObject will be destroyed.

8. Bug resolution

  1. The character does not fall after jumping and hitting a wall
  • The cause of the problem: When the character jumps, the collider will have an acceleration to the right, and then friction will be generated.
  • Solution to the problem: Create a new material for the character and set the friction of the material to 0
  1. The problem of multiple jumps of characters
  • Solution 1: As long as the character is in the air, the y-axis direction has a speed, so it can jump when the y-axis speed is 0
  • Solution 2: Like the previous change of falling state, when the character touches the ground, it changes to a static state. This condition can also be used here.

9. UI displays the collected data

Use UGUI to create a Text number in the upper left corner, write a script to change the value of the number.
There is nothing to record in this part, it is relatively simple

10. Add game enemies, add enemy AI

Add the frog enemy AI, add a gameobject on the left and right sides of the frog as the left and right boundary points. Let the frog only move back and forth between these two objects.

Guess you like

Origin blog.csdn.net/woshihaizeiwang/article/details/115217440