Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

짱이 될거야

Unity 3D Minimap: v2. Fixed map(2D) 본문

프로젝트

Unity 3D Minimap: v2. Fixed map(2D)

jeong57 2022. 10. 27. 17:21

결과 화면: Fixed Minimap

 

프로젝트 초기 설정

1.     프로젝트 생성

2.     Plane 생성 (2:1 비율 지도로 시작)

3.     Cube 생성 (움직이는 캐릭터 될 예정)

 

캐릭터 이동 설정

Live Map과 동일하게 MoveController.cs 생성 후 cube component에 적용

 

미니맵 초기 설정

1.     Canvas 생성

2.     Canvas > Raw Image (Minimap) 생성 후 지도 그림 넣기

 

미니맵 스크립트 작성

Minimap.cs 작성 후 cube component에 삽입

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Minimap : MonoBehaviour
{
    public RectTransform playerInMap;
    public RectTransform map2dEnd;
    public Transform map3dParent;
    public Transform map3dEnd;

    private Vector3 normalized, mapped;

    private void Update()
    {
        normalized = Divide(
                map3dParent.InverseTransformPoint(this.transform.position),
                map3dEnd.position - map3dParent.position
            );
        normalized.y = normalized.z;
        mapped = Multiply(normalized, map2dEnd.localPosition);
        mapped.z = 0;
        playerInMap.localPosition = mapped;
    }

    private static Vector3 Divide(Vector3 a, Vector3 b)
    {
        return new Vector3(a.x / b.x, a.y / b.y, a.z / b.z);
    }

    private static Vector3 Multiply(Vector3 a, Vector3 b)
    {
        return new Vector3(a.x * b.x, a.y * b.y, a.z * b.z);
    }
}

 

미니맵 플레이어, End 생성

1.     Canvas/Minimap > Create Empty (Player)

2.     Player Inspector > Add Component (Image), 빨간색 네모 만들어준다.

  • Inspector > shift+alt 누르고 왼쪽 하단으로 위치시킨다.

Canvas/Minimap/Player Inspector

3.     Canvas/Minimap > Create Empty (End)

4.     End Inspector > 우측 상단 위치하도록 position 설정

  • Inspector > shift+alt 누르고 오른쪽 상단으로 위치시킨다.

Canvas(2d) Minimap Components

Plane 위 위치 설정

-      미니맵을 canvas 정중앙에 배치시킨다면: plane의 정중앙이 (0, 0, 0)이 되도록 배치

-      미니맵을 canvas 좌측하단에 배치시킨다면: plane의 왼쪽 하단이 (0, 0, 0)이 되도록 배치

1.     Create Empty (Map)

  • 현재 미니맵이 canvas 정중앙에 있으므로 map의 position을 (0, 0, 0)으로 맞춰준다.

2.     Map > Create Empty (End)

  • Plane의 우측 상단으로 좌표를 맞춰준다.

Map, Map/End components(3d)

Player Minimap.cs의 각 변수에 component drag & drop

  • Player In Map: Canvas/Minimap/Player
  • Map 2d End: Canvas/Minimap/End
  • Map 3d Parent: Map
  • \Map 3d End: Map/End

Player에 Minimap.cs 파일 삽입 후 변수 설정

 

 

결과 화면

결과화면:

 

 

참고

https://www.youtube.com/watch?v=waZBbsfwSzQ&t=2s

Comments