'드래곤 플라이트 만들기'에 해당되는 글 1건

  1. 2019.12.08 2019.12.08 드래곤플라이트 과제

2019.12.08 드래곤플라이트 과제

C#/과제 2019. 12. 8. 01:37

 

코드:

 

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
101
102
103
104
105
106
107
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class App : MonoBehaviour
{
    public Ship ship;
    public Button CreateEnemy;
    public Button CreateShip;
    private bool enemyExist = false;
    private GameObject enemyPrefab;
    public static GameObject bulletPrefab;
    private GameObject shipPrefab;
    // Start is called before the first frame update
    private void Awake()
    {
        bulletPrefab = Resources.Load<GameObject>("fireball-blue-med");
        enemyPrefab = Resources.Load<GameObject>("ship1");
        shipPrefab = Resources.Load<GameObject>("ship2");
    }
    void Start()
    {
        CreateShip.onClick.AddListener(() =>
        {
            if(GameObject.Find("Ship"== null)
            {
                GameObject shipGO = new GameObject("Ship");
                ship = shipGO.AddComponent<Ship>();
                var model = Instantiate(shipPrefab);
                ship.transform.position = new Vector3(0-1.237f, -1.7045f);
                model.transform.SetParent(shipGO.transform, false);
                model.transform.localPosition = Vector3.zero;
                model.transform.localRotation = Quaternion.Euler(new Vector3(00, 180f));
            }
        });
        CreateEnemy.onClick.AddListener(() => //enemy 생성
        {
            if(!enemyExist)
            {
                float x = 1;
                for (int i = 0; i<5; i++)
                {
                    
                    GameObject enemyGO = new GameObject("enemy");
                    Enemy enemy = enemyGO.AddComponent<Enemy>();
                    enemyGO.transform.position = new Vector3(x, 3.38f, -1.7045f);
                    var model = Instantiate(enemyPrefab);
                    model.transform.SetParent(enemyGO.transform, false);
                    model.transform.localPosition = Vector3.zero;
                    enemy.model = model;                    
                    enemy.Attack();
                    x -= 0.5f;
                }
                enemyExist = true;
                
            }
        });
    }
 
    // Update is called once per frame
    void Update()
    {        
        if(GameObject.Find("enemy"== null)
        {
            enemyExist = false;
        }        
        if(ship != null)
        {
            if (Input.GetKey(KeyCode.LeftArrow))
            {
                if (ship.transform.position.x >= -1.96f)
                {
                    ship.transform.position += Vector3.left * 2 * Time.deltaTime;
                }
            }
            if (Input.GetKey(KeyCode.RightArrow))
            {
                if (ship.transform.position.x <= 1.96f)
                    ship.transform.position += Vector3.right * 2 * Time.deltaTime;
            }
            if (Input.GetKey(KeyCode.UpArrow))
            {
                if (ship.transform.position.y <= 4.4f)
                    ship.transform.position += Vector3.up * 2 * Time.deltaTime;
            }
            if (Input.GetKey(KeyCode.DownArrow))
            {
                if (ship.transform.position.y >= -1.7f)
                    ship.transform.position += Vector3.down * 2 * Time.deltaTime;
            }
            if (Input.GetKeyDown(KeyCode.Space))
            {
                GameObject bulletGO = new GameObject("Bullet");
                bulletGO.transform.position = ship.bulletInitPoint.transform.position;
                Bullet bullet = bulletGO.AddComponent<Bullet>();
                var model = Instantiate(App.bulletPrefab);
                model.transform.SetParent(bulletGO.transform, false);
                model.transform.localPosition = Vector3.zero;
                bullet.GoTo();
            }
        }
        
         
    }
}
 
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
 
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Ship : MonoBehaviour
{
    public GameObject bulletInitPoint;
 
    // Start is called before the first frame update
    void Start()
    {
        bulletInitPoint = new GameObject("bulletInitPoint");
        bulletInitPoint.transform.SetParent(this.transform, false);
        bulletInitPoint.transform.localPosition = new Vector3(00.4f, 0);
        BoxCollider boxCollider = this.gameObject.AddComponent<BoxCollider>();
        Rigidbody rigidBody = this.gameObject.AddComponent<Rigidbody>();
        boxCollider.isTrigger = true;
        boxCollider.size = new Vector3(0.153413f, 0.46f, 1f);
        rigidBody.useGravity = false;
        rigidBody.isKinematic = true;
    }
    private void OnTriggerEnter(Collider other)
    {
        Destroy(this.gameObject);
    }
 
    // 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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Enemy : MonoBehaviour
{
    public Vector3 target;
    public GameObject model;
    private GameObject bulletInitPoint;
    // Start is called before the first frame update
    private void Awake()
    {
        bulletInitPoint = new GameObject("bulletInitPoint");
        bulletInitPoint.transform.SetParent(this.transform, false);
        bulletInitPoint.transform.localPosition = new Vector3(0-0.25f, 0);
        BoxCollider boxCollider = this.gameObject.AddComponent<BoxCollider>();
        Rigidbody rigidBody = this.gameObject.AddComponent<Rigidbody>();
        boxCollider.isTrigger = true;
        boxCollider.size = new Vector3(0.3373507f, 0.34f, 0.2f);
        rigidBody.useGravity = false;
        rigidBody.isKinematic = true;
    }
    private void OnTriggerEnter(Collider other)
    {
        Destroy(this.gameObject);
    }
    void Start()
    {
        
    }
    public void Attack()
    {
        StartCoroutine(AttackC());
    }
    IEnumerator AttackC()//자동공격
    {
        while(true)
        {
            if (GameObject.Find("Ship"!= null)
            {
                //bullet 생성                                             
                GameObject bulletGO = new GameObject("Bullet");
                bulletGO.transform.position = bulletInitPoint.transform.position;
                Bullet bullet = bulletGO.AddComponent<Bullet>();
                var model = Instantiate(App.bulletPrefab);
                model.transform.SetParent(bulletGO.transform, false);
                model.transform.localPosition = Vector3.zero;
                target = GameObject.Find("Ship").transform.position;
                bullet.GoTo(target);//발사(비유도탄)   
            }
            yield return new WaitForSeconds(1f);
        }
    }
    // 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
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
 
public class Bullet : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        BoxCollider boxCollider = this.gameObject.AddComponent<BoxCollider>();
        Rigidbody rigidBody = this.gameObject.AddComponent<Rigidbody>();
        boxCollider.isTrigger = true;
        boxCollider.size = new Vector3(0.07f, 0.07f, 0.2f);
        rigidBody.useGravity = false;
        rigidBody.isKinematic = true;
        this.tag = "bullet";
    }
    public void GoTo(Vector3 target)
    {        
        StartCoroutine(GoToC(target));
    }
    public void GoTo()
    {
        StartCoroutine(GoToC());
    }
    IEnumerator GoToC()
    {        
        while (true)
        {
            this.transform.position += this.transform.up * 3 * Time.deltaTime;
            if (this.transform.position.x <= -1.96f || this.transform.position.x >= 1.96f || this.transform.position.y >= 4.4f || this.transform.position.y <= -1.7f)
            {
                Destroy(this.gameObject);
            }
            
            yield return null;
        }        
    }
    IEnumerator GoToC(Vector3 target)
    {
        var vc = (target - this.transform.position).normalized;
        while(true)
        {
            this.transform.position += vc * 1 * Time.deltaTime;
            if (this.transform.position.x <= -1.96f || this.transform.position.x >= 1.96f || this.transform.position.y >= 4.4f || this.transform.position.y <= -1.7f)
            {
                Destroy(this.gameObject);
            }
            //if (Vector3.Distance(target, this.transform.position) <= 0.01f)
            //{
            //    Destroy(this.gameObject);
            //}
            yield return null;
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        if(other.gameObject.tag != "bullet")
            Destroy(this.gameObject);
 
        
        
    }
 
    // Update is called once per frame
    void Update()
    {
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
 

'C# > 과제' 카테고리의 다른 글

12.16 토글 버튼 만들기  (0) 2019.12.16
: