Implement hit detection for each type of MapChip

Page update date :
Page creation date :

Verification environment

  • Windows 11
Unity Editor
  • 2021.3.3f1
Input System Package
  • 1.3.0

Prerequisites for this tip

The following settings have been made in advance as a premise for the description of this tip.

What are the prerequisites for this tip?

About borrowing map materials

We process borrowed from the following sites.

At first

This time, MapChips are placed in one TileMap, and it is decided whether to perform hit determination according to the type of MapTip placed.

Since this tip is largely the same as another Tip "Implement hit detection with tilemaps", Please refer to there for a detailed explanation. Here, only the procedure is explained for the same content, and only the differences are added.

Implementation of walking characters

First, prepare a sprite image that the player will control to perform the hit detection operation and add it to your project. This time, the size of the maptip is 32 pixels, so I will make the image the same size.

I will omit the walking animation this time because the procedure will be long if it is implemented. If you want to implement it, please refer to the following tips.

Select the image that you added to your project. Since we want to match the unit with the map chip, set the "Number of pixels per unit" to "32".

Once configured, click the "Apply" button below.

Once the image is set, drop the image into the view and add the object.

Set the character's movement process. Add the script and leave it named Player .

The script looks like this: This is a simple movement process that moves the sprite with the keyboard.

using UnityEngine;
using UnityEngine.InputSystem;

public class Player : MonoBehaviour
  // 一定時間ごとに呼ばれます
  void FixedUpdate()
    // キーボードの情報を取得
    var keyboard = Keyboard.current;

    // スプライトの移動処理
    if (keyboard.leftArrowKey.isPressed)
      transform.Translate(-0.1f, 0, 0, Space.World);
    if (keyboard.rightArrowKey.isPressed)
      transform.Translate(0.1f, 0, 0, Space.World);
    if (keyboard.upArrowKey.isPressed)
      transform.Translate(0, 0.1f, 0, Space.World);
    if (keyboard.downArrowKey.isPressed)
      transform.Translate(0, -0.1f, 0, Space.World);

The script you create attaches to the character object.

Run the game and check if the character moves with the keyboard.

Preparing MapTips

Prepare a maptip image and add it to your project. As the number of map chips increases, setting becomes troublesome, so this time we have prepared only two squares, one that can be walked and one that cannot walk.

Select the images you added to your project and configure the settings for MapChips. See the following tips for more information:

Once configured, click the Apply button and then the Sprite Editor button.

Divide by 32 pixels.

Select the Tile palette tab to create a palette. If there are no tabs, you can display them from the menu with "Window > 2D -> Tile Palette". The palette name can be anything, but here it is "MapChipPalette".

Since we will save it in a folder, create a new "MapChipPalette" folder in the Scene folder and specify it.

A MapChipPalette folder has been added to the project.

Next, you'll add MapTips to the palette. Drag and drop the split maptip image onto the palette.

MapTips have been added to the palette.

Create a map

Leave the hit detection settings aside and create the map first. You can set up hit detection later. First, add a tilemap to your hierarchy.

Once added, select Tilemap. The view displays a grid.

First, set a mapchip that does not become a wall in the view.

For the time being, I laid it all over the place.

Next, place the stone maptip that will serve as the wall.

This time there was no problem, but if your character is going to be behind the map, set the Layer Order to a number larger than Tilemap.

Of course, even if you move the game at this point, you will slip through the wall and walk.

MapChip hit detection settings

From here, we will set whether to enter hit judgment for each type of map chip. First, open the "MapChipPalette" folder that you created when you created the palette.

There is a maptip set in the palette inside, so first select a maptip that you can walk on.

From the Inspector, set the Collider Type value to None. Later, you will add a collision detection setting to Tilemap, but setting it to "None" will prevent this mapchip from colliding.

The other stone maptip is set to "Grid". Since it is not a transparent maptip this time, there is no problem with "sprite", but if you set it to "sprite", it will not be judged by hitting the transparent part of the image.

That's all there is to MapTip. It's easy because there are only two of them this time, but if you have a large number of map chips, you need to set that much.

Collision Detection Settings

From here, it is almost the same as the content of "Implement hit detection with tilemap".

First, make the necessary settings for character collision detection. Add "Physics 2D -> Circle Collider 2D" as a component. This time, the sprite is round, so I made it a Circle, but please select Box or Capsule according to the shape.

We will also add "Physics 2D -> Rigidbody 2D".

Set the gravity scale to 0 to prevent gravity from falling down. Also, check Z for "Fix rotation" to prevent the sprite from rotating.

For Tilemap, add the Tilemap -> Tilemap Collider 2D component.

You can see that a green line has been added around the stone maptip in the view. This means that hit detection is set only for stones. You can see that the collider type setting of the maptip in the palette above is reflected.

Try running the game and walking towards the wall. I think you can make sure that the character stops in front of the wall properly. You can see that it can be easily implemented without writing any programs related to hit detection.

Combining MapChip hit detection

As mentioned in the tips below, when you actually move it, the behavior of wall hit detection is a little strange. The cause and workaround are described in the Tips below.

However, by combining the hit detection part of the map chip, the strange behavior will be corrected and the processing load will be reduced at the same time, so let's do it at a minimum.

First, check "Use in composite" in Tilemap Collider 2D.

Under Add Component, select Physics 2D -> Composite Collider 2D.

"Rigidbody 2D" will also be added, so make "Body Type" to "Static".

Then, I think you can confirm that the hit judgment of the stone is not for each map chip and is combined.

Now run the game and make sure it's working properly.