본문 바로가기

종스크롤 슈팅게임(1942)

1. 플레이어 이동 & 맵 경계선 설정

사용한 에셋 : https://assetstore.unity.com/packages/2d/characters/vertical-2d-shooting-assets-pack-188719

 

Vertical 2D Shooting Assets Pack | 2D 캐릭터 | Unity Asset Store

Elevate your workflow with the Vertical 2D Shooting Assets Pack asset from Goldmetal. Find this & more 캐릭터 on the Unity Asset Store.

assetstore.unity.com

플레이어 생성 및 기본 움직임 구현

Background Grid는 그냥 배경 오브젝트로, Layer in order을 -1로 설정해 제일 뒤에 놓습니다.

스크립트와 BoxCollider2D, Rigidbody2D도 주기

public float speed;
    
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        
        float v = Input.GetAxisRaw("Vertical");
       
        Vector3 curPos = transform.position;
        Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;

        transform.position = curPos + nextPos; 
    }

 

 

이제 빈 오브젝트를 이용해 맵 경계선을 만들어줍니다.

플레이어가 경계선에 닿았을 때 인식하기 위해, isTrigger을 체크해주고, Rigidbody는 다른 요소의 영향을 받지 않도록 Static으로 설정해줍니다.

 

경계선에 닿았다면 플레이어의 이동 값을 0으로 만들어 플레이어가 경계선 밖으로 나가지 못하도록 구현합니다.

    //Components
    Animator animator;
    //Vars
    public float speed;
    public bool isTouchTop;
    public bool isTouchBottom;
    public bool isTouchLeft;
    public bool isTouchRight;


    void Awake()
    {
        animator = GetComponent<Animator>();
    }
    void Update()
    {
        float h = Input.GetAxisRaw("Horizontal");
        if ((isTouchLeft && h == -1) || (isTouchRight && h == 1)) h = 0;
        float v = Input.GetAxisRaw("Vertical");
        if ((isTouchTop && v == 1) || (isTouchBottom && v == -1)) v = 0;
        Vector3 curPos = transform.position;
        Vector3 nextPos = new Vector3(h, v, 0) * speed * Time.deltaTime;

        transform.position = curPos + nextPos;

        if (Input.GetButtonDown("Horizontal") || Input.GetButtonUp("Horizontal")) {
            animator.SetInteger("Direction", (int)h);
        }
    
    
    
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Border") {
            switch (collision.gameObject.name) {
                case "Top":
                    isTouchTop = true;
                    break;
                case "Bottom":
                    isTouchBottom = true;
                    break;
                case "Left":
                    isTouchLeft = true;
                    break;
                case "Right":
                    isTouchRight = true;
                    break;
            }
        }
    }

    void OnTriggerExit2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Border")
        {
            switch (collision.gameObject.name)
            {
                case "Top":
                    isTouchTop = false;
                    break;
                case "Bottom":
                    isTouchBottom = false;
                    break;
                case "Left":
                    isTouchLeft = false;
                    break;
                case "Right":
                    isTouchRight = false;
                    break;
            }
        }
    }
}

OnTriggerEnter2D를 통해 어느 경계선에 콜라이더가 중첩됬는지 알아내고, 중첩된 상태에서 해당 경계선 방향으로 키를 누르고 있다면 이동 값을 0으로 만들어 플레이어의 이동을 제한합니다.

또한, OnTriggerExit2D로 경계선에서 떨어졌을 때 isTouch 변수를 다시 false로 초기화 해줍니다.

 

이제 좌우 이동 애니메이션을 넣어줍니다.

좌우 이동키를 누르거나, 뗐을 때, 애니메이션 용 변수(Direction)에 getAxisRaw값을 주어서, 애니메이션의 전환이 있도록 합니다.

 

마지막으로 Game 탭의 종스크롤을 위한 세로 화면을 만들기 위해 아래와 같이 설정합니다.

Label : 프리셋 이름

Aspect Ratio : 화면 비율 예)16:9

Fixed Resolution : 해상도 예 ) 1920x1080

Aspect Ratio 9:16 혹은 9:19로 만들어 설정해줍니다.  

 

결과