LoginSignup
2
4

More than 5 years have passed since last update.

【記録】Unity 2Dでゲームを作る 03[TilemapとMouse座標軸の連動]

Last updated at Posted at 2018-07-15

目的

Mouseでクリックしたワールド座標と
Gridで管理されているTilemapのワールド座標から
クリックされたTileの位置を特定したい。

クリックした座標と連動

手段

Mouse側

        // Vector3でマウス位置座標を取得する
        position = Input.mousePosition;
        // Z軸修正
        position.z = 10f;
        // マウス位置座標をスクリーン座標からワールド座標に変換する
        screenToWorldPointPosition = Camera.main.ScreenToWorldPoint(position);
        //tilemapに座標情報を渡す
        Tilemap.clickedPosition(screenToWorldPointPosition);

こんな感じでポインターの位置を特定

Tilemap側

    public void clickedPosition(Vector3 position) {
        Vector3Int clickPosition =  Tilemap.WorldToCell(position);
        Tilemap.SetTile(clickPosition, testTile);
    }

tilemapのref: WorldToCell関数を使ってワールド座標からセルの特定

結果

06.gif
今回はクリックしたタイルをそのままテスト用のタイルに変換するようにした

タイル表示がすいつくように

~作成中~

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4