質問編集履歴
5
aa
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
4
ああ
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
判定外だったらまたランダムに動かしたい
|
test
CHANGED
@@ -9,129 +9,3 @@
|
|
9
9
|
当たり判定内に入ってもランダムで移動してて
|
10
10
|
|
11
11
|
色々試しても上手くいかなくて、そもそも何をすればいいのか悩んでます。
|
12
|
-
|
13
|
-
### 該当のソースコード
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
```ここに言語名を入力
|
18
|
-
|
19
|
-
using System.Collections;
|
20
|
-
|
21
|
-
using System.Collections.Generic;
|
22
|
-
|
23
|
-
using UnityEngine;
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
public class Move : MonoBehaviour
|
28
|
-
|
29
|
-
{
|
30
|
-
|
31
|
-
private Transform player;
|
32
|
-
|
33
|
-
public float speed = 1.0f;
|
34
|
-
|
35
|
-
public float rotationSmooth = 2f;
|
36
|
-
|
37
|
-
public Vector3 targetPosition;
|
38
|
-
|
39
|
-
public float changeTargetSqrDistance = 40f;
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
void Start()
|
44
|
-
|
45
|
-
{
|
46
|
-
|
47
|
-
targetPosition = GetRandomPositionOnLevel();
|
48
|
-
|
49
|
-
player = GameObject.FindWithTag("Player").transform;
|
50
|
-
|
51
|
-
player = GameObject.FindWithTag("Enamy").transform;
|
52
|
-
|
53
|
-
}
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
void Update()
|
58
|
-
|
59
|
-
{
|
60
|
-
|
61
|
-
// 現在地から目標地点までの距離を取得
|
62
|
-
|
63
|
-
float sqrDistanceToTarget = Vector3.SqrMagnitude(transform.position - targetPosition);
|
64
|
-
|
65
|
-
if (sqrDistanceToTarget < changeTargetSqrDistance)
|
66
|
-
|
67
|
-
{
|
68
|
-
|
69
|
-
targetPosition = GetRandomPositionOnLevel();
|
70
|
-
|
71
|
-
}
|
72
|
-
|
73
|
-
// 目標地点の方向を向く
|
74
|
-
|
75
|
-
Quaternion targetRotation = Quaternion.LookRotation(targetPosition - transform.position);
|
76
|
-
|
77
|
-
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmooth);
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
// 前に進む
|
82
|
-
|
83
|
-
transform.Translate(Vector3.forward * speed * Time.deltaTime);
|
84
|
-
|
85
|
-
}
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
public Vector3 GetRandomPositionOnLevel()
|
90
|
-
|
91
|
-
{
|
92
|
-
|
93
|
-
float levelSize = 15f;
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
// この関数を開始する直前の位置へ戻る
|
98
|
-
|
99
|
-
return new Vector3(Random.Range(-levelSize, levelSize), 0, Random.Range(-levelSize, levelSize));
|
100
|
-
|
101
|
-
}
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
↓ここから悩んでます↓
|
106
|
-
|
107
|
-
// Enamyの当たり判定に触れたら追跡
|
108
|
-
|
109
|
-
void OnTriggerStay(Collider other)
|
110
|
-
|
111
|
-
{
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
if (other.gameObject.tag == "Enamy")
|
116
|
-
|
117
|
-
{
|
118
|
-
|
119
|
-
this.tag = "EnamyMove";
|
120
|
-
|
121
|
-
Quaternion targetRotation = Quaternion.LookRotation(player.position - transform.position);
|
122
|
-
|
123
|
-
transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, Time.deltaTime * rotationSmooth);
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
// 前に進む
|
128
|
-
|
129
|
-
transform.Translate(Vector3.forward * speed * Time.deltaTime);
|
130
|
-
|
131
|
-
}
|
132
|
-
|
133
|
-
}
|
134
|
-
|
135
|
-
}
|
136
|
-
|
137
|
-
```
|
3
gozi
test
CHANGED
File without changes
|
test
CHANGED
File without changes
|
2
誤字です
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,7 +24,7 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
public class
|
27
|
+
public class Move : MonoBehaviour
|
28
28
|
|
29
29
|
{
|
30
30
|
|
1
誤字です
test
CHANGED
File without changes
|
test
CHANGED
@@ -24,13 +24,13 @@
|
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
-
public class
|
27
|
+
public class Randamu : MonoBehaviour
|
28
28
|
|
29
29
|
{
|
30
30
|
|
31
31
|
private Transform player;
|
32
32
|
|
33
|
-
public float speed =
|
33
|
+
public float speed = 1.0f;
|
34
34
|
|
35
35
|
public float rotationSmooth = 2f;
|
36
36
|
|
@@ -48,7 +48,7 @@
|
|
48
48
|
|
49
49
|
player = GameObject.FindWithTag("Player").transform;
|
50
50
|
|
51
|
-
|
51
|
+
player = GameObject.FindWithTag("Enamy").transform;
|
52
52
|
|
53
53
|
}
|
54
54
|
|
@@ -67,8 +67,6 @@
|
|
67
67
|
{
|
68
68
|
|
69
69
|
targetPosition = GetRandomPositionOnLevel();
|
70
|
-
|
71
|
-
|
72
70
|
|
73
71
|
}
|
74
72
|
|
@@ -104,15 +102,15 @@
|
|
104
102
|
|
105
103
|
|
106
104
|
|
107
|
-
|
108
|
-
|
109
|
-
↓ここから
|
105
|
+
↓ここから悩んでます↓
|
110
106
|
|
111
107
|
// Enamyの当たり判定に触れたら追跡
|
112
108
|
|
113
109
|
void OnTriggerStay(Collider other)
|
114
110
|
|
115
111
|
{
|
112
|
+
|
113
|
+
|
116
114
|
|
117
115
|
if (other.gameObject.tag == "Enamy")
|
118
116
|
|