Unity2D

Unity) [유니티 2D] 탄환 발사 방향으로 조준선 표시하기

HSH12345 2023. 4. 27. 23:22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class LaserLine : FutureEquipment
{
    public GameObject laser;
    private List<GameObject> list = new List<GameObject>();

    public override void Init()
    {
        int bulletAmount = PlayerStats.instance.bulletAmountCharacteristic;
        if (bulletAmount >= 10) bulletAmount = 10;

        float rotationZ = 0;
        int bulletCnt = 0;
        bool isEven = false;

        switch (bulletAmount)
        {
            case 1:
                bulletCnt = 1;
                break;
            case 2:
                bulletCnt = 2;
                isEven = true;
                break;
            case 3:
                rotationZ = 20;
                bulletCnt = bulletAmount;
                break;
            case 4:
                rotationZ = 20;
                bulletCnt = bulletAmount - 2;
                isEven = true;
                break;
            case 5:
                rotationZ = 40;
                bulletCnt = bulletAmount;
                break;
            case 6:
                rotationZ = 40;
                bulletCnt = bulletAmount - 2;
                isEven = true;
                break;
            case 7:
                rotationZ = 60;
                bulletCnt = bulletAmount;
                break;
            case 8:
                rotationZ = 60;
                bulletCnt = bulletAmount - 2;
                isEven = true;
                break;
            case 9:
                rotationZ = 80;
                bulletCnt = bulletAmount;
                break;
            case 10:
                rotationZ = 80;
                bulletCnt = bulletAmount - 2;
                isEven = true;
                break;
            default:
                break;
        }

        foreach(var item in list)
        {
            Destroy(item);
        }
        list.Clear();

        GameObject go = null;
        if (isEven)
        {
            for (int i = 0; i < 2; i++)
            {
                go = Instantiate(this.laser, this.gunShell.bulletPoints[i].transform);
                go.transform.rotation = this.gunShell.lookRotation;
                list.Add(go);
            }
        }

        for (int i = 0; i < bulletCnt; i++)
        {
            if (bulletAmount == 2) continue;
            go = Instantiate(this.laser, this.gunShell.bulletPoint.transform);            
            var rotation = Quaternion.Euler(0, 0, rotationZ);

            go.transform.rotation = this.gunShell.lookRotation * rotation;
            if (isEven && rotationZ == 20) rotationZ = 0;
            rotationZ -= 20;
            list.Add(go);
        }
    }
}

 

 

 탄환 개수가 짝수일 때 모양이 좀 이상해 보이긴 하지만 정상적으로 기능하도록 구현하였습니다.