맵을 한갈래의 길로 바꾸는데 

처음 구상한 게임이 풍선타워디펜스처럼 맵은 그대로유지하되 스테이지를 추가하고싶어서

맵을 최대한 꼬불꼬불하면서도 타워 지을 공간 적당히 있게? 나름 만들어봤다.



그 과정에서 길을 구성하는데는 최소 3줄이 필요하다보니 너무 맵이 좁아서 기본 맵의 테두리를 전부 없애기로했다.

(이렇게 만들면 좋겠다 생각해본 경로)


(실제로 만든 경로)

만들어보니까 UI가 들어갈 공간도 필요할거 같아서 맵을 전체적으로 올리기로 하였다.

근데 길이 최소 3칸씩 잡아먹다보니 결국엔 최대 1칸밖에 못올리고 이렇게 완성.





UI 공간을 집어넣어보니까 너무 좁아서 결국 해상도를 조절해서 1줄을 더 집어넣었다.

원래 해상도는 1280 x 768 (20줄 x 12줄)이었는데 1280 x 832 (20줄 x 13줄)로 늘렸다.

마음같아서는 16:9(1280 x 720)나 16:10(1280 x 800)비율로 만들고 싶었지만 딱 맞게 떨어지진 않아서 

최대한 비슷하게하려고 1줄까지만 더늘렸다.






이 후 몬스터를 생성하고 알아서 길을 따라가게 만들었다.



EnemyManager를 만들어 일단은 0.5초 주기로 길의 시작위치에 몬스터를 계속 생성하도록 하였다.

현재까지는 은색 박쥐와 금색 박쥐 2종류.

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
32
33
34
35
36
37
38
public class EnemyMaker : MonoBehaviour {
 
    public GameObject Knight = null;
    public GameObject SilverBat = null;
    public GameObject GoldBat = null;
 
    private float regenTime = 0;
    private int count = 0;
    // Use this for initialization
    void Start () {
        
    }
    
    // Update is called once per frame
    void Update () {
        regenTime += Time.deltaTime;
        if(regenTime > 0.5f)
        {
            Vector3 startpos = new Vector3(TileMaker.startX + MakePath.movePath[0].x * 0.64f, TileMaker.startY + MakePath.movePath[0].y * 0.64f);
 
            if (count < 20)
            {
                Instantiate(SilverBat, startpos, Quaternion.identity);
                count++;
            }
            else if (count < 40)
            {
                Instantiate(GoldBat, startpos, Quaternion.identity);
                count++;
            }
            else
            {
                count = 0;
            }
            regenTime = 0.0f;
        }
    }
}
cs


몬스터가 이동하는 스크립트는 아래와 같다.

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
public class Mob1 : MonoBehaviour {
 
    private List<MakePath.MovePath> movePath = null;
    private int nowCount = 0;
    private int maxCount = 0;
    private float fTime = 0;
 
    private float footheight = 0.3f;
    private Vector3 nowPos = Vector3.zero;
    private Vector3 targetPos = Vector3.zero;
 
    // Use this for initialization
    void Start () {
        //movePath = GameObject.FindGameObjectWithTag("TileManager").GetComponent<MakePath>().movePath;
        movePath = MakePath.movePath;
        maxCount = movePath.Count;
    }
    
    // Update is called once per frame
    void Update () {
        // 이동속도 설정
        fTime += Time.deltaTime * 3.0f;
 
        // 현재방향과 다음방향을 잡음
        nowPos = new Vector3(movePath[nowCount].x, movePath[nowCount].y, 0);
        targetPos = new Vector3(movePath[nowCount + 1].x, movePath[nowCount + 1].y, 0);
 
        // Lerp 함수를 이용하여 moveTime이 증가하면서 그에 맞게 움직이도록 함.
        float x = Mathf.Lerp(TileMaker.startX + nowPos.x * 0.64f, TileMaker.startX + targetPos.x * 0.64f, fTime);
        float y = Mathf.Lerp(TileMaker.startY + nowPos.y * 0.64f + footheight, TileMaker.startY + targetPos.y * 0.64f + footheight, fTime);
        transform.position = new Vector3(x, y, 0);
 
        // 다음 위치에 도달하면 nowCount를 ++하여 다음 위치로 변경
        if (fTime >= 1.0f)
        {
            fTime = 0;
            nowCount++;
 
            if (nowCount >= maxCount - 1)
            {
                Destroy(gameObject);
            }
        }     
    }
}
 
cs

MakePath에서 길을 만든 List를 static으로 정의하여 여기서는 그 List를 받아와서 순서대로 길을 따라가도록 했다.

Mathf.Lerp함수를 이용하여 1초동안 다음위치까지 서서히 이동하고 

1초뒤에는 다음위치를 넘겨줘서 그다음위치로 가도록. 마지막 위치에 도달하면 스스로 죽도록 만들었다.


footheight 변수는 몬스터이미지의 중점이 길의 중점에 맞춰지니 조금 어색하게 느껴져서

몬스터의 중앙이 아닌 발 위치가 길의 중앙으로 가도록 y축을 조금 더 올려준 것이다.

이렇게 기본적으로 몬스터 생성과 이동은 완성하였다.

Posted by misty_
,