엘리트 몬스터의 스킬 패턴을 구현하던 중 원 형태로 전방위 투사체 발사를 구현하고자 하였다.
@@ 개의 투사체를 원 형태로 둥글게 발사 + 오브젝트 풀링을 활용하여 제작하고자 하였고, 단순히 특정 위치에서 Instantiate을 하는 것이 아니라 고르게 둥근 형태로 소환하는 방식을 한다면, 삼각함수를 활용할 필요가 있어 보였다.
IEnumerator MultiTimeProjectile(int skillTime,int projectileCount)
{
for (int i = 0; i < skillTime; i++)
{
if (target == null) yield break;// 타겟이 없으면 리턴
int count = 0;
float rotationOffset = Random.Range(0, 360f);// 랜덤 회전값
// 풀에 사용 가능한 투사체가 있는지 확인 후 발사
foreach (var projectile in projectilePool)
{
if (!projectile.activeSelf)
{
FireProjectile(projectile, count, rotationOffset);
count++;
if (count >= projectileCount) break;
}
}
// 부족한 투사체 생성
while (count < projectileCount)
{
GameObject newProjectile = Instantiate(monsterProjectile, transform.position, Quaternion.identity);
projectilePool.Add(newProjectile);
FireProjectile(newProjectile, count, rotationOffset);
count++;
}
voidCircle.transform.DOScale(1.35f, 0.2f).SetLoops(2, LoopType.Yoyo).SetEase(Ease.InOutQuad);// 발사 이펙트
yield return new WaitForSeconds(0.5f);
}
}
void FireProjectile(GameObject projectile, int index, float rotationOffset)// 투사체 발사(코드 재사용성을 위한 분리)
{
projectile.transform.position = transform.position;
projectile.SetActive(true);
float angle = (360f / 18f * index + rotationOffset) * Mathf.Deg2Rad; // 랜덤 회전값 추가 + 각도를 360도 / 18개 로 나누어 계산
projectile.GetComponent<Rigidbody2D>().velocity = new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * projectileSpeed;
}
float angle = 360f / 24f * index * Mathf.Deg2Rad;
projectile.GetComponent <Rigidbody2D>(). velocity
= new Vector2(Mathf.Cos(angle), Mathf.Sin(angle)) * projectileSpeed;
위 코드를 사용해서 둥근 형태로 발사체를 Instantiate 할 수 있다.
위는 원형 투사체 발사가 실제로 구현된 모습이다.
'C# > 게임 제작 + TIL' 카테고리의 다른 글
IDragHandler - UI 드래그 및 곡선 화살표 (0) | 2025.04.07 |
---|---|
Physics2D.OverlapPoint - 2D 상의 겹침의 정도 (0) | 2025.04.02 |
Particle System - 잔상 효과 (0) | 2025.03.31 |
소규모 개인 게임 회고 - Action의 남용에 대한 반성. (0) | 2025.03.26 |
비트 마스크 - LayerMask (0) | 2025.03.24 |