티스토리 뷰

코드 2-13, 2-14 를 구현하였습니다.


2차원 벡터를 회전시키는 확장 함수


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
using UnityEngine;
 
public static class MyExtension
{
     public static Vector2 Rotate(this Vector2 v2, float degrees) {
 
         float sin = Mathf.Sin(degrees * Mathf.Deg2Rad);
         float cos = Mathf.Cos(degrees * Mathf.Deg2Rad);
         
         float tx = v2.x;
         float ty = v2.y;
 
         v2.x = (cos * tx) - (sin * ty);
         v2.y = (sin * tx) + (cos * ty);
         
         return v2;
     }
}
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
using System.Collections.Generic;
using UnityEngine;
 
public class BulletPool : Singleton<BulletPool> {
 
    public GameObject bulletPref;
    public int bulletNum;
    private Stack<GameObject> bulletStack = new Stack<GameObject>();
 
    void Awake()
    {
        InitPool();
    }
 
    void InitPool()
    {
        for(int i = 0; i < bulletNum; i++)
        {
            PushBullet(CreateBullet());
        }
    }
 
    GameObject CreateBullet()
    {
        var gObj = Instantiate(bulletPref, transform);
        gObj.SetActive(false);
        return gObj;
    }
 
    public GameObject PopBullet()
    {
        if(bulletStack.Count == 0)
            PushBullet(CreateBullet());
        
        var gObj = bulletStack.Pop(); 
        gObj.SetActive(true);
        return gObj;    
    }
 
    public void PushBullet(GameObject bullet)
    {
        bullet.SetActive(false);
        bulletStack.Push(bullet);
    }
}
 
cs


N-Way 탄환 속도 벡터 구하는 클래스 


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
using UnityEngine;
 
public class InitNWayBullets : MonoBehaviour {
    
    //중심 탄환 속도 벡터
    [SerializeField]
    private Vector2 axisSpeedVector;
 
    //탄환과 탄환 사이의 각도
    [SerializeField]
    private float intervalTheta;
 
    //탄환의 수
    [SerializeField]
    private int bulletNum;
 
    //탄환들의 속도 벡터
    public Vector2[] bulletsSpeedVector;
 
    void Awake()
    {
        bulletsSpeedVector = new Vector2[bulletNum];
        float edgeDegree;
        
        //가장자리 탄환의 각도 계산
 
        //짝수
        if(bulletNum % 2 == 0)
        {
            edgeDegree = (- bulletNum / 2 + 0.5f) * intervalTheta;    
        }
        //홀수
        else
        {
            edgeDegree = - (bulletNum - 1/ 2 * intervalTheta;
        }
 
 
        for(int i = 0; i < bulletNum; i++ )
        {
            bulletsSpeedVector[i] = axisSpeedVector.Rotate(edgeDegree);
            edgeDegree += intervalTheta;
        }
    }
}
 
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
using System.Collections;
using UnityEngine;
 
public class BulletGenerator : MonoBehaviour {
 
    public InitNWayBullets initNWayBullets;
    public float time;
 
    WaitForSeconds waitForSeconds;
    
    IEnumerator Start()
    {
        waitForSeconds = new WaitForSeconds(time);
        while(true)
        {
            yield return null;
            for(int i = 0; i < initNWayBullets.bulletsSpeedVector.Length; i ++)
            {
                var gObj =  BulletPool.Instance.PopBullet();
                var bullet = gObj.GetComponent<Bullet>();
                bullet.speedVector = initNWayBullets.bulletsSpeedVector[i];
                bullet.axis = transform;
            }
            
            yield return waitForSeconds;
        }
    }
 
}
 
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
using UnityEngine;
 
public class Bullet : MonoBehaviour {
 
    public Transform axis;
    public Vector2 speedVector;
    
    void Update()
    {
        transform.Translate(speedVector * Time.deltaTime);
        OutOfScreen();
    }
 
    void OutOfScreen()
    {
        Vector2 viewportPos = Camera.main.WorldToViewportPoint(transform.position);
 
        if(viewportPos.x > 1 || viewportPos.x < 0|| viewportPos.y >1 || viewportPos.y <0 )
        {
            gameObject.SetActive(false);
            transform.position = axis.transform.position;
            BulletPool.Instance.PushBullet(this.gameObject);
        }
 
    }
 
}
 
cs


결과 영상



댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
TAG
more
«   2025/01   »
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
글 보관함