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

質問編集履歴

6

追記

2018/11/15 16:39

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -69,7 +69,7 @@
69
69
 
70
70
  ```C#
71
71
  void Start () {
72
- StartCoroutine(ChangeCoroutine(cube, 1f, 5f, 3f));
72
+ StartCoroutine(ChangeCoroutine(cube, cube.transform.position.x, 5f, 3f));
73
73
  }
74
74
 
75
75
  // Update is called once per frame

5

補足

2018/11/15 16:39

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -88,7 +88,7 @@
88
88
  yield return null;
89
89
  }
90
90
 
91
- //tが1未満でwhile文から抜ける可能性がある為、調整。
91
+ //調整。
92
92
  go.transform.position = new Vector3(end, go.transform.position.y, go.transform.position.z);
93
93
 
94
94
  }

4

追記

2018/11/15 16:33

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -37,7 +37,7 @@
37
37
 
38
38
  ```
39
39
 
40
- ### 追記
40
+ ### 追記
41
41
 
42
42
  ```C#
43
43
  void Start () {
@@ -61,4 +61,46 @@
61
61
  Debug.Log(value);
62
62
  }
63
63
  }
64
+ ```
65
+
66
+ ### 追記②
67
+
68
+ 追記①のように変化させる値を引数で渡すコードは、実用的でなかったので、次のようなコード例を考えてみました。
69
+
70
+ ```C#
71
+ void Start () {
72
+ StartCoroutine(ChangeCoroutine(cube, 1f, 5f, 3f));
73
+ }
74
+
75
+ // Update is called once per frame
76
+ void Update () {
77
+
78
+ }
79
+
80
+ IEnumerator ChangeCoroutine(GameObject go, float start, float end, float duration){
81
+
82
+ float t=0;
83
+ float x=0;
84
+ while(t<1){
85
+ x = Mathf.Lerp(start, end, t);
86
+ t += Time.deltaTime/duration;
87
+ go.transform.position = new Vector3(x, go.transform.position.y, go.transform.position.z);
88
+ yield return null;
89
+ }
90
+
91
+ //tが1未満でwhile文から抜ける可能性がある為、調整。
92
+ go.transform.position = new Vector3(end, go.transform.position.y, go.transform.position.z);
93
+
94
+ }
95
+ ```
96
+
97
+ 経過時間が格納されているかログで確認しました。
98
+ ```C#
99
+ float time = 0;
100
+ //3秒間、毎フレーム行う処理をwhile文の中に記述。
101
+ while(time < 3){
102
+ time += Time.deltaTime;
103
+ Debug.Log(time);
104
+ yield return null;
105
+ }
64
106
  ```

3

追記

2018/11/15 16:06

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -53,8 +53,7 @@
53
53
  value = Mathf.Lerp(start, end, t);
54
54
  t += Time.deltaTime/duration;
55
55
  Debug.Log(value);
56
- //yield return null;
56
+ yield return null;
57
- yield return new WaitForSeconds(2);
58
57
  }
59
58
 
60
59
  //tが1未満でwhile文から抜ける可能性がある為、調整。

2

追記

2018/11/15 09:00

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
File without changes

1

追記。

2018/11/15 08:59

投稿

退会済みユーザー
title CHANGED
File without changes
body CHANGED
@@ -35,4 +35,31 @@
35
35
  }
36
36
  }
37
37
 
38
+ ```
39
+
40
+ ### 追記
41
+
42
+ ```C#
43
+ void Start () {
44
+ float i = 0;
45
+ StartCoroutine(ChangeCoroutine(i, 1f, 5f, 3f));
46
+ }
47
+
48
+ IEnumerator ChangeCoroutine(float value, float start, float end, float duration){
49
+
50
+ float t=0;
51
+
52
+ while(t<1){
53
+ value = Mathf.Lerp(start, end, t);
54
+ t += Time.deltaTime/duration;
55
+ Debug.Log(value);
56
+ //yield return null;
57
+ yield return new WaitForSeconds(2);
58
+ }
59
+
60
+ //tが1未満でwhile文から抜ける可能性がある為、調整。
61
+ value = end;
62
+ Debug.Log(value);
63
+ }
64
+ }
38
65
  ```