12.06 드래곤플라이트 구현

C#/실습 2019. 12. 6. 17:16

코드:

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
91
92
93
94
95
96
97
98
99
100
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Ship : MonoBehaviour
{
    public Transform bulletInitPoint;
    public Vector3 targetV;
    public GameObject model;
    GameObject prefab;
    GameObject modelBullet;
 
    // Start is called before the first frame update
    void Start()
    {
        prefab = Resources.Load<GameObject>("Prefabs/fireball-blue-big");
    }
    public void Attack()
    {
        StartCoroutine("MakeBulletForward");
        StartCoroutine("MakeBulletSide1");
        StartCoroutine("MakeBulletSide2");
 
    }
    IEnumerator MakeBulletSide1()
    {
        while (true)
        {
            GameObject bulletGO = new GameObject("bullet");
            bulletGO.transform.position = bulletInitPoint.position;
            Bullet bullet = bulletGO.AddComponent<Bullet>();
            modelBullet = Instantiate(prefab);
 
            modelBullet.transform.SetParent(bulletGO.transform, false);
            modelBullet.transform.localPosition = Vector3.zero;
            bullet.MoveSide1();
            yield return new WaitForSeconds(1f);
        }
    }
    IEnumerator MakeBulletSide2()
    {
        while (true)
        {
            GameObject bulletGO = new GameObject("bullet");
            bulletGO.transform.position = bulletInitPoint.position;
            Bullet bullet = bulletGO.AddComponent<Bullet>();
            var model = Instantiate(prefab);
            model.transform.SetParent(bulletGO.transform, false);
            model.transform.localPosition = Vector3.zero;
            bullet.MoveSide2();
            yield return new WaitForSeconds(1f);
        }
    }
 
    IEnumerator MakeBulletForward()
    {
        while(true)
        {
            GameObject bulletGO = new GameObject("bullet");           
            bulletGO.transform.position = bulletInitPoint.position;
            Bullet bullet = bulletGO.AddComponent<Bullet>();
            var model = Instantiate(prefab);
            model.transform.SetParent(bulletGO.transform, false);
            model.transform.localPosition = Vector3.zero;
            bullet.Move();
            yield return new WaitForSeconds(1f);
        }
        
        
    }
    
    
    // Update is called once per frame
    void Update()
    {
        {
            if(this.transform.position.x >= -3.5f)
            {
                this.transform.position += Vector3.left * 2 * Time.deltaTime;
            }
        }
        {
            if (this.transform.position.x <= 3.5f )
                this.transform.position += Vector3.right * 2 * Time.deltaTime;
        }
        if (Input.GetKey(KeyCode.UpArrow))
        {
            if ( this.transform.position.y <= 6f)
                this.transform.position += Vector3.up * 2 * Time.deltaTime;
        }
        {
            if (this.transform.position.y >= -6f )
                this.transform.position += Vector3.down * 2 * Time.deltaTime;
        }
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Bullet : MonoBehaviour
{
    Coroutine coroutineMove;
    // Start is called before the first frame update
    void Start()
    {
    }
    public void Move()
    {
        coroutineMove = StartCoroutine("MoveC");
 
    }
    public void MoveSide2()
    {
        StartCoroutine("MoveSideC2");
        StartCoroutine("MoveC");
    }
    IEnumerator MoveSideC2()
    {
        while (true)
        {
            var speed = 2;
 
            this.transform.position += -this.transform.right * speed * Time.deltaTime;
            if (this.transform.position.y >= 6f || this.transform.position.x <= -3.6f || this.transform.position.x >= 3.6f)
            {
                Destroy(this.gameObject);
            }
            yield return null;
        }
    }
    public void MoveSide1()
    {
        StartCoroutine("MoveSideC1");
        StartCoroutine("MoveC");
    }
    IEnumerator MoveSideC1()
    {
        while (true)
        {
            var speed = 2;
 
            this.transform.position += this.transform.right * speed * Time.deltaTime;
            if (this.transform.position.y >= 6f || this.transform.position.x <= -3.6f || this.transform.position.x >= 3.6f)
            {
                Destroy(this.gameObject);
            }
            yield return null;
        }
    }
    IEnumerator MoveC()
    {
        while (true)
        {
            var speed = 2;
            this.transform.position += this.transform.up * speed * Time.deltaTime;
            if (this.transform.position.y >= 6f)
            {
                Destroy(this.gameObject);
            }
            yield return null;
        }
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class App2 : MonoBehaviour
{
    public Ship ship;
    public Button btnAttack;
    // Start is called before the first frame update
    void Start()
    {
        btnAttack.onClick.AddListener(() =>
        {
            ship.Attack();
        });
    }
 
    // Update is called once per frame
    void Update()
    {
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class BGScroller : MonoBehaviour
{
    private MeshRenderer render;
    private float offset;
    public float speed;
    // Start is called before the first frame update
    void Start()
    {
        render = GetComponent<MeshRenderer>();
    }
 
    // Update is called once per frame
    void Update()
    {
        offset += Time.deltaTime * speed;
        render.material.mainTextureOffset = new Vector2(0, offset);
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

참조:

무한 스크롤 배경:https://www.youtube.com/watch?v=asraLkuR3Jg

: