質問編集履歴
1
最終的に行ったことを書き加えました。
title
CHANGED
File without changes
|
body
CHANGED
@@ -14,4 +14,73 @@
|
|
14
14
|
また、出現させたクローンすべてに力を加えたいため、findObjectsOfTypeをつかおうとしましたが、うまくできませんでした。
|
15
15
|
|
16
16
|
どうすればいいか教えてください。
|
17
|
-
また、あるオブジェクトに力を加えるような場合、プレイやーにアタッチされたスクリプトか吹き飛ばす方にアタッチされたスクリプトどちらに書いた方がいいのでしょうか。
|
17
|
+
また、あるオブジェクトに力を加えるような場合、プレイやーにアタッチされたスクリプトか吹き飛ばす方にアタッチされたスクリプトどちらに書いた方がいいのでしょうか。
|
18
|
+
|
19
|
+
##最終的に行ったこと
|
20
|
+
boolで吹き飛ばす命令をPlayerControllerから受け取り、教えていただいたAddExplosionForceで爆発の影響を受けるようにしました。
|
21
|
+
```C#
|
22
|
+
using System;
|
23
|
+
using System.Collections;
|
24
|
+
using System.Collections.Generic;
|
25
|
+
using UnityEngine;
|
26
|
+
|
27
|
+
public class NomalEnemy : MonoBehaviour
|
28
|
+
{
|
29
|
+
private Rigidbody nomalEnemyRb;
|
30
|
+
private GameObject player;
|
31
|
+
public GameObject bigEnemyPrefab;
|
32
|
+
|
33
|
+
public float nomalEnemySpeed = 30.0f;
|
34
|
+
public float blowOffStrengh = 50.0f;
|
35
|
+
|
36
|
+
public float power;
|
37
|
+
public float radius;
|
38
|
+
public float upwardsModifier;
|
39
|
+
|
40
|
+
private PlayerController playerControllerScript;
|
41
|
+
|
42
|
+
// Start is called before the first frame update
|
43
|
+
void Start()
|
44
|
+
{
|
45
|
+
player = GameObject.Find("Player");
|
46
|
+
nomalEnemyRb = GetComponent<Rigidbody>();
|
47
|
+
playerControllerScript = GameObject.Find("Player").GetComponent<PlayerController>();
|
48
|
+
}
|
49
|
+
|
50
|
+
// Update is called once per frame
|
51
|
+
void Update()
|
52
|
+
{
|
53
|
+
moveToPlayer();
|
54
|
+
|
55
|
+
if (playerControllerScript.blownAwayByPlayerWithSpecialWeapon)
|
56
|
+
{
|
57
|
+
StartCoroutine(BlownAwayByPlayerWithSpecialWeapon());
|
58
|
+
}
|
59
|
+
}
|
60
|
+
|
61
|
+
private void moveToPlayer()
|
62
|
+
{
|
63
|
+
if (!playerControllerScript.gameOver)
|
64
|
+
{
|
65
|
+
if (playerControllerScript.readyToRanchSpecialWeapon)
|
66
|
+
{
|
67
|
+
nomalEnemyRb.velocity = Vector3.zero;
|
68
|
+
}
|
69
|
+
else
|
70
|
+
{
|
71
|
+
Vector3 lookDirection = (player.transform.position - transform.position).normalized;
|
72
|
+
|
73
|
+
nomalEnemyRb.AddForce(lookDirection * nomalEnemySpeed);
|
74
|
+
}
|
75
|
+
}
|
76
|
+
}
|
77
|
+
|
78
|
+
|
79
|
+
IEnumerator BlownAwayByPlayerWithSpecialWeapon()
|
80
|
+
{
|
81
|
+
GetComponent<Rigidbody>().AddExplosionForce(power, player.transform.position, radius, upwardsModifier, ForceMode.Impulse);
|
82
|
+
yield return null;
|
83
|
+
playerControllerScript.blownAwayByPlayerWithSpecialWeapon = false;
|
84
|
+
}
|
85
|
+
}
|
86
|
+
```
|