teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

4

補足情報

2020/02/10 23:04

投稿

leeee1019
leeee1019

スコア9

title CHANGED
File without changes
body CHANGED
@@ -144,4 +144,5 @@
144
144
 
145
145
  ### 補足情報(FW/ツールのバージョンなど)
146
146
 
147
- クラロワとゆうのは、フィールドにキャラを出して戦うカードゲームです
147
+ クラロワとゆうのは、フィールドにキャラを出して戦うカードゲームです
148
+ https://clashroyale.com/ja/

3

補足情報

2020/02/10 23:04

投稿

leeee1019
leeee1019

スコア9

title CHANGED
File without changes
body CHANGED
@@ -144,4 +144,4 @@
144
144
 
145
145
  ### 補足情報(FW/ツールのバージョンなど)
146
146
 
147
- ここより詳細な情報記載してください。
147
+ クラロワとゆうのは、フィールドキャラして戦うカードゲームです

2

言語名

2020/02/10 23:02

投稿

leeee1019
leeee1019

スコア9

title CHANGED
File without changes
body CHANGED
@@ -134,7 +134,7 @@
134
134
  }
135
135
  }
136
136
  }
137
- ```ここに言語名を入力
137
+ C♯ここに言語名を入力
138
138
  ソースコード
139
139
  ```
140
140
 

1

ソースコード

2020/02/04 10:34

投稿

leeee1019
leeee1019

スコア9

title CHANGED
File without changes
body CHANGED
@@ -8,10 +8,132 @@
8
8
 
9
9
  ```
10
10
  エラーメッセージ
11
- ```
11
+ 特にないですが、キャラは自動生成です。
12
12
 
13
13
  ### 該当のソースコード
14
+ using UnityEngine;
15
+ using UnityEngine.AI;
16
+ [RequireComponent(typeof(NavMeshAgent))]
17
+ public class Character : MonoBehaviour
18
+ {
19
+ private GameObject target;
20
+ private NavMeshAgent agent;
21
+ [SerializeField] private GameObject[] enemies;
22
+ [SerializeField] private string attackTag;
23
+ [SerializeField] private string attackTag1;//Inspectorで設定
24
+ private float stopDistance;
25
+ private float attackTimer;
26
+ private bool isAttack;
27
+ //ここから
28
+ public int distance;
29
+ public int hp;
30
+ public int power;
31
+ public int attackTime;
32
+ //ここまでInspectorで設定
33
+ private enum targetType
34
+ {
35
+ normal,
36
+ castle,
37
+ }
38
+ private targetType currentTargetType;
14
39
 
40
+ private void Awake()
41
+ {
42
+ agent = GetComponent<NavMeshAgent>();
43
+ }
44
+ private void Start()
45
+ {
46
+ target = GameObject.Find(attackTag+attackTag1 + "Castle");
47
+ isAttack = false;
48
+ }
49
+ private void Update()
50
+ {
51
+ if (target == null)
52
+ {
53
+ target = GameObject.Find(attackTag+ attackTag1 + "Castle");
54
+ }
55
+
56
+ SetStopDistance();
57
+ FintTarget();
58
+ agent.SetDestination(target.transform.position);
59
+
60
+ if (Vector3.Distance(transform.position, target.transform.position) <= stopDistance)
61
+ {
62
+ isAttack = true;
63
+ agent.speed = 0;
64
+ }
65
+
66
+ if (isAttack)
67
+ {
68
+ CheckNearTarget();
69
+ SetStopDistance(); //攻撃中にtargetが変わった時のためにここでも記述
70
+ Attack();
71
+ }
72
+ }
73
+ //targetが近くにいるのかどうか判定する
74
+ private void CheckNearTarget()
75
+ {
76
+ if (Vector3.Distance(transform.position, target.transform.position) > stopDistance)
77
+ {
78
+ isAttack = false;
79
+ agent.speed = 1;
80
+ }
81
+ }
82
+ private void SetStopDistance()
83
+ {
84
+ if (target.gameObject.name.Contains("Castle"))
85
+ {
86
+ currentTargetType = targetType.castle;
87
+ stopDistance = distance;
88
+ }
89
+ else
90
+ {
91
+ currentTargetType = targetType.normal;
92
+ stopDistance = distance;
93
+ }
94
+ }
95
+ private void FintTarget()
96
+ {
97
+ enemies = GameObject.FindGameObjectsWithTag(attackTag+ attackTag1);
98
+
99
+ float closestDistance = Vector3.Distance(transform.position, target.transform.position);
100
+
101
+ foreach (GameObject enemy in enemies)
102
+ {
103
+ if (Vector3.Distance(transform.position, enemy.transform.position) < closestDistance)
104
+ {
105
+ target = enemy;
106
+ }
107
+ }
108
+ }
109
+ private void Attack()
110
+ {
111
+ attackTimer += Time.deltaTime;
112
+ switch (currentTargetType)
113
+ {
114
+ case (targetType.normal):
115
+ if (attackTimer > 1)
116
+ {
117
+ target.GetComponent<Character>().hp -= power;
118
+ attackTimer = 0f;
119
+ }
120
+ if (target.GetComponent<Character>().hp <= 0)
121
+ {
122
+ isAttack = false;
123
+ Destroy(target.gameObject);
124
+ agent.speed = 1;
125
+ }
126
+ break;
127
+ case (targetType.castle):
128
+ if (attackTimer > attackTime)
129
+ {
130
+ target.GetComponent<Castle>().hp -= power;
131
+ attackTimer = 0f;
132
+ }
133
+ break;
134
+ }
135
+ }
136
+ }
15
137
  ```ここに言語名を入力
16
138
  ソースコード
17
139
  ```