GameObject.Find("Axe/Model")
-Model 이라는 게임 오브젝트가 두개 이상 있을 경우,(Axe/Model , Target/Model , Hero/Model)
Axe/Model 이렇게 사용하면 Axe 게임 오브젝트에 속해 있는 Model 게임 오브젝트에 바로 접근 가능
-안하면 Hierarchy의 가장 밑에 있는 Model이 있는 다른 게임 오브젝트로 들어가버림
버튼 활성화하기
btn.interactable = true;
버튼 비활성화하기 (어두운 색으로 바뀜)
btn.interactable = false;
1.App
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class App : MonoBehaviour
{
public Button btn;
public Hero hero;
public Axe axe;
public Target target;
void Start()
{
GameObject loadedAxe = Resources.Load("Prefabs/axe") as GameObject;
var modelshell = GameObject.Find("Axe/Model");
this.hero.OnImpackAttack = () =>
{
//Debug.Log("App: 대리자");
GameObject axeGo = Instantiate(loadedAxe) as GameObject;
axeGo.transform.position = this.hero.AxePoint.transform.position;
this.axe.transform.position = Vector3.zero;
axeGo.transform.SetParent(modelshell.transform);
this.axe.Init();
this.target.Init();
};
this.axe.OnDestroyComplete = () =>
{
this.btn.interactable = true;
};
this.btn.onClick.AddListener(() =>
{
this.hero.ShootAxe();
this.btn.interactable = false;
});
}
}
|
2. 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
54
55
56
57
58
59
60
61
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Hero : MonoBehaviour
{
public GameObject AxePoint;
public UnityAction OnImpackAttack;
private Animation anim;
private string attackAction;
private float attackAniLength;
private float attackTime;
private bool isShoot;
private float elapsedTime;
void Start()
{
GameObject model = GameObject.Find("ch_04_01");
this.anim = model.GetComponent<Animation>();
this.attackAction = "attack_sword_01";
this.attackAniLength = this.anim[this.attackAction].length;
float attackAniFrameRate = this.anim[this.attackAction].clip.frameRate;
float totlaFrame = this.attackAniLength * attackAniFrameRate;
float attackFrame = 8;
this.attackTime = attackFrame * this.attackAniLength / totlaFrame;
}
public void ShootAxe()
{
this.anim.Play("attack_sword_01");
this.isShoot = true;
}
void Update()
{
if (this.isShoot)
{
this.elapsedTime += Time.deltaTime;
if (elapsedTime > this.attackTime)
{
this.elapsedTime = 0;
this.OnImpackAttack();
this.isShoot = false;
}
}
}
}
|
3. Target
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Target : MonoBehaviour
{
private GameObject model;
private Animation anim;
private float damageAniTime;
private float elapsedTime;
private bool isDamage;
private float damageAniLength;
private bool isReady;
public UnityAction OnDamageComplete;
public Axe axe;
void Start()
{
this.model = GameObject.FindGameObjectWithTag("target");
this.anim = this.model.GetComponent<Animation>();
float damageFrame = 6;
this.damageAniLength = this.anim["damage"].length;
float damageAniFrameRate = this.anim["damage"].clip.frameRate;
float totalFrame = damageAniLength * damageAniFrameRate;
this.damageAniTime = damageFrame * damageAniLength / totalFrame;
}
public void Init()
{
this.isReady = true;
}
public void DamageAction()
{
this.anim.Play("damage");
}
void Update()
{
if (this.isReady)
{
float distance = Vector3.Distance(this.transform.position, this.axe.transform.position);
if (distance < 0.1f)
{
this.isDamage = true;
}
if (this.isDamage)
{
this.DamageAction();
this.elapsedTime += Time.deltaTime;
if (this.elapsedTime > this.damageAniTime)
{
this.axe.Destroy();
this.isDamage = false;
this.isReady = false;
}
}
}
}
}
|
4. Axe
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
|
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
public class Axe : MonoBehaviour
{
private GameObject model;
private bool isMove;
public Target target;
public UnityAction OnDestroyComplete;
public void Init()
{
this.model = GameObject.Find("axe(Clone)");
this.isMove = true;
}
void Update()
{
if(this.isMove)
{
this.model.transform.Rotate(800f * Vector3.forward * Time.deltaTime);
this.transform.Translate(1f * Vector3.forward * Time.deltaTime);
}
}
public void Destroy()
{
this.isMove = false;
GameObject.Destroy(this.model);
this.OnDestroyComplete();
}
}
|
'C# > 과제' 카테고리의 다른 글
2020.05.24. 과제 - 로그인 -> 캐릭터 선택 -> 레이캐스트를 이용한 이동, 자동공격 -> 죽으면 팝업 및 재시작 -> 로그인..반복 (0) | 2020.05.24 |
---|---|
2020.05.14. 과제 - ObjectPool 이용해서 무기선택 이펙트 바꾸기 (0) | 2020.05.14 |
2020.05.12. 과제 - Scene전환, Json파일 로드, 캐릭터 생성 및 애니메이션 실행 (0) | 2020.05.12 |
2020.05.10. 과제 - 캐릭터 달리기, 순위 매기기 (0) | 2020.05.10 |
2020.05.07. 과제 - Unity 무기 장착 (수정) (0) | 2020.05.07 |