ãUnityã2Dã¿ã¤ã«ãããâ© Isometricãªãããã§å»ºç©ã®ä¸ãéãã»ä¸ãããããå¾ç·¨ã
Unity 2020.2.1f1 Personal(2021å¹´3æ)
前回ã®ç¶ãã§ãï¼
åç·¨ã§ã¯Tilemapã®é
ç½®ã¨Playerã®ä½æãè¡ã¾ããã
å¾ç·¨ã§ã¯Colliderç¨Tilemapã®ä½æã¨ãã¬ã¤ã¤ã¼ã®ä½ç½®ã«ãã£ã¦æå¹ãªColliderãåãæ¿ããæ¹æ³ã解説ãã¾ãã
ãã®è¨äºã¯Unity 公式のIsometric 2DTilemapプロジェクトãä¸é¨åèã«ãã¦ãã¾ãã
TilePaletteã®ä½æ(ColliderPalette)
ã¾ãã¯ColliderTilemapç¨ã®ãã¬ãããä½æãã¾ãã
ColliderPaletteã¨ããååã§TilePaletteãä½æãã¦ä¸ããã
ColliderPaletteä½æ
ç¶ãã¦Colliderã¨ãªãSpriteããã¬ããã«ç»é²ãã¦ããã¾ãã
以ä¸ã®ç»åãUnityã«ã¢ãããã¦ä¸ããã
collider1.png
collider2.png
collider3.png
collider4.png
collider5.png
collider6.png
åSpriteã®è¨å®ã¯ä»¥ä¸ã®éãã§ãã
ã¾ãSprite Editorã®Custom Physics Shapeã«ã¦åSpriteã®å½¢éãã«å²ã£ã¦ä¸ããã
collider1.pngã®ä¾
åSpriteã®è¨å®ãå®äºãããColliderPaletteã«ãã©ãã°&ãããããTileãç»é²ãã¦ããã¾ãã
Colliderç¨Tileãç»é²
Tilemapãä½æããï¼Colliderç¨Tileï¼
ãã¬ããã®è¨å®ãå®äºããã®ã§ç¶ãã¦Tileãé
ç½®ãã¦ããã¾ãã
ä»åã¯1éç¨ã»é段ç¨ã»2éç¨ã®Tilemapãä½æãã¾ãã
ã¾ãã¯1éç¨Tilemapã§ãã
HierarchyããCreate > 2D Object > Tilemap > Isometricã§Tilemapãä½æãã¦ä¸ããã
ååã¯ãCollider1fãã¨ãã¾ãã
Tilemapä½æ
ä½æããTilemapã®TilemapRendererã®ModeãIndividualã«ã
ã¾ãColliderã確èªããããããã«Sorting Layerã«ãColliderDebugããä½æãã¦è¨å®ãã¾ãã
ã¾ãColliderã確èªããããããã«è²ãéãããã³ã¯ã«ãã¾ãã
Projectãã¥ã¼ã«ã¦Create > Materialã§Materialãæ°è¦ä½æããååããColliderTilemapãã«ãã¾ãã
ä½æããMaterialã®InspectorãããShaderãSprite/Defaultã«ãTintããã³ã¯ã«ãã¦éæ度ã60%ç¨åº¦ã«ãã¾ãã
ä½æããMaterialãCollider1fã®TilemapRendererã®Materialã«è¨å®ãã¾ãã
Materialãè¨å®
ãã¨ã¯Collider1fãªãã¸ã§ã¯ãã«TilemapCollider2Dã¨CompositeCollider2Dãåãä»ãã¾ãã
TilemapCollider2Dã®Used byCompositeãtrueã«ã
èªåçã«åãä»ããããRigidbody2Dã®Body TypeãKinematicã«å¤æ´ãã¾ãã
TilemapCollider2D, Rigidbody2D, CompositeCollider2Dã®è¨å®
Tileãé ç½®ããï¼Colliderç¨Tileï¼
ããããTilemapã®æºåãå®äºãã¾ããã
ããã§ã¯Tileãé
ç½®ãã¦ããã¾ãã
1éã®Colliderãé
ç½®
ãã£ã¼ã«ãã®å¤ã«åºãªãããã«å²ã£ãã®ã¨ãé段ã®å¾ãå´ããããã¯ãã¾ããã
åãè¦é ã§é段ç¨ã®Tilemapãä½æãã¾ãã
åºæ¬çã«ã¯Collider1fã¨åãã§ãã
ã»ColliderStairã¨ããååã§TIlemapä½æ
ã»ModeãIndividualã«å¤æ´
ã»Materialã«ColliderTilemapãè¨å®
ã»Sorting LayerãColliderDebugã«è¨å®
ã»TilemapCollider2Dãåãä»ãUsed by Compositeãtrue
ã»Composite Collider2Dãåãä»ãã
ã»Rigidbody2Dã®Body TypeãKinematicã«è¨å®
é段ã®Colliderãé
ç½®
åãããã«Collider2fã¨ããååã®2éç¨Tilemapãä½æããTileãé ç½®ãã¦ããã¾ãã
2éã®Colliderãé
ç½®
以ä¸ã§Colliderç¨ã®Tilemapã®ä½æã¯å®äºã§ãã
Colliderç¨Tilemapã3ã¤ä½æ
ãã¬ã¤ã¤ã¼ã®ä½ç½®ã«ãã£ã¦æå¹ãªColliderãåãæ¿ãã
Colliderç¨TIlemapã®ä½æã¾ã§å®äºãã¾ããã
æå¾ã«ãã¬ã¤ã¤ã¼ã®ä½ç½®ã«ãã£ã¦æå¹ãªColliderãåãæ¿ããæ©è½ãå®è£
ãã¾ãã(1éã«ããæã¯1éã®Colliderãã2éã«ããæã¯2éã®Colliderãæå¹ã«ãã)
ã¾ãã¯MapController.csãä½æãã¾ãã
using System.Collections; using System.Collections.Generic; using UnityEngine; public class MapController : MonoBehaviour { public static MapController instance; [SerializeField] List<GameObject> floorList; private void Awake() { if (instance == null) { instance = this; } else { Destroy(this.gameObject); } } private void Start() { SetCurrentFloor(0); } public void SetCurrentFloor(int floor) { for (int i = 0; i < floorList.Count; i++) { floorList[i].SetActive(i == floor); } } }
MapController.cs
ãã¡ãã®ScriptãControllerãªãã¸ã§ã¯ãã«åãä»ãã¦ä¸ããã
ã¾ãInspectorããFloorListã3ã¤ç¨æããåColliderç¨ã®Tilemapãç»é²ãã¾ãã
Collider1f, ColliderStair, Collider2fã®é çªã§ç»é²ããã
SetCurrentFloorã¡ã½ãããå®è¡ããã¨
å¼æ°ã§è¨å®ããColliderç¨Tilemapãæå¹ã«ãªããã以å¤ã®Colliderç¨Tilemapãç¡å¹ã«ãªãã¾ãã
ç¶ãã¦éãåãæ¿ãããã¤ã³ãã¨ãªãFloorChangePointãªãã¸ã§ã¯ããä½æãã¦ããã¾ãã
空ãªãã¸ã§ã¯ããä½æãååããFloorChangePointãã«å¤æ´ãã¾ãã
ä½æããFloorChangePointãªãã¸ã§ã¯ãã¯Collider1fãªãã¸ã§ã¯ãã®åãªãã¸ã§ã¯ãã«ãã¾ãã
InspectorããPolygonCollider2Dã³ã³ãã¼ãã³ããåãä»ãã
PolygonCollider2Dã®Edit Colliderããä¸ã®ç»åã®ããã«é段ã®å
¥ãå£ã«Colliderãå½¢ä½ãã¾ãã
PolygonCollider2Dã®IsTriggerã¯trueã«ãã¦ããã¾ãã
æå¾ã«FloorChangePoint.csãä½æãFloorChangePointãªãã¸ã§ã¯ãã«åãä»ãã¾ãã
using System.Collections; using System.Collections.Generic; using UnityEngine; public class FloorChangePoint : MonoBehaviour { [SerializeField] int floorNum; private void OnTriggerExit2D(Collider2D collision) { if (collision.transform.tag == "Player") { MapController.instance.SetCurrentFloor(floorNum); switch (floorNum) { case 0: collision.transform.GetComponent<Player>().SetZPos(2f); break; case 1: case 2: collision.transform.GetComponent<Player>().SetZPos(8f); break; } } } }
FloorChangePoint.cs
OnTriggerExit2Dãå®è¡ãããã¿ã¤ãã³ã°ã¯å
ã»ã©ä½æããColliderãPlayerãªãã¸ã§ã¯ããå¤ã«åºãã¿ã¤ãã³ã°ã§ãã
å
ã»ã©ã®MapControllerã®SetCurrentFloorã¡ã½ãããå®è¡ããã¾ãã
å¼æ°ã®floorNumã¯FloorChangePointãªãã¸ã§ã¯ãã®Inspectorããè¨å®ã§ãã¾ãã
Collider1fãé表示ã«ãã¦ColliderStairã表示ãããå ´åã¯ãfloorNumã1ã«è¨å®ãããã¨ãã£ãæãã§ãã
switchæã§ã¯Playerã®position.zãå¤æ´ãã¦ãã¾ãã
ã¨ããããã§Player.csã«SetZPosã¡ã½ããã¨è¿½å ãã¦ããã¾ãã
Player.csã«SetZPosã¡ã½ããã追å
ããã§ã¯Player.csã«SetZPosã¡ã½ããã追å ä¿®æ£ãã¦ããã¾ãã
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); } // ***** 追å ***** public void SetZPos(float zPos) { transform.position = new Vector3(transform.position.x, transform.position.y, zPos); } // ***** 追å çµäº ***** }
Player.cs
SetZPosã¡ã½ããã¯åç´ã«Playerã®zPosãæ´æ°ããã ãã®ã¡ã½ããã§ãã
FloorChangePointãè¤æ°ä½æãã
ããã¾ã§ã§Playerãé段ãééãããã¨ããã¨Collider1fãç¡å¹ã«ãªããColliderStairãæå¹ã«ãªãããã«ãªãã¾ããã
åãããã«é段ãã2éã¸ä¸ãããã¤ã³ãã«FloorChangePointãªãã¸ã§ã¯ããç½®ãã¾ãã
FloorChangePointãPrefabåããColliderStairãªãã¸ã§ã¯ãã®åãªãã¸ã§ã¯ãã«ãã¾ãã
ColliderStairãé表示ã«ãCollider2fã表示ããããã®ã§ã
FloorChangePoint.csã®floorNumã2ã«ãã¾ãã
ä½ç½®ã¨ãã¦ã¯é段ã¨2éã®éã§ãã
é段ã¨2éã®éã«Collideré
ç½®
ç¶ãã¦2éããé段ã¸ç§»åããFloorChangePointãªãã¸ã§ã¯ããä½æãã¾ãã
Collider2fãªãã¸ã§ã¯ãã®åãªãã¸ã§ã¯ãã¨ãã¦FloorChangePointãªãã¸ã§ã¯ããä½æããFloorChangePoint.csã®floorNumã1ã«ãã¾ãã
ä½ç½®ã¨ãã¦ã¯é段ã¨2éã®éã§ãããå
ã»ã©ä½æãã2éã«åãæ¿ããFloorChangePointãªãã¸ã§ã¯ãããé段å¯ãã«é
ç½®ãã¾ãã
é段ã¨2éã®éã«Collideré
ç½®
æå¾ã«é段ãã1éã¸ç§»åããFloorChangePointãªãã¸ã§ã¯ããä½æãã¾ãã
ColliderStairãªãã¸ã§ã¯ãã®åãªãã¸ã§ã¯ãã«ä½æããfloorNumã0ã«ãã¾ãã
æåã«ä½æããFloorChangePointãªãã¸ã§ã¯ããã1éå¯ãã«é
ç½®ãã¾ãã
1éã¨é段ã®éã«Collideré
ç½®
以ä¸ã§å·¦å´ã®é段ã®FloorChangePointã®ä½æãå®äºãã¾ããã
å³å´ã®é段ãåæ§ã®æé ã§FloorChangePointãé
ç½®ãããä½æ¥å®äºã§ãã
çµæ
ããã§ã¯çµæã確èªãã¦ããã¾ãã
ã¾ãã¯å»ºç©ã®ä¸ãããã£ã¦ã¿ã¾ãã
æå¹ã«ãªã£ã¦ããColliderã¯1éã®ã¿ãªã®ã§ã
å½ç¶å»ºç©ã®ä¸ãããããã¨ãã§ãã¾ãã
ç¶ãã¦å»ºç©ã®ä¸ãéãã¾ãã
1éãé段ã2éã¨ãã¬ã¤ã¤ã¼ã®ä½ç½®ã«ãã£ã¦æå¹ãªColliderãåæ¿ãã
ç¡äºå»ºç©ã®ä¸ãééãããã¨ãã§ãã¾ããã
ãªãã±ï¼HideTilemapRendererã¹ã¯ãªãã
ã²ã¼ã éå§æã«TilemapColliderã®è²ãæ¶ãHideTilemapRenderer.csãæå¾ã«æ²è¼ãã¾ãã
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Tilemaps; public class HideTilemapRenderer : MonoBehaviour { void Start() { GetComponent<TilemapRenderer>().enabled = false; } }
HideTilemapRenderer.cs
TilemapRendererã®enabledãfalseã«ãã¦è²ã ãæ¶ãã¦ãã¾ãã(Colliderã¯æ®ã£ã¦ãã¾ãã)
using UnityEngine.Tilemaps;
ã®è¨è¿°ãããªãã¨TilemapRendererã使ããªãã®ã§ã注æä¸ããã
ãã¡ãã®ScriptãCollider1f, ColliderStair, Collider2fãªãã¸ã§ã¯ãã«ã¤ãããã¨ã§ã²ã¼ã éå§æã«Colliderã®è²ãé ããã¨ãã§ãã¾ãã
ã¨ããããã§ä»åã¯ä»¥ä¸ã§ãã
ãããã¨ããããã¾ãããã