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

質問編集履歴

11

追記⑧

2021/02/10 18:52

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -63,31 +63,6 @@
63
63
  まず、PingPongメソッドの往復運動が実装できてからでないと、考えることができないという状態です。
64
64
 
65
65
  ### 追記
66
-
67
- ・t-length。
68
- ```C#
69
- int length = 3;
70
- for (int t=0; t<13; t++){
71
- Debug.Log($"Mathf.PingPong({t},{length}):{Mathf.PingPong(t,length)}, t-length:{t-length}");
72
- }
73
- ```
74
- 出力結果。
75
- ```
76
- Mathf.PingPong(0,3):0, t-length:-3
77
- Mathf.PingPong(1,3):1, t-length:-2
78
- Mathf.PingPong(2,3):2, t-length:-1
79
- Mathf.PingPong(3,3):3, t-length:0
80
- Mathf.PingPong(4,3):2, t-length:1
81
- Mathf.PingPong(5,3):1, t-length:2
82
- Mathf.PingPong(6,3):0, t-length:3
83
- Mathf.PingPong(7,3):1, t-length:4
84
- Mathf.PingPong(8,3):2, t-length:5
85
- Mathf.PingPong(9,3):3, t-length:6
86
- Mathf.PingPong(10,3):2, t-length:7
87
- Mathf.PingPong(11,3):1, t-length:8
88
- Mathf.PingPong(12,3):0, t-length:9
89
- ```
90
-
91
66
  ・length - Mathf.Repeat(t, length * 2.0f)
92
67
  ```C#
93
68
  int length = 3;
@@ -108,10 +83,6 @@
108
83
  Mathf.PingPong(6,3):0, length - Mathf.Repeat(t, length * 2.0f):3
109
84
  Mathf.PingPong(7,3):1, length - Mathf.Repeat(t, length * 2.0f):2
110
85
  Mathf.PingPong(8,3):2, length - Mathf.Repeat(t, length * 2.0f):1
111
- Mathf.PingPong(9,3):3, length - Mathf.Repeat(t, length * 2.0f):0
112
- Mathf.PingPong(10,3):2, length - Mathf.Repeat(t, length * 2.0f):-1
113
- Mathf.PingPong(11,3):1, length - Mathf.Repeat(t, length * 2.0f):-2
114
- Mathf.PingPong(12,3):0, length - Mathf.Repeat(t, length * 2.0f):3
115
86
  ```
116
87
 
117
88
  ### 追記②
@@ -169,12 +140,6 @@
169
140
  float startTime;
170
141
  float speed = 2.0f;
171
142
 
172
- void Start()
173
- {
174
-
175
- }
176
-
177
-
178
143
  void Update(){
179
144
  if (Input.GetMouseButtonDown(0)){
180
145
  // TimetimeCoroutineが動いていたら何もしないという処理を可能であれば書きたい
@@ -316,4 +281,48 @@
316
281
  RunningMyCoroutine = null;
317
282
  }
318
283
  }
284
+ ```
285
+ ### 追記⑧
286
+
287
+ ```C#
288
+ [SerializeField] GameObject startPoint;
289
+ [SerializeField] GameObject endPoint;
290
+ [SerializeField] float animationLength = 1.0f;
291
+
292
+ readonly float speed = 4;
293
+
294
+ void Start()
295
+ {
296
+ Vector3 startPosition = startPoint.transform.position;
297
+ Vector3 endPosition = endPoint.transform.position;
298
+ startPosition.y = 0.0f;
299
+ endPosition.y = 0.0f;
300
+ StartCoroutine(Move(this.transform, endPosition));
301
+ }
302
+
303
+ IEnumerator Move(Transform trf, Vector3 toPosition)
304
+ {
305
+ WaitForInterruption interrupter = new WaitForInterruption();
306
+ while (true)
307
+ {
308
+ Vector3 currentPosition = trf.position;
309
+ Vector3 nextPosition = Vector3.MoveTowards(currentPosition, toPosition, speed * Time.deltaTime);
310
+ if (currentPosition == nextPosition)
311
+ {
312
+ break;
313
+ }
314
+ trf.position = nextPosition;
315
+ // yield return new WaitUntil(()=>true);
316
+ // yield return new WaitWhile(()=>false);
317
+ // yield return false;
318
+ // yield return true;
319
+ yield return interrupter;
320
+ }
321
+ }
322
+
323
+ class WaitForInterruption : CustomYieldInstruction
324
+ {
325
+ // public override bool keepWaiting => this.runningCoroutineCount > 0;
326
+ public override bool keepWaiting => false;
327
+ }
319
328
  ```

10

追記⑦追記

2021/02/10 18:52

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -284,4 +284,36 @@
284
284
  m_time = 0;
285
285
  }
286
286
  }
287
+ ```
288
+ ### 追記⑦
289
+
290
+ ```C#
291
+ public class TestCoroutine : MonoBehaviour
292
+ {
293
+
294
+ UnityEngine.Coroutine RunningMyCoroutine;
295
+
296
+ void Start()
297
+ {
298
+ RunningMyCoroutine = StartCoroutine(MyCoroutine());
299
+ }
300
+
301
+ void Update()
302
+ {
303
+ if(Input.GetMouseButtonDown(0)){
304
+ RunningMyCoroutine = StartCoroutine(MyCoroutine());
305
+ }
306
+ }
307
+
308
+ IEnumerator MyCoroutine(){
309
+ if (RunningMyCoroutine != null) yield break;
310
+ int i = 0;
311
+ while(i<5){
312
+ Debug.Log($"MyCoroutine:{i}");
313
+ i++;
314
+ yield return new WaitForSeconds(2.0f);
315
+ }
316
+ RunningMyCoroutine = null;
317
+ }
318
+ }
287
319
  ```

9

追記⑥

2021/02/09 16:51

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -220,4 +220,68 @@
220
220
  StartCoroutine(TimeCoroutine(startTime));
221
221
  Debug.Log("再開");
222
222
  }
223
+ ```
224
+
225
+ ### 追記⑥
226
+
227
+ ```C#
228
+ public class MyTime : MonoBehaviour
229
+ {
230
+ private static bool m_isActive = false;
231
+ private static float m_time;
232
+ private static float m_timeScale = 1.0f;
233
+ private static IEnumerator mytimeCoroutine;
234
+ public static float time{
235
+ get { return m_time; }
236
+ }
237
+ public static bool isActive{
238
+ get { return m_isActive; }
239
+ }
240
+
241
+ public static float timeSale{
242
+ get { return m_timeScale; }
243
+ set {
244
+ if (m_timeScale == 0 && m_isActive){
245
+ StopCoroutine(mytimeCoroutine);
246
+ m_timeScale = 0;
247
+ }else{
248
+ m_timeScale = value;
249
+ }
250
+ }
251
+ }
252
+
253
+ private static IEnumerator TimeCoroutine(){
254
+ // 本当はメソッドのようにreturnで処理を中止したかったのですが、できませんか?
255
+ // if (m_isActive) return;
256
+ if(!m_isActive){
257
+ m_isActive = true;
258
+ while(true){
259
+ m_time += Time.deltaTime;
260
+ m_time *= m_timeScale;
261
+ yield return null;
262
+ }
263
+ }
264
+ }
265
+
266
+ public static void StartTime(){
267
+ StartCoroutine(TimeCoroutine());
268
+ }
269
+
270
+ public static void StopTime(){
271
+ if (!m_isActive) return;
272
+ m_isActive = false;
273
+ StopCoroutine(mytimeCoroutine);
274
+ }
275
+
276
+ public static void RestartTime(){
277
+ if (mytimeCoroutine == null) return;
278
+ StartCoroutine(mytimeCoroutine);
279
+ }
280
+
281
+ public static void ClearTime(){
282
+ StopTime();
283
+ mytimeCoroutine = null;
284
+ m_time = 0;
285
+ }
286
+ }
223
287
  ```

8

追記⑤

2021/02/05 15:37

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -161,4 +161,63 @@
161
161
  // 補間値t=1の調整処理
162
162
  trf.forward = Quaternion.Euler(0.0f, dir.y, 0.0f) * baseForward;
163
163
  }
164
+ ```
165
+
166
+ ### 追記⑤
167
+
168
+ ```C#
169
+ float startTime;
170
+ float speed = 2.0f;
171
+
172
+ void Start()
173
+ {
174
+
175
+ }
176
+
177
+
178
+ void Update(){
179
+ if (Input.GetMouseButtonDown(0)){
180
+ // TimetimeCoroutineが動いていたら何もしないという処理を可能であれば書きたい
181
+ startTime = Time.time;
182
+ StartCoroutine(TimeCoroutine(startTime));
183
+ }else if(Input.GetMouseButtonDown(1)){
184
+ Suspension(TimeCoroutine, DoSomethingCoroutine);
185
+ }
186
+ }
187
+
188
+ // Time.timeを使ったコルーチン
189
+ // 今回はPingPongの処理に該当。コードが長くなり、他でエラーが発生しているため、暫定的な内容となりました。
190
+ IEnumerator TimeCoroutine(float startTime){
191
+ float t = (Time.time - startTime) * speed;
192
+ while(true){
193
+ Debug.Log("$TimeCoroutine:{t}");
194
+ }
195
+ }
196
+
197
+ IEnumerator DoSomethingCoroutine(float animationLength){
198
+ for (float time = 0.0f; time < animationLength; time += Time.deltaTime)
199
+ {
200
+ float t = time / this.animationLength;
201
+ // 補間値tを使った何かしらの処理
202
+ Debug.Log($"t:{t}");
203
+ yield return null;
204
+ }
205
+ // 補間値t=1の調整処理
206
+ Debug.Log("t:1");
207
+ }
208
+
209
+ void Suspension(Coroutine TimeCoroutine, Coroutine SuspensionCoroutine){
210
+ // 第1引数のTimetimeCoroutineが動いていなければ何もしない
211
+ // if文で判定してreturn
212
+ // ...というような処理を組みたいと思ったのですが、コルーチンの生存確認をする方法が
213
+ // 見つかりませんでした。
214
+ // フィールドにフラグを持たせて、TimetimeCoroutine内で切り替えるという手法以外にありますか?
215
+ StopCoroutine(TimeCoroutine(0f));
216
+ Debug.Log("中断");
217
+ float pauseTime = Time.time;
218
+ yield return SuspensionCoroutine();
219
+ startTime += Time.time - pauseTime;
220
+ StartCoroutine(TimeCoroutine(startTime));
221
+ Debug.Log("再開");
222
+ }
164
223
  ```

7

追記

2021/02/05 15:36

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -142,4 +142,23 @@
142
142
  // 補間値t=1の調整処理
143
143
  trf.forward = Quaternion.Euler(0.0f, 180.0f, 0.0f) * baseForward;
144
144
  }
145
+ ```
146
+
147
+ ### 追記④
148
+
149
+ ```C#
150
+ IEnumerator YTurnToTarget(Transform trf, Transform target){
151
+ Vector3 dir = (target.position - trf.position).normalized;
152
+ Debug.Log(dir.y);
153
+ Vector3 baseForward = trf.forward;
154
+ for (float time = 0.0f; time < this.animationLength; time += Time.deltaTime)
155
+ {
156
+ float t = time / this.animationLength;
157
+ Quaternion q = Quaternion.Euler(0.0f, Mathf.Lerp(0.0f, dir.y, t), 0.0f);
158
+ trf.forward = q * baseForward;
159
+ yield return null;
160
+ }
161
+ // 補間値t=1の調整処理
162
+ trf.forward = Quaternion.Euler(0.0f, dir.y, 0.0f) * baseForward;
163
+ }
145
164
  ```

6

追記③

2021/02/05 15:31

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -130,15 +130,16 @@
130
130
  ### 追記③
131
131
 
132
132
  ```C#
133
- IEnumerator Turn(Transform trf, Vector3 forward){
133
+ IEnumerator Turn(Transform trf){
134
+ Vector3 baseForward = trf.forward;
134
135
  for (float time = 0.0f; time < this.animationLength; time += Time.deltaTime)
135
136
  {
136
137
  float t = time / this.animationLength;
137
138
  Quaternion q = Quaternion.Euler(0.0f, Mathf.Lerp(0.0f, 180.0f, t), 0.0f);
138
- trf.forward = q * forward;
139
+ trf.forward = q * baseForward;
139
140
  yield return null;
140
141
  }
141
142
  // 補間値t=1の調整処理
142
- trf.forward = Quaternion.Euler(0.0f, 180.0f, 0.0f) * forward;
143
+ trf.forward = Quaternion.Euler(0.0f, 180.0f, 0.0f) * baseForward;
143
144
  }
144
145
  ```

5

追記③

2021/02/02 15:20

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -125,4 +125,20 @@
125
125
  Debug.Log(hitInfo.collider);
126
126
  }
127
127
  ```
128
- ![イメージ説明](7e26424c8d765578f22ea31b67ea31a9.gif)
128
+ ![イメージ説明](7e26424c8d765578f22ea31b67ea31a9.gif)
129
+
130
+ ### 追記③
131
+
132
+ ```C#
133
+ IEnumerator Turn(Transform trf, Vector3 forward){
134
+ for (float time = 0.0f; time < this.animationLength; time += Time.deltaTime)
135
+ {
136
+ float t = time / this.animationLength;
137
+ Quaternion q = Quaternion.Euler(0.0f, Mathf.Lerp(0.0f, 180.0f, t), 0.0f);
138
+ trf.forward = q * forward;
139
+ yield return null;
140
+ }
141
+ // 補間値t=1の調整処理
142
+ trf.forward = Quaternion.Euler(0.0f, 180.0f, 0.0f) * forward;
143
+ }
144
+ ```

4

追記

2021/02/02 15:15

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -112,4 +112,17 @@
112
112
  Mathf.PingPong(10,3):2, length - Mathf.Repeat(t, length * 2.0f):-1
113
113
  Mathf.PingPong(11,3):1, length - Mathf.Repeat(t, length * 2.0f):-2
114
114
  Mathf.PingPong(12,3):0, length - Mathf.Repeat(t, length * 2.0f):3
115
- ```
115
+ ```
116
+
117
+ ### 追記②
118
+ レイの変更箇所だけ記載します。
119
+ ```C#
120
+ if (Physics.Raycast(new Ray(new Vector3(newPosition.x, 100.0f, newPosition.z), Vector3.down), out var hitInfo))
121
+ {
122
+ if(!(hitInfo.collider.CompareTag("Player") || hitInfo.collider.CompareTag("Ground"))){
123
+ newPosition = hitInfo.point;
124
+ }
125
+ Debug.Log(hitInfo.collider);
126
+ }
127
+ ```
128
+ ![イメージ説明](7e26424c8d765578f22ea31b67ea31a9.gif)

3

追記

2021/02/01 16:45

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -60,4 +60,56 @@
60
60
  で設定できると考えていて、
61
61
  進行方向は、startPointかendPointのどちらかから、求めることができることはわかっていますが、
62
62
  現在、startPointとendPointのどちらに向かっているかを取得する方法は、
63
- まず、PingPongメソッドの往復運動が実装できてからでないと、考えることができないという状態です。
63
+ まず、PingPongメソッドの往復運動が実装できてからでないと、考えることができないという状態です。
64
+
65
+ ### 追記
66
+
67
+ ・t-length。
68
+ ```C#
69
+ int length = 3;
70
+ for (int t=0; t<13; t++){
71
+ Debug.Log($"Mathf.PingPong({t},{length}):{Mathf.PingPong(t,length)}, t-length:{t-length}");
72
+ }
73
+ ```
74
+ 出力結果。
75
+ ```
76
+ Mathf.PingPong(0,3):0, t-length:-3
77
+ Mathf.PingPong(1,3):1, t-length:-2
78
+ Mathf.PingPong(2,3):2, t-length:-1
79
+ Mathf.PingPong(3,3):3, t-length:0
80
+ Mathf.PingPong(4,3):2, t-length:1
81
+ Mathf.PingPong(5,3):1, t-length:2
82
+ Mathf.PingPong(6,3):0, t-length:3
83
+ Mathf.PingPong(7,3):1, t-length:4
84
+ Mathf.PingPong(8,3):2, t-length:5
85
+ Mathf.PingPong(9,3):3, t-length:6
86
+ Mathf.PingPong(10,3):2, t-length:7
87
+ Mathf.PingPong(11,3):1, t-length:8
88
+ Mathf.PingPong(12,3):0, t-length:9
89
+ ```
90
+
91
+ ・length - Mathf.Repeat(t, length * 2.0f)
92
+ ```C#
93
+ int length = 3;
94
+ for (int t=0; t<13; t++){
95
+ Debug.Log($"Mathf.PingPong({t},{length}):{Mathf.PingPong(t,length)},
96
+ length - Mathf.Repeat(t, length * 2.0f):{length - Mathf.Repeat(t, length * 2.0f)}");
97
+ }
98
+ ```
99
+
100
+ 出力結果。
101
+ ```
102
+ Mathf.PingPong(0,3):0, length - Mathf.Repeat(t, length * 2.0f):3
103
+ Mathf.PingPong(1,3):1, length - Mathf.Repeat(t, length * 2.0f):2
104
+ Mathf.PingPong(2,3):2, length - Mathf.Repeat(t, length * 2.0f):1
105
+ Mathf.PingPong(3,3):3, length - Mathf.Repeat(t, length * 2.0f):0
106
+ Mathf.PingPong(4,3):2, length - Mathf.Repeat(t, length * 2.0f):-1
107
+ Mathf.PingPong(5,3):1, length - Mathf.Repeat(t, length * 2.0f):-2
108
+ Mathf.PingPong(6,3):0, length - Mathf.Repeat(t, length * 2.0f):3
109
+ Mathf.PingPong(7,3):1, length - Mathf.Repeat(t, length * 2.0f):2
110
+ Mathf.PingPong(8,3):2, length - Mathf.Repeat(t, length * 2.0f):1
111
+ Mathf.PingPong(9,3):3, length - Mathf.Repeat(t, length * 2.0f):0
112
+ Mathf.PingPong(10,3):2, length - Mathf.Repeat(t, length * 2.0f):-1
113
+ Mathf.PingPong(11,3):1, length - Mathf.Repeat(t, length * 2.0f):-2
114
+ Mathf.PingPong(12,3):0, length - Mathf.Repeat(t, length * 2.0f):3
115
+ ```

2

改行修正

2021/01/31 17:12

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -51,13 +51,13 @@
51
51
  Debug.Log(Mathf.PingPong(3, -2)); // -4
52
52
  ```
53
53
 
54
- また、位置ベクトル間の往復運動が実装できたとして、ゲームオブジェクトをY軸周りで、進行方向に向かせたいのですが、
54
+ また、位置ベクトル間の往復運動が実装できたとして、
55
- どのように実装すればよいでしょうか?
55
+ ゲームオブジェクトをY軸周りで、進行方向に向かせたいのですが、どのように実装すればよいでしょうか?
56
56
  Y軸周りで向きを設定することに関しては、進行方向のベクトルをdirとすると、
57
57
  ```C#
58
58
  this.transform.rotaion = Quaternion.Euler(0, dir.y, 0);
59
59
  ```
60
+ で設定できると考えていて、
60
- で設定できると考えていて、進行方向は、startPointかendPointのどちらかから、
61
+ 進行方向は、startPointかendPointのどちらかから、求めることができることはわかっていますが、
61
- 求めることができることはわかっていますが、
62
62
  現在、startPointとendPointのどちらに向かっているかを取得する方法は、
63
63
  まず、PingPongメソッドの往復運動が実装できてからでないと、考えることができないという状態です。

1

改行修正

2021/01/30 17:55

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -3,7 +3,8 @@
3
3
  位置ベクトル間で、PingPongメソッドを使って往復運動を実装させるにはどのようにすればよいでしょうか?
4
4
  そもそもPingPongメソッドで、位置ベクトル間の往復運動が実装可能かどうかということがわかっていないです。
5
5
  やりたいこととしては、キャラクタの往復運動です。
6
- PingPongメソッドで実装可能であれば、おそらく1番手軽な実装になると思うのでPingPongメソッドで実装したいと考えています。
6
+ PingPongメソッドで実装可能であれば、おそらく1番手軽な実装になると思うのでPingPongメソッドで
7
+ 実装したいと考えています。
7
8
 
8
9
  ### 試したこと
9
10
 
@@ -32,14 +33,16 @@
32
33
  transform.position.y, startPoint.transform.position.z+dz);
33
34
  }
34
35
  ```
35
- 上記でゲームを実行したところ、往復運動はするものの、直線上の動きではなく、カクカクとした動きとなってしまいました。
36
+ 上記でゲームを実行したところ、往復運動はするものの、直線上の動きではなく、
37
+ カクカクとした動きとなってしまいました。
36
38
  この不具合の原因は、x座標とz座標とで、dirの最大値に到達するタイミングが合わないためと考えています。
37
39
  そうすると、PingPongメソッドで、位置ベクトル間での往復運動は実装不可能なのでしょうか?
38
40
 
41
+ ちなみに、dx=-dxのような処理をしているのは、
39
- ちなみに、dx=-dxのような処理をしているのは、PingPongメソッドの第2引数が負の値に対応していないみたいだからです。
42
+ PingPongメソッドの第2引数が負の値に対応していないみたいだからです。
40
43
  [以前の質問](https://teratail.com/questions/318056)は、「負の値も振り幅にする」であって、
41
- 片道方向ならば、もしかしたら、第2引数で負の値に対応しているのかもしれないと期待したのですが、ダメそうでした。
44
+ 片道方向ならば、もしかしたら、第2引数で負の値に対応しているのかもしれないと期待したのですが、
42
- 下記のようなログを取ってみました。
45
+ ダメそうでした。下記のようなログを取ってみました。
43
46
  負の値に対応してなさそうというのはわかるのですが、なぜ一律に-4の出力なるのかは不明です。
44
47
  ```C#
45
48
  Debug.Log(Mathf.PingPong(0, -2)); // -4
@@ -54,6 +57,7 @@
54
57
  ```C#
55
58
  this.transform.rotaion = Quaternion.Euler(0, dir.y, 0);
56
59
  ```
57
- で設定できると考えていて、進行方向は、startPointかendPointのどちらかから、求めることができることはわかっていますが、
60
+ で設定できると考えていて、進行方向は、startPointかendPointのどちらかから、
61
+ 求めることができることはわかっていますが、
58
62
  現在、startPointとendPointのどちらに向かっているかを取得する方法は、
59
63
  まず、PingPongメソッドの往復運動が実装できてからでないと、考えることができないという状態です。