回答編集履歴

1

位置を移動する例を追記

2019/06/28 18:58

投稿

Bongo
Bongo

スコア10807

test CHANGED
@@ -5,3 +5,157 @@
5
5
 
6
6
 
7
7
  ![結果](47df00cecfc7bfb98241e6b944bab8cd.gif)
8
+
9
+
10
+
11
+ **追記**
12
+
13
+ ParticleSystemForceFieldを付けたオブジェクトを移動・回転・拡大縮小すると効果範囲もちゃんと変形しますので、たとえば下記コードのようにフィールドオブジェクトを数珠つなぎに参照させて、常に次のオブジェクトの方を向くようにしてやれば、オブジェクトの位置を調整するだけで向きを自動的に合わせてくれるでしょうからレイアウトが楽になりそうです。
14
+
15
+
16
+
17
+ ```C#
18
+
19
+ using UnityEngine;
20
+
21
+
22
+
23
+ [RequireComponent(typeof(ParticleSystemForceField))]
24
+
25
+ [ExecuteAlways]
26
+
27
+ public class FieldDirector : MonoBehaviour
28
+
29
+ {
30
+
31
+ // インスペクター上でnextFieldに次の目標となるオブジェクトをセットしておき
32
+
33
+ // 全体としてフィールドオブジェクトが数珠つなぎになるようにする
34
+
35
+ public Transform nextField;
36
+
37
+ private ParticleSystemForceField field;
38
+
39
+
40
+
41
+ private void OnEnable()
42
+
43
+ {
44
+
45
+ this.field = this.GetComponent<ParticleSystemForceField>();
46
+
47
+ }
48
+
49
+
50
+
51
+ private void Update()
52
+
53
+ {
54
+
55
+ if (this.nextField == null)
56
+
57
+ {
58
+
59
+ return;
60
+
61
+ }
62
+
63
+
64
+
65
+ // Y軸を次の目標の方角へ向ける(Y軸方向に力をかけるよう設定されていることを前提としています)
66
+
67
+ this.transform.rotation = Quaternion.LookRotation(this.nextField.position - this.transform.position) *
68
+
69
+ Quaternion.Euler(90.0f, 0.0f, 0.0f);
70
+
71
+ }
72
+
73
+
74
+
75
+ private void OnDrawGizmos()
76
+
77
+ {
78
+
79
+ // 次の目標に向かって線を引き、また効果範囲をわかりやすくするため枠線を描く
80
+
81
+ Gizmos.color = Color.yellow;
82
+
83
+ if (this.nextField != null)
84
+
85
+ {
86
+
87
+ Gizmos.DrawLine(this.transform.position, this.nextField.position);
88
+
89
+ }
90
+
91
+
92
+
93
+ Gizmos.matrix = this.transform.localToWorldMatrix;
94
+
95
+ Gizmos.DrawWireCube(Vector3.zero, new Vector3(2.0f, this.field.length, 2.0f));
96
+
97
+ }
98
+
99
+ }
100
+
101
+ ```
102
+
103
+
104
+
105
+ 手動操作に限らず、スクリプトで位置を操作しても問題ないはずです。下記のようなスクリプトでフィールドを移動させると...
106
+
107
+
108
+
109
+ ```C#
110
+
111
+ using UnityEngine;
112
+
113
+
114
+
115
+ public class FieldMotor : MonoBehaviour
116
+
117
+ {
118
+
119
+ [SerializeField] private float amplitude = 3.0f;
120
+
121
+ [SerializeField] private float angularSpeed = 15.0f;
122
+
123
+ private float phase;
124
+
125
+
126
+
127
+ private void Start()
128
+
129
+ {
130
+
131
+ this.phase = Mathf.Asin(Mathf.Clamp(this.transform.position.x / this.amplitude, -1.0f, 1.0f));
132
+
133
+ }
134
+
135
+
136
+
137
+ private void Update()
138
+
139
+ {
140
+
141
+ this.phase += this.angularSpeed * Mathf.Deg2Rad * Time.deltaTime;
142
+
143
+ var position = this.transform.position;
144
+
145
+ position.x = Mathf.Sin(this.phase) * this.amplitude;
146
+
147
+ this.transform.position = position;
148
+
149
+ }
150
+
151
+ }
152
+
153
+ ```
154
+
155
+
156
+
157
+ 下図のように流れが変化しました。フィールドの形状はある程度長くして、雨どいを並べるような感じでレイアウトするのがよさそうに思います。
158
+
159
+
160
+
161
+ ![結果](2d3c859bff36570f6ec1d20dbbcc63a1.gif)