본문 바로가기

종스크롤 슈팅게임(1942)

10. 보조슈터 만들기

Player Bullet 프리펩을 누른 후 Ctrl + D로 프리펩 복사본 생성

인스펙터에서 Open Prepab을 눌러 이름과 스프라이트, 콜라이더 범위 수정

복사 후 이름과 스프라이트, 콜라이더 범위 수정
보조슈터 총알 프리펩 생성 완료

Follower 오브젝트 3개 생성 & 모두 비활성화

Follower 오브젝트 3개 생성 & 비활성화

Follower 스크립트 생성 및 Follower 오브젝트들에게 주기

Follower 스크립트 코드 입력

큐를 이용한 로직 구성

    //Vars
    public float maxShotDelay;
    public float curShotDelay;
    //ObjectManager;
    public ObjectManager objectManager;

    public Vector3 followPos;
    public int followDelay;
    public Transform parent;
    public Queue<Vector3> parentPos;

    void Awake()
    {
        parentPos = new Queue<Vector3>();
    }

    void Update()
    {
        watch();
        follow();
        fire();
        reload();
    }

    void watch() {
        //Input Position
        if (!parentPos.Contains(parent.position)) { 
            parentPos.Enqueue(parent.position);
        }
        //Output Position
        if (parentPos.Count > followDelay)
        {
            followPos = parentPos.Dequeue();
        }
        else if (parentPos.Count < followDelay) {
            followPos = parent.position;
        }
    }

    void follow() {
        transform.position = followPos;
    }

    void fire()
    {
        if (curShotDelay < maxShotDelay)
        {
            return;
        }

        //Bullet
        GameObject bullet = objectManager.makeObj("BulletFollower");
        bullet.transform.position = transform.position + new Vector3(0, 0.7f, 0);
        Rigidbody2D rigid = bullet.GetComponent<Rigidbody2D>();
        rigid.AddForce(Vector2.up * 10, ForceMode2D.Impulse);

        curShotDelay = 0;
    }

    void reload()
    {
        curShotDelay += Time.deltaTime;
    }

맵핑 

1번 Follower

맵핑 시 보조슈터가 줄줄이 플레이어를 따라가게 하기 위해, 2, 3번째 Follower은 Parent에 자기보다 앞 Follower을 넣어줍니다.

2번 Follower의 Parent는 1번 Follower을 할당
3번 Follower의 Parent는 2번 Follower을 할당

 

Player 스크립트 코드 추가

플레이어의 파워가 4, 5, 6이 될때마다 Follower 하나씩 활성화되는 로직

       public GameObject[] followers; //Follower 오브젝트를 담을 배열
   
   void fire() {
		...
        switch (power) {
		...
            case 3:
            case 4:
            case 5:
            case 6:
			...
        }

        curShotDelay = 0;
    }
    
        void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag == "Border")
        {
			...
        }
        else if (collision.gameObject.tag == "Enemy" || collision.gameObject.tag == "EnemyBullet")
        {
			...
        }
        else if (collision.gameObject.tag == "Item") {
            Item item = collision.gameObject.GetComponent<Item>();
            switch (item.type) {
                case "Coin":
                    score += 1000;
                    break;
                case "Power":
                    if (power == maxPower)
                    {
                        score += 500;
                    }
                    else {
                        power += 1;
                        addFollower(); //보조슈터 활성화 함수 호출
                    }
                    break;
                case "Boom":
                    if (boom == maxBoom)
                    {
                        score += 500;
                    }
                    else { 
                        boom += 1;
                    }
                    gm.updateBoomIcon(boom);
                    break;
            }
            collision.gameObject.SetActive(false);
            //Destroy(collision.gameObject);
        }
    }
    
        void addFollower() {
        if (power == 4) {
            followers[0].SetActive(true);
        }else if(power == 5)
        {
            followers[1].SetActive(true);
        }
        else if (power == 6)
        {
            followers[2].SetActive(true);
        }
    }

Player 오브젝트에 Follower 오브젝트들을 맵핑

결과

파워가 3인 상태로 시작