Unity / C#
Beginner
Unity 2021.3+ / Unity 6
2D Tilemap & RuleTile 자동 지형 배치와 CompositeCollider2D 콜라이더 병합2D Tilemap & RuleTile Automated Terrain Placement and CompositeCollider2D Merging
인접 타일 패턴에 따라 외벽/코너를 자동 타일링하는 RuleTile 에셋 구축, 수백 개 개별 타일 충돌체를 단일 지형 폴리곤으로 합쳐 물리 부하를 줄이는 최적화.Building RuleTile assets to auto-tile walls and corners based on neighbor patterns, and merging individual tile colliders into unified polygons with CompositeCollider2D.
#Unity #2D #Tilemap #RuleTile #CompositeCollider2D #Optimization
핵심 개념수작업으로 타일 모서리를 일일이 배치하면 레벨 디자인 작업 효율이 저하됩니다. RuleTile은 인접 8개 이웃 타일의 유무에 맞춰 조건에 맞는 스프라이트를 자동 선택합니다. 또한 개별 타일마다 생성되는 사각형 콜라이더를 CompositeCollider2D로 결합하면 경계선에 캐릭터 발이 걸리는 고스트 버텍스(Ghost Vertex) 현상이 해결됩니다.
Core ConceptPlacing tile edges and corners manually slows down level design workflows. RuleTile automatically selects the matching sprite based on the presence of its 8 neighboring tiles. Additionally, merging individual rectangular tile colliders with CompositeCollider2D resolves the Ghost Vertex issue where characters snag on seams between adjacent tiles.
1. 타일맵 콜라이더 병합 설정 가이드1. Tilemap Collider Merging Setup Guide
좌우로 밀어서 표 전체 내용을 확인하세요
| 컴포넌트 | 필수 옵션 설정 | 핵심 역할 |
|---|---|---|
| TilemapCollider2D | Used By Composite = 체크(True) | 개별 타일 사각형 충돌체를 부모 Composite에 제공 |
| CompositeCollider2D | Geometry Type = Polygons or Outlines | 인접한 모든 타일의 외곽선만 추출해 단일 폴리곤으로 병합 |
| Rigidbody2D | Body Type = Static | 합쳐진 대형 지형의 물리 연산 비용을 0으로 고정 |
Swipe horizontally to view the full table
| Component | Required Setting | Primary Role |
|---|---|---|
| TilemapCollider2D | Used By Composite = Checked (True) | Feeds individual tile box colliders into the parent Composite collider |
| CompositeCollider2D | Geometry Type = Polygons or Outlines | Extracts outer contours of adjacent tiles and merges them into unified polygons |
| Rigidbody2D | Body Type = Static | Fixes the physics simulation cost of the merged terrain to zero |
2. 커스텀 런타임 타일 교체 스크립트2. Custom Runtime Tile Modification Script
using UnityEngine;
using UnityEngine.Tilemaps;
public class DestructibleTilemap : MonoBehaviour
{
[SerializeField] private Tilemap targetTilemap;
[SerializeField] private TileBase destroyedTileParticle;
/// <summary>
/// 월드 좌표 위치의 타일을 파괴하고 물리 콜라이더를 즉시 갱신
/// </summary>
public void DestroyTileAtWorldPos(Vector3 worldPosition)
{
Vector3Int cellPos = targetTilemap.WorldToCell(worldPosition);
if (targetTilemap.HasTile(cellPos))
{
// 타일 제거
targetTilemap.SetTile(cellPos, null);
// CompositeCollider2D는 SetTile 호출 시 자동으로 외곽선을 재계산함
Debug.Log($"타일 파괴 완료: {cellPos}");
}
}
}
using UnityEngine;
using UnityEngine.Tilemaps;
public class DestructibleTilemap : MonoBehaviour
{
[SerializeField] private Tilemap targetTilemap;
[SerializeField] private TileBase destroyedTileParticle;
/// <summary>
/// Destroy the tile at the given world position and update the physics collider
/// </summary>
public void DestroyTileAtWorldPos(Vector3 worldPosition)
{
Vector3Int cellPos = targetTilemap.WorldToCell(worldPosition);
if (targetTilemap.HasTile(cellPos))
{
// Remove tile
targetTilemap.SetTile(cellPos, null);
// CompositeCollider2D automatically recalculates outlines when SetTile is called
Debug.Log($"Tile destroyed at: {cellPos}");
}
}
}
3. 고스트 버텍스(Ghost Vertex) 해결 원리3. How Ghost Vertex Snagging Is Resolved
- 개별 타일 콜라이더의 한계: 평평한 바닥에 놓인 사각형 타일들이 개별 박스 콜라이더를 가질 경우, 타일과 타일 사이의 미세한 버텍스 이음새에 플레이어의 캡슐/박스 콜라이더 모서리가 걸려 이동 중 멈추는 현상이 발생합니다.
- CompositeCollider 병합 효과:
CompositeCollider2D를 활성화하면 내부 수평선이 제거되고 수평 지면이 하나의 연속된 직선 에지(Edge)로 합쳐지므로 이음새 걸림 현상이 방지됩니다.
- Limitation of Individual Tile Colliders: When flat floor tiles each have their own box collider, the corners of a player's capsule or box collider can catch on microscopic vertex seams between tiles, causing sudden stops while moving.
- Effect of CompositeCollider Merging: Enabling
CompositeCollider2Dremoves internal shared edges and merges flat ground into a single continuous line segment, preventing seam collisions.
RuleTile을 통한 지형 자동 배치와 CompositeCollider2D 병합 설정을 함께 적용하면 레벨 제작 시간을 줄이면서 물리 연산 부하와 이동 걸림 문제를 동시에 예방할 수 있습니다.
Combining automated terrain placement via RuleTile with CompositeCollider2D merging reduces level authoring time while preventing physics overhead and collider seam snagging.