Shooting Tutorial 자습서 분해하고 재구성하기

Unity/Unity Tutorial 2019. 5. 2. 18:49


자습서 중 하나인 Space Shooter Tutorial의 코드를 공부하고 처음부터 나만의 코드로 다시 재구성 해보았다.



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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestShootingGame : MonoBehaviour
{
    public Transform[] arrGround;
    public GameObject[] meteors;
    public GameObject spaceShip;
    public GameObject objPool;
    public Vector3 spawnPosition;
    public float xMin, xMax, zMin, zMax;
    public float tilt;
    public float waveDelay, spawnDelay, meteorCount;
    private Rigidbody shipBody;
    private float speed = 5.0f;
    private int index = 0;
 
    // Start is called before the first frame update
    void Start()
    {
        this.shipBody = this.spaceShip.GetComponent<Rigidbody>();
    }
 
    // Update is called once per frame
    void Update()
    {
        for (int i = 0; i < this.arrGround.Length; i++)
        {
            this.arrGround[i].Translate(new Vector3(0-0.1f, 0* this.speed);
            if (this.arrGround[i].transform.localPosition.z <= -30)
            {
                this.arrGround[i].transform.localPosition = new Vector3(00, 60f);
            }
        }
        if(Input.GetMouseButtonDown(0))
        {
            var list = this.objPool.GetComponent<TestObjectPool>().listObj;
            print(list.Count);
            foreach(var bullet in list)
            {
                if(bullet.activeSelf == false)
                {
                    bullet.SetActive(true);
                    bullet.gameObject.GetComponent<TestBullets>().Init();
                    bullet.transform.position = GameObject.Find("bulletStartPoint").transform.position;
                    break;
                }
            }
        }
        this.Move();
        StartCoroutine(this.CreateWave());
    }
 
    private IEnumerator CreateWave()
    {
        yield return new WaitForSeconds(2.0f);
        while (true)
        {
            for (int i = 0; i < this.meteorCount; i++)
            {
                GameObject meteor = this.meteors[Random.Range(0, meteors.Length)];
                if(meteor.activeSelf == false)
                {
                    meteor.SetActive(true);
                    meteor.transform.GetComponent<TestMeteor>().Init();
                    meteor.transform.position = new Vector3(Random.Range(-this.spawnPosition.x, this.spawnPosition.x), this.spawnPosition.y, this.spawnPosition.z);
                    meteor.transform.rotation = Quaternion.identity;
                }
                yield return new WaitForSeconds(this.spawnDelay);
            }
        }
    }
 
    private void Move()
    {
        //Horizaontal = 수평, Virtical = 수직 (-1 ~ 1 return)
        float horizontal = Input.GetAxis("Horizontal");
        float virtical = Input.GetAxis("Vertical");
 
        var movement = new Vector3(horizontal, 0, virtical);
        this.shipBody.velocity = movement * this.speed;
 
        this.shipBody.position = new Vector3(Mathf.Clamp(this.shipBody.position.x, this.xMin, this.xMax),
                                             0, Mathf.Clamp(this.shipBody.position.z, this.zMin, this.zMax));
        this.shipBody.rotation = Quaternion.Euler(00this.shipBody.velocity.x * -this.tilt);
 
    }
}
 
cs



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestBGScroller : MonoBehaviour
{
    public Transform[] ground;
    public float speed = 2.0f;
 
    private void Update()
    {
        for (int i = 0; i<this.ground.Length; i++)
        {
            this.ground[i].Translate(new Vector3(0-0.1f, 0* this.speed);
            if(this.ground[i].transform.localPosition.z <= -30)
            {
                this.ground[i].transform.localPosition = new Vector3(00, 60f);
            }
        }
    }
}
 
cs


1
2
3
4
5
6
7
8
9
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestObjectPool : MonoBehaviour
{
    public List<GameObject> listObj;
}
 
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
47
48
49
50
51
52
53
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class TestMeteor : MonoBehaviour
{
    public float tumble;
    public float speed;
    public GameObject particle;
    private ParticleSystem fx;
 
    void Start()
    {
        this.fx = this.particle.GetComponentInChildren<ParticleSystem>();
        GetComponent<Rigidbody>().angularVelocity = Random.insideUnitSphere * this.tumble;
        GetComponent<Rigidbody>().velocity = transform.forward * this.speed;
    }
 
    public void Init()
    {
        StartCoroutine(this.CountDownToPassive());
    }
 
    private IEnumerator CountDownToPassive()
    {
        while(true)
        {
            if(this.transform.position.z < -14)
            {
                this.gameObject.SetActive(false);
                break;
            }
            yield return null;
        }
        
        //Debug.Log("countdonwtopassive");
        //yield return new WaitForSeconds(2.0f);
        //this.gameObject.SetActive(false);
        //yield return null;
    }
 
    private void OnTriggerEnter(Collider other)
    {
        if(!other.CompareTag("Enemy"))
        {
            this.gameObject.SetActive(false);
            this.fx.gameObject.SetActive(true);
            this.fx.transform.position = this.gameObject.transform.position;
            this.fx.Play();
        }
    }
}
 
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 System.Collections.Generic;
using UnityEngine;
 
public class TestBullets : MonoBehaviour
{
    public float speed;
 
    void Start()
    {
        GetComponent<Rigidbody>().velocity = transform.forward * speed;
    }
 
    public void Init()
    {
        StartCoroutine(this.TimeToOff());
    }
 
    private void OnTriggerEnter(Collider other)
    {
        this.gameObject.SetActive(false);
    }
 
    private IEnumerator TimeToOff()
    {
        yield return new WaitForSeconds(1.0f);
        this.gameObject.SetActive(false);
    }
}
 
cs


'Unity > Unity Tutorial' 카테고리의 다른 글

Tower Defense Template  (0) 2019.05.03
: