ãUnityã2Dã¿ã¤ã«ããã⨠Isometricãªãããã§å»ºç©ã®ä¸ãéãã»ä¸ãããããåç·¨ã
Unity 2020.2.1f1 Personal(2021å¹´3æ)
前回ã®ç¶ãã§ãï¼
ä»åã¯2Dã¿ã¤ã«ãããã§Isometricãªããããä½æã建ç©ã®ä¸ãéã£ããä¸ãããã£ãããã¾ãã
1éãæ©ãã¦ããæã«2éã®Colliderã«è¡çªãã¦ãã¾ãã®ãåé¿ãã¦ããã¾ãã
ãã®è¨äºã¯Unity 公式のIsometric 2DTilemapプロジェクトãä¸é¨åèã«ãã¦ãã¾ãã
Transparency Sort Modeã®ç·¨é
ã¾ãã¯Tilemapã®Isometric Z as Yã使ç¨ããéã®è¨å®ããã¦ããã¾ãã
Unityã¡ãã¥ã¼ããEdit > Project SettingsãéããGraphics > Camera Settingsã«ãã
Transparency Sort ModeãCustom Axisã«ã
Transparency Sort AxisãX:0 Y:1 Z: -0.49ã«å¤æ´ãã¾ãã
ç¶ãã¦ä¸è¨ã®è¨å®ãEditorã¢ã¼ãã§å¸¸ã«æå¹ã«ããScriptãä½æãã¾ãã
using UnityEngine; #if UNITY_EDITOR using UnityEditor; #endif [ExecuteInEditMode] public class AxisDistanceSortCameraHelper : MonoBehaviour { void Start() { var camera = GetComponent<Camera>(); camera.transparencySortMode = TransparencySortMode.CustomAxis; camera.transparencySortAxis = new Vector3(0.0f, 1.0f, -0.49f); #if UNITY_EDITOR foreach (SceneView sv in SceneView.sceneViews) { sv.camera.transparencySortMode = TransparencySortMode.CustomAxis; sv.camera.transparencySortAxis = new Vector3(0.0F, 1.0F, -0.49F); } #endif } }
AxisDistanceSortCameraHelper.cs
AxisDistanceSortCameraHelper.csã®è§£èª¬ã¯éå»è¨äºããåç §ãã ããã
AxisDistanceSortCameraHelper.csãä½æãããMain Cameraã«åãä»ãã¾ãã
Playerã®ä½æ
ç¶ãã¦Playerãä½æãã¦ããã¾ãã
ãã¡ãã®éå»è¨äºã¨ã»ã¨ãã©åãä½ãæ¹ã§ããIsometricç¨ã«å°ãä¿®æ£ãã¦ãã¾ãã
ã¾ãã¯ãã¡ãã®SpriteããPlayerãªãã¸ã§ã¯ããä½æãã¾ãã
slime.png
Playerãªãã¸ã§ã¯ãã«ãPlayerãã¨ããååã®ã¿ã°ãã¤ãã
CircleCollder2Dã³ã³ãã¼ãã³ãã¨Rigidbody2Dã³ã³ãã¼ãã³ãã以ä¸ã®è¨å®ã§åãä»ãã¾ãã
ç¶ãã¦ä¸ã®Scriptãä½æãã¦Playerãªãã¸ã§ã¯ãã«åãä»ãã¾ãã
using System.Collections; using System.Collections.Generic; using UnityEngine; public class Player : MonoBehaviour { private readonly float speed = 0.03f; public void MoveUp() { transform.position = new Vector3(transform.position.x + speed, transform.position.y + (speed / 2f), transform.position.z); } public void MoveDown() { transform.position = new Vector3(transform.position.x - speed, transform.position.y - (speed / 2f), transform.position.z); } public void MoveLeft() { transform.position = new Vector3(transform.position.x - speed, transform.position.y + (speed / 2f), transform.position.z); } public void MoveRight() { transform.position = new Vector3(transform.position.x + speed, transform.position.y - (speed / 2f), transform.position.z); }
Player.cs
ç¶ãã¦PlayerController.csãä½æãã¾ãã
using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerController : MonoBehaviour { [SerializeField] Player _player; [SerializeField] Transform _camera; void Update() { if (Input.GetKey(KeyCode.A)) _player.MoveLeft(); else if (Input.GetKey(KeyCode.W)) _player.MoveUp(); else if (Input.GetKey(KeyCode.D)) _player.MoveRight(); else if (Input.GetKey(KeyCode.S)) _player.MoveDown(); } private void LateUpdate() { if (!_camera) return; if (_camera.position.x != _player.transform.position.x || _camera.position.x != _player.transform.position.y) { _camera.position = new Vector3(_player.transform.position.x, _player.transform.position.y, -10f); } } }
PlayerController.cs
Controllerãªãã¸ã§ã¯ããä½æãã¦PlayerController.csãåãä»ãã¾ãã
ã¾ãInspectorãã_playerã«Playerãªãã¸ã§ã¯ãã_cameraã«Main Cameraãªãã¸ã§ã¯ããæå®ãã¾ãã
以ä¸ã§Playerã®ä½æã¯å®äºã§ãã
TilePaletteã®ä½æ(IsometricPalette)
ç¶ãã¦TilePaletteãä½æãã¦ããã¾ãã
åºæ¬çãªTilePaletteä½æã«ã¤ãã¦ã¯ãã¡ãã®éå»è¨äºããåç
§ä¸ããã
ã¾ãã¯IsometricPaletteã¨ãããã¬ãããä½æãã¾ãã
IsometricPaletteä½æ
ç¶ãã¦IsometricPaletteã«Tileãç»é²ãã¦ããã¾ãã
ä¸ã®3ã¤ã®Spriteã使ã£ã¦ããã¾ãã
field_block.png
stone_block.png
stone_stair.png
SpriteãUnityã«èªã¿è¾¼ã¿ã¾ãããåSpriteã®Inspectorã«ã¦ã
Pixels per Unitã32ãFilter ModeãPoint(no filter)ã«è¨å®ãã¾ãã
ã¾ãPivotãCustomãX:0.5 Y:0.25ã«è¨å®ãã¾ãã
Pivotã®ä½ç½®ãªã©ã«ã¤ãã¦ã¯ãã¡ãã®éå»è¨äºããåç
§ãã ããã
åSpriteã®è¨å®
Spriteã®è¨å®ãå®äºãããIsometricPaletteã«ãã©ãã°&ãããããã¦ç»é²ãã¾ãã
TileãIsometricPaletteã«ç»é²
ãã¬ããã«ç»é²ããæã«Spriteã¨ååã®Tileã¢ã»ãããä½æãããã¨æãã¾ãã
ä»åã¯ãããç¨ã¨Colliderç¨ã®Tilemapãåãã¦ä½æãã¦ããã¾ãã
ãã®ãããä»åä½æããTileã¢ã»ããã®Collider Typeã¯å
¨ã¦Noneã«å¤æ´ãã¾ãã
åTileã¢ã»ããã®Collider TypeãNoneã«å¤æ´
Tileãé ç½®ããï¼ãããï¼
ããã§ã¯ä½æããTileãé
ç½®ãã¦ããã¾ãã
HierarchyããCreate > 2D Object > Tilemap > Isometric Z as Yã§Tilemapãä½æãã¾ãã
ä½æããTilemapã®ååãFieldã«å¤æ´ããTIlemap Rendererã®ModeãIndividualã«å¤æ´ãã¾ãã
ããã§ã¯ã¾ãã¯Z Position:0ããTileãé
ç½®ãã¦ããã¾ãã
é«ããå¤ããTileã®é
ç½®ã«ã¤ãã¦ã¯ãã¡ãã®éå»è¨äºããåç
§ãã ããã
TilePaletteã®Z Positionã0ã«è¨å®ãã¾ãã
Z Position: 0
é
ç½®å®äº
ç¶ãã¦Z Positionã2ã«è¨å®ãã¦Tileãé ç½®ãã¾ãã
Z Position: 2
é
ç½®å®äº
ã¡ãªã¿ã«å³å´ã®é段ã¯Tileãå転ããã¦é
ç½®ãã¦ãã¾ãã
Tileã¯Sceneãã¥ã¼ä¸ã§ãShift + [ãã§å転ããããã¾ãã
ç¶ãã¦Z Positionã4ã«ãã¦Tileãé ç½®ãã¾ãã
Z Position: 4
é
ç½®å®äº
Z Positionã6ã«ãã¦Tileãé ç½®ãã¾ãã
Z Position: 6
é
ç½®å®äº
ã²ã¨ã¾ãä»åã¯ä»¥ä¸ã«ãªãã¾ãã
å¾ç·¨ã§ã¯Colliderç¨ã®Tilemapã®ä½æã¨Playerã®ç§»åã«ããCollideråæ¿ãã解説ãã¦ããã¾ãã
å¾ç·¨ãã©ãããããããé¡ããã¾ãã
ãããã¨ããããã¾ãããã