|
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;
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);
}
});
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);
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)
{
ship.transform.position += Vector3.left * 2 * Time.deltaTime;
}
}
{
if (ship.transform.position.x <= 1.96f)
}
{
if (ship.transform.position.y <= 4.4f)
ship.transform.position += Vector3.up * 2 * Time.deltaTime;
}
{
if (ship.transform.position.y >= -1.7f)
ship.transform.position += Vector3.down * 2 * Time.deltaTime;
}
{
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);
}
}
}
}
|
|
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()
{
}
}
|
|
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);
}
yield return new WaitForSeconds(1f);
}
}
// Update is called once per frame
void Update()
{
}
}
|
|
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)
{
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)
{
{
GameObject explosionInst = Instantiate(App.explosion);
explosionInst.transform.position = this.gameObject.transform.position;
Destroy(this.gameObject);
}
}
// Update is called once per frame
void Update()
{
}
}
|
|
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);
}
}
|
|
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()
{
}
}
|
|
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#/실습 2019. 12. 9. 17:25
코드:
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class App : MonoBehaviour
{
public Button btnShoot;
public Transform bulletInitPoint;
private GameObject bulletPrefab;
private Sprite explosionsprite;
// Start is called before the first frame update
void Start()
{
this.LoadResources();
this.btnShoot.onClick.AddListener(() =>
{
this.Shoot();
});
}
private void LoadResources()
{
}
private void Shoot()
{
GameObject bulletModel = Instantiate(bulletPrefab);
Bullet bullet = bulletModel.AddComponent<Bullet>();
bulletModel.transform.position = this.bulletInitPoint.position;
bullet.ShootCoroutine = bullet.ShootImple();
StartCoroutine(bullet.ShootCoroutine);
{
StopCoroutine(bullet.ShootCoroutine);
GameObject explosionGO = new GameObject("explosion");
explosionGO.transform.position = bullet.transform.position;
SpriteRenderer spriteRenderer = explosionGO.AddComponent<SpriteRenderer>();
Animator animator = explosionGO.AddComponent<Animator>();
animator.runtimeAnimatorController =
(RuntimeAnimatorController)RuntimeAnimatorController.Instantiate(
explosion explosion = explosionGO.AddComponent<explosion>();
Destroy(bullet.gameObject);
};
}
// Update is called once per frame
void Update()
{
}
}
|
|
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
{
public void whenExplode()
{
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Bullet : MonoBehaviour
{
public Action del;
public IEnumerator ShootCoroutine;
// Start is called before the first frame update
void Start()
{
}
//private void OnCollisionEnter(Collision collision)
//{
//}
private void OnTriggerEnter(Collider other)
{
{
this.del();
}
}
public IEnumerator ShootImple()
{
while (true)
{
this.transform.position += vc * 1 * Time.deltaTime;
yield return null;
}
}
// Update is called once per frame
void Update()
{
}
}
|
|
참조:
Sprite 파일
https://www.pngguru.com/free-transparent-background-png-clipart-zceay
스프라이트 애니메이션
https://m.blog.naver.com/PostView.nhn?blogId=heartybrain1&logNo=221142037112&proxyReferer=https%3A%2F%2Fwww.google.com%2F
코드에서 애니메이션 바꾸기
https://playground10.tistory.com/95
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()
{
}
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);
}
});
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);
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)
{
ship.transform.position += Vector3.left * 2 * Time.deltaTime;
}
}
{
if (ship.transform.position.x <= 1.96f)
}
{
if (ship.transform.position.y <= 4.4f)
ship.transform.position += Vector3.up * 2 * Time.deltaTime;
}
{
if (ship.transform.position.y >= -1.7f)
ship.transform.position += Vector3.down * 2 * Time.deltaTime;
}
{
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);
}
}
}
}
|
|
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);
}
}
|
|
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()
{
}
}
|
|
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);
}
yield return new WaitForSeconds(1f);
}
}
// Update is called once per frame
void Update()
{
}
}
|
|
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;
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)
{
Destroy(this.gameObject);
}
// Update is called once per frame
void Update()
{
}
}
|
|
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()
{
}
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);
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);
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);
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 )
}
{
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;
}
}
}
|
|
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;
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;
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()
{
}
}
|
|
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(() =>
{
});
}
// Update is called once per frame
void Update()
{
}
}
|
|
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);
}
}
|
|
참조:
무한 스크롤 배경:https://www.youtube.com/watch?v=asraLkuR3Jg
C#/실습 2019. 12. 5. 17:28
코드:
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class App2 : MonoBehaviour
{
public GameObject model1;
public GameObject model2;
public Button btn;
float angle;
// Start is called before the first frame update
void Start()
{
btn.onClick.AddListener(() =>
{
{
model1.transform.rotation = model1.transform.rotation * Quaternion.Euler(0, angle, 0);
}
else
{
model1.transform.rotation = model1.transform.rotation * Quaternion.Euler(0, -angle, 0);
}
});
}
// Update is called once per frame
void Update()
{
}
}
|
|
그냥 A 와 B사이의 벡터를 구하고 A가 바라보는 벡터의 각을 구하기엔 B가 A의 오른쪽에 있든 왼쪽에 있든 같은 angle값이 나오기때문에 구분이 불가능했다.
따라서 A의 right 벡터와 A,B사이 벡터의 내적을 구하고 이것이 0보다 큰지 작은지를 비교해 이를 해결했다.
C#/Study 2019. 12. 2. 23:44
예제: Hero 게임 오브젝트가 특정 지역으로 이동중일때 어떤 객체와 부딪히면 문자열을 출력한다.
코드:
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 UnityEngine;
public class App : MonoBehaviour
{
public Button btnMoveCoroutine;
public Button btnMoveUpdate;
public Hero hero;
public GameObject targetPoint;
// Start is called before the first frame update
void Start()
{
//GameObject heroGO = new GameObject("Hero");
//hero = heroGO.AddComponent<Hero>();
//var model = Instantiate(prefab);
//model.transform.SetParent(heroGO.transform, false);
{
var dis = Vector3.Distance(hero.transform.position, target.transform.position);
var vc = target.transform.position - hero.transform.position;
var dir = vc.normalized;
var speed = 1;
hero.transform.position += dir * speed * Time.deltaTime;
if (dis <= 0.5f)
{
hero.transform.position = target.transform.position;
}
};
btnMoveCoroutine.onClick.AddListener(() =>
{
hero.Move2(targetPoint);
});
btnMoveUpdate.onClick.AddListener(() => {
});
}
// Update is called once per frame
void Update()
{
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Hero : MonoBehaviour
{
public bool isMove;
public Action<GameObject> onMove;
public GameObject target;
public GameObject model;
IEnumerator onMoveCoroutine;
// Start is called before the first frame update
void Start()
{
onMoveCoroutine = OnMoveCoroutine();
}
public void Move(GameObject target)
{
this.target = target;
isMove = true;
}
public void Move2(GameObject target)
{
this.target = target;
//isMove = true;
this.model.GetComponent<Animation>().Play("run@loop");
StartCoroutine(onMoveCoroutine);
}
public void Update()
{
if (this.isMove == true)
{
this.model.GetComponent<Animation>().Play("run@loop");
this.onMove(target);
}
if (this.transform.position == target?.transform.position)
{
StopCoroutine(onMoveCoroutine);
}
}
private void OnTriggerEnter(Collider other)
{
{
}
}
IEnumerator OnMoveCoroutine()
{
while (true)
{
var dis = Vector3.Distance(this.transform.position, target.transform.position);
var vc = target.transform.position - this.transform.position;
var dir = vc.normalized;
var speed = 1;
this.transform.position += dir * speed * 0.01f;
if (dis <= 0.1f)
{
this.model.GetComponent<Animation>().Play("idle@loop");
this.transform.position = target.transform.position;
//isMove = false;
}
yield return new WaitForSeconds(0.01f);
}
}
}
|
|
코드뿐만 아니라 유니티에서 Hero 게임오브젝트와 collider 오브젝트에 각각 Box Collider, Rigid Body 컴포넌트를 부착하고 is Trigger와 is Kinematic 항목에 체크표시를 해주었다.
C#/실습 2019. 11. 29. 16:11
코드:
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 UnityEngine;
public class App : MonoBehaviour
{
public Button btnMoveCoroutine;
public Button btnMoveUpdate;
public Hero hero;
public GameObject targetPoint;
// Start is called before the first frame update
void Start()
{
GameObject heroGO = new GameObject("Hero");
hero = heroGO.AddComponent<Hero>();
string path = string.Format("Prefabs/{0}", "ch_01_01");
var model = Instantiate(prefab);
model.transform.SetParent(heroGO.transform, false);
{
var dis = Vector3.Distance(hero.transform.position, target.transform.position);
var vc = target.transform.position - hero.transform.position;
var dir = vc.normalized;
var speed = 1;
hero.transform.position += dir * speed * Time.deltaTime;
if (dis <= 0.5f)
{
hero.transform.position = target.transform.position;
}
};
btnMoveCoroutine.onClick.AddListener(() =>
{
hero.Move2(targetPoint);
});
btnMoveUpdate.onClick.AddListener(() => {
});
}
// Update is called once per frame
void Update()
{
}
}
|
|
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
public class Hero : MonoBehaviour
{
public bool isMove;
public Action<GameObject> onMove;
public GameObject target;
public GameObject model;
IEnumerator onMoveCoroutine;
// Start is called before the first frame update
void Start()
{
onMoveCoroutine = OnMoveCoroutine();
}
public void Move(GameObject target)
{
this.target = target;
isMove = true;
}
public void Move2(GameObject target)
{
this.target = target;
//isMove = true;
this.model.GetComponent<Animation>().Play("run@loop");
StartCoroutine(onMoveCoroutine);
}
public void Update()
{
if (this.isMove == true)
{
this.model.GetComponent<Animation>().Play("run@loop");
this.onMove(target);
}
if (this.transform.position == target?.transform.position)
{
StopCoroutine(onMoveCoroutine);
}
}
IEnumerator OnMoveCoroutine()
{
while (true)
{
var dis = Vector3.Distance(this.transform.position, target.transform.position);
var vc = target.transform.position - this.transform.position;
var dir = vc.normalized;
var speed = 1;
this.transform.position += dir * speed * 0.01f;
if (dis <= 0.1f)
{
this.model.GetComponent<Animation>().Play("idle@loop");
this.transform.position = target.transform.position;
//isMove = false;
}
yield return new WaitForSeconds(0.01f);
}
}
}
|
|
C#/실습 2019. 11. 26. 17:40
코드:
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;
using System.Collections.Generic;
using System.Text;
using System.Threading;
namespace _11._26_study2
{
class App
{
private delegate void Del(Monster monster, Action<Monster> action);
Action<Monster> monsterHit;
Action<Hero, Monster> attackAction;
public App()
{
var hero = new Hero(10);
var monster = new Monster(100);
//monsterHit = (monster) =>
//{
// Console.WriteLine("몬스터의 체력이 감소되었습니다. 몬스터의 현재체력: {0}", monster.hp);
// {
// }
// else
// {
// }
//};
//**********************************************
//모든 과정을 App을 통해 전달:
//어떻게 맞는지 정의
monster.GetDamageAc = (damage) =>
{
monster.GetDamage(damage);
Console. WriteLine( "몬스터가 피격당했습니다. 몬스터 현재체력: {0}", monster.hp); //얘 맞음
};
//어떻게 때리는지 정의
hero.iDamaged = (damage) =>
{
Console.WriteLine("영웅이 때렸습니다. 데미지:{0}", damage);
monster.GetDamageAc(damage); //너 맞으래
};
//얘네 서로 어떻게싸우는지 정의
attackAction = (hero, monster) =>
{
{
Thread.Sleep(300);
AttackUntilDie(hero, monster, attackAction);
}
else
{
}
};
AttackUntilDie(hero, monster, attackAction);
}
public void AttackUntilDie(Hero hero, Monster monster, Action<Hero, Monster> attackAction)
{
attackAction(hero, monster); //얘네 서로 싸운다
}
}
}
|
|
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 System;
using System.Collections.Generic;
using System.Text;
namespace _11._26_study2
{
class Hero
{
public int damage;
public Action<int> iDamaged;
public Hero(int damage)
{
this.damage = damage;
}
//public void Attack(Monster monster, Action<Monster> attackComplete)
//{
// //몬스터의 체력을 감소시킨다
// attackComplete(monster);
//}
public void BlindAttack()
{
iDamaged(this.damage);
}
}
}
|
|
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
|
using System;
using System.Collections.Generic;
using System.Text;
namespace _11._26_study2
{
class Monster
{
public int hp;
public Action onDie;
public Action<int> GetDamageAc;
public Monster(int hp)
{
this.hp = hp;
}
public void MyHpIsZero()
{
if(this.hp <=0)
{
onDie(); //나 죽었소
}
}
public void GetDamage(int damage)
{
this.hp -= damage;
GetDamageAc(hp); // 나 맞았소
}
}
}
|
|
출력:
|