Getting Started with Unity Day 2: Movement, Collision and Rendering

This article describes the collision volume of objects and the rendering of map priority.

Add collision volume

In order to implement the map design in tank battle, we need to add collision volumes (Box Collider) to the terrain (walls, rivers), and to add rigid bodies (Rigidbody) to the tanks.

Because Tank Wars is a flat game, remember to import 2D files, such as Box Collider 2D, Rigidbody 2D.

The Rigidbody component allows a body to be physically affected. For example, after adding the Rigidbody component, the object will immediately respond to gravity. If a Collider is also added to the object, the object will also move when collided. The reason why the rigid body is added to the tank is because the player will always control the tank, so the rigid body will not sleep, but will remain active.

However, during testing we found that the tank would keep falling down without our control, the reason is that the gravity parameter is set in the initial setting, just set the Gravity Scale to zero.

Restrict object movement

  • In the first day of learning, we realized the movement of the tank, but in the actual test, we found that the direction of the tank would become distorted because it collided with the edge of the terrain, which is not the ideal up and down, left and right. The solution is to click Rigidbody 2D —> constraints (constraints) -> Freeze Rotation to lock the Z axis.
  • The second problem is that when the tank is driven towards the wall, the tank will not stop moving near the wall, but will make a weird jitter in the push-and-pull style. We put the original code that controlled the movement of the tank in Update() into FixedUpdate(). The jitter is because the trigger judgment time is different for each device. If fixedupdate is used, let him make a uniform judgment to solve the jitter . Correspondingly, Time.deltaTime should also be changed to Time.fixedDeltaTime.
  • For players (me) who like to press two arrow keys to move at the same time, setting the priority of up, down, left and right can limit the player’s operation. For example, when the player presses the up and right keys at the same time, if the priority of horizontal movement is set higher than that of vertical movement, the player can only go to the right. The implementation code is as follows:
    1
    2
    3
    4
    5
    //If the player has input in both the horizontal and vertical directions, set the horizontal direction to have the highest priority
    //If there is input in the horizontal direction, just return directly without taking care of the vertical direction
    if (h != 0){
    return;
    }

2D scene rendering

In order to realize that the grass should be above the tank after the tank enters the grass, and the bullet is above the river when the bullet flies over the river, we need to set the priority of the 2D scene rendering:

  • Sorting layer is a large layer
  • Control the order of rendering under the large layer: The larger the Order in layer, the later the rendering will cover the objects with smaller numbers in other layers.

#Update the launch and movement of bullets tomorrow! (Actually, I have learned the firing of bullets, but the direction design Euler angles and quaternions, I think I should learn it clearly and then write)