質問編集履歴
1
最終的に行ったことを書き加えました。
test
CHANGED
File without changes
|
test
CHANGED
@@ -31,3 +31,141 @@
|
|
31
31
|
どうすればいいか教えてください。
|
32
32
|
|
33
33
|
また、あるオブジェクトに力を加えるような場合、プレイやーにアタッチされたスクリプトか吹き飛ばす方にアタッチされたスクリプトどちらに書いた方がいいのでしょうか。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
##最終的に行ったこと
|
38
|
+
|
39
|
+
boolで吹き飛ばす命令をPlayerControllerから受け取り、教えていただいたAddExplosionForceで爆発の影響を受けるようにしました。
|
40
|
+
|
41
|
+
```C#
|
42
|
+
|
43
|
+
using System;
|
44
|
+
|
45
|
+
using System.Collections;
|
46
|
+
|
47
|
+
using System.Collections.Generic;
|
48
|
+
|
49
|
+
using UnityEngine;
|
50
|
+
|
51
|
+
|
52
|
+
|
53
|
+
public class NomalEnemy : MonoBehaviour
|
54
|
+
|
55
|
+
{
|
56
|
+
|
57
|
+
private Rigidbody nomalEnemyRb;
|
58
|
+
|
59
|
+
private GameObject player;
|
60
|
+
|
61
|
+
public GameObject bigEnemyPrefab;
|
62
|
+
|
63
|
+
|
64
|
+
|
65
|
+
public float nomalEnemySpeed = 30.0f;
|
66
|
+
|
67
|
+
public float blowOffStrengh = 50.0f;
|
68
|
+
|
69
|
+
|
70
|
+
|
71
|
+
public float power;
|
72
|
+
|
73
|
+
public float radius;
|
74
|
+
|
75
|
+
public float upwardsModifier;
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
private PlayerController playerControllerScript;
|
80
|
+
|
81
|
+
|
82
|
+
|
83
|
+
// Start is called before the first frame update
|
84
|
+
|
85
|
+
void Start()
|
86
|
+
|
87
|
+
{
|
88
|
+
|
89
|
+
player = GameObject.Find("Player");
|
90
|
+
|
91
|
+
nomalEnemyRb = GetComponent<Rigidbody>();
|
92
|
+
|
93
|
+
playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
|
94
|
+
|
95
|
+
}
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
// Update is called once per frame
|
100
|
+
|
101
|
+
void Update()
|
102
|
+
|
103
|
+
{
|
104
|
+
|
105
|
+
moveToPlayer();
|
106
|
+
|
107
|
+
|
108
|
+
|
109
|
+
if (playerControllerScript.blownAwayByPlayerWithSpecialWeapon)
|
110
|
+
|
111
|
+
{
|
112
|
+
|
113
|
+
StartCoroutine(BlownAwayByPlayerWithSpecialWeapon());
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
}
|
118
|
+
|
119
|
+
|
120
|
+
|
121
|
+
private void moveToPlayer()
|
122
|
+
|
123
|
+
{
|
124
|
+
|
125
|
+
if (!playerControllerScript.gameOver)
|
126
|
+
|
127
|
+
{
|
128
|
+
|
129
|
+
if (playerControllerScript.readyToRanchSpecialWeapon)
|
130
|
+
|
131
|
+
{
|
132
|
+
|
133
|
+
nomalEnemyRb.velocity = Vector3.zero;
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
else
|
138
|
+
|
139
|
+
{
|
140
|
+
|
141
|
+
Vector3 lookDirection = (player.transform.position - transform.position).normalized;
|
142
|
+
|
143
|
+
|
144
|
+
|
145
|
+
nomalEnemyRb.AddForce(lookDirection * nomalEnemySpeed);
|
146
|
+
|
147
|
+
}
|
148
|
+
|
149
|
+
}
|
150
|
+
|
151
|
+
}
|
152
|
+
|
153
|
+
|
154
|
+
|
155
|
+
|
156
|
+
|
157
|
+
IEnumerator BlownAwayByPlayerWithSpecialWeapon()
|
158
|
+
|
159
|
+
{
|
160
|
+
|
161
|
+
GetComponent<Rigidbody>().AddExplosionForce(power, player.transform.position, radius, upwardsModifier, ForceMode.Impulse);
|
162
|
+
|
163
|
+
yield return null;
|
164
|
+
|
165
|
+
playerControllerScript.blownAwayByPlayerWithSpecialWeapon = false;
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
}
|
170
|
+
|
171
|
+
```
|