12.09 드래곤플라이트 2 (이펙트 추가)
C#/Study 2019. 12. 9. 22:15
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
108
109
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class App : MonoBehaviour
{
public Ship ship;
public Button CreateEnemy;
public Button CreateShip;
private bool enemyExist = false;
private GameObject enemyPrefab;
public static GameObject bulletPrefab;
public static GameObject explosion;
private GameObject shipPrefab;
// Start is called before the first frame update
private void Awake()
{
}
void Start()
{
CreateShip.onClick.AddListener(() =>
{
{
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;
}
});
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;
x -= 0.5f;
}
enemyExist = true;
}
});
}
// Update is called once per frame
void Update()
{
{
enemyExist = false;
}
if(ship != null)
{
{
if (ship.transform.position.x >= -1.96f)
{
}
}
{
if (ship.transform.position.x <= 1.96f)
}
{
if (ship.transform.position.y <= 4.4f)
}
{
if (ship.transform.position.y >= -1.7f)
}
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;
}
}
}
}
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(0, 0.4f, 0);
BoxCollider boxCollider = this.gameObject.AddComponent<BoxCollider>();
Rigidbody rigidBody = this.gameObject.AddComponent<Rigidbody>();
boxCollider.isTrigger = true;
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;
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)
{
{
//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;
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
72
73
74
75
76
|
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;
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)
{
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)
{
{
GameObject explosionInst = Instantiate(App.explosion);
explosionInst.transform.position = this.gameObject.transform.position;
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
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class explosion : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
}
void DestroyGO()
{
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
|
explosion 스프라이트 애니메이션 만들기:
1. 스프라이트 png파일을 받아서 slice하여 sprite로 만든다.
2. gameobject를 하나 생성하여 Sprite Renderer 컴포넌트를 추가한 후에 Sprite에 생성한 explosion sprite를 드래그한다.
3. window창 -> animation으로 들어가 2번에서 만든 gameobject 선택후 클립 생성, sprite를 드래그하여 프레임을 씌운다.
4. 원하는 프레임을 골라 이벤트를 추가한다.
5. 스크립트를 추가해 2번에서 만든 gameobject에 추가한 후 메서드를 생성, 4번에서 만든 프레임을 선택해 실행될 메서드(스크립트에있는)를 선택한다.
6.다시 프리팹화 하여 추후에 사용한다.
'C# > Study' 카테고리의 다른 글
오브젝트 풀링 (인스턴스로 접근) (0) | 2019.12.16 |
---|---|
12.10 오브젝트 풀 활용 (0) | 2019.12.10 |
12.02 객체 충돌 (0) | 2019.12.02 |
struct 와 클래스의 차이 (0) | 2019.11.02 |
ref 와 out (0) | 2019.10.31 |