回答編集履歴
2
修正
test
CHANGED
@@ -34,6 +34,10 @@
|
|
34
34
|
|
35
35
|
|
36
36
|
|
37
|
+
(追記) インデックスの更新ミスってたので直しました。
|
38
|
+
|
39
|
+
|
40
|
+
|
37
41
|
```c#
|
38
42
|
|
39
43
|
using System.Collections.Generic;
|
@@ -46,7 +50,7 @@
|
|
46
50
|
|
47
51
|
{
|
48
52
|
|
49
|
-
public float
|
53
|
+
public float Speed = 5f;
|
50
54
|
|
51
55
|
public List<Vector3> Points = new List<Vector3>()
|
52
56
|
|
@@ -54,9 +58,9 @@
|
|
54
58
|
|
55
59
|
new Vector3(0, 0, 0),
|
56
60
|
|
57
|
-
new Vector3(0, 0,
|
61
|
+
new Vector3(0, 0, 5),
|
58
62
|
|
59
|
-
new Vector3(
|
63
|
+
new Vector3(5, 0, 0),
|
60
64
|
|
61
65
|
};
|
62
66
|
|
@@ -72,7 +76,7 @@
|
|
72
76
|
|
73
77
|
var prevPoint = Points[PrevPointIndex];
|
74
78
|
|
75
|
-
var nextPoint = Points[
|
79
|
+
var nextPoint = Points[(PrevPointIndex < (Points.Count - 1)) ? (PrevPointIndex + 1) : 0];
|
76
80
|
|
77
81
|
|
78
82
|
|
@@ -84,7 +88,7 @@
|
|
84
88
|
|
85
89
|
// 現在の位置を計算します
|
86
90
|
|
87
|
-
var ratio = (Vector3.Distance(prevPoint, prevPos) + Time.fixedDeltaTime) / Vector3.Distance(prevPoint, nextPoint);
|
91
|
+
var ratio = (Vector3.Distance(prevPoint, prevPos) + Time.fixedDeltaTime * Speed) / Vector3.Distance(prevPoint, nextPoint);
|
88
92
|
|
89
93
|
var crntPos = Vector3.Lerp(prevPoint, nextPoint, ratio);
|
90
94
|
|
@@ -98,13 +102,13 @@
|
|
98
102
|
|
99
103
|
// ポイントのインデックスを更新します
|
100
104
|
|
101
|
-
if(ratio >= 1.0f)
|
105
|
+
if (ratio >= 1.0f)
|
102
106
|
|
103
107
|
{
|
104
108
|
|
105
|
-
PrevPointIndex =
|
109
|
+
PrevPointIndex = (PrevPointIndex < (Points.Count - 1)) ? (PrevPointIndex + 1) : 0;
|
106
110
|
|
107
|
-
}
|
111
|
+
}
|
108
112
|
|
109
113
|
}
|
110
114
|
|
1
コードを追記しました
test
CHANGED
@@ -20,8 +20,94 @@
|
|
20
20
|
|
21
21
|
|
22
22
|
|
23
|
-
とあります。言い換えると、`Vector3.Lerp(地点Aの位置, 地点Bの位置, AからBへの進み具合(0-1の値))`というメソッドです。なので、`距離: Time.time * speed`を進み具合(割合)として渡すのは変ですよね。
|
23
|
+
とあります。言い換えると、`Vector3.Lerp(地点Aの位置, 地点Bの位置, AからBへの進み具合(0-1の値))`というメソッドです。なので、`距離: Time.time * speed`を進み具合(割合)として渡すのは変ですよね。少なくとも`((自地点と地点Aの距離) + Time.time * speed)/(地点Aと地点Bの距離)`を進み具合(割合)として渡してあげるべきです。
|
24
24
|
|
25
25
|
|
26
26
|
|
27
|
+
######サンプルコード
|
28
|
+
|
29
|
+
**※もっといい方法があるかもしれませんのでその点ご留意ください**
|
30
|
+
|
31
|
+
|
32
|
+
|
33
|
+
`Vector3`にしてますが、`Vector2`に変えてもいけるかと。あと、Time.fixedDeltaTimeは必要に応じて変えて下さい。
|
34
|
+
|
35
|
+
|
36
|
+
|
37
|
+
```c#
|
38
|
+
|
39
|
+
using System.Collections.Generic;
|
40
|
+
|
41
|
+
using UnityEngine;
|
42
|
+
|
43
|
+
|
44
|
+
|
45
|
+
public class MoveToPoints : MonoBehaviour
|
46
|
+
|
47
|
+
{
|
48
|
+
|
49
|
+
public float speed = 0.5f;
|
50
|
+
|
51
|
+
public List<Vector3> Points = new List<Vector3>()
|
52
|
+
|
53
|
+
{
|
54
|
+
|
55
|
+
new Vector3(0, 0, 0),
|
56
|
+
|
57
|
+
new Vector3(0, 0, 10),
|
58
|
+
|
59
|
+
new Vector3(10,0, 0),
|
60
|
+
|
61
|
+
};
|
62
|
+
|
63
|
+
int PrevPointIndex = 0;
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
private void FixedUpdate()
|
68
|
+
|
69
|
+
{
|
70
|
+
|
71
|
+
// ポイントを取得します
|
72
|
+
|
73
|
+
var prevPoint = Points[PrevPointIndex];
|
74
|
+
|
75
|
+
var nextPoint = Points[Mathf.Clamp(PrevPointIndex + 1, 0, Points.Count)];
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
// 位置を取得します
|
80
|
+
|
81
|
+
var prevPos = this.transform.position;
|
82
|
+
|
83
|
+
|
84
|
+
|
85
|
+
// 現在の位置を計算します
|
86
|
+
|
27
|
-
|
87
|
+
var ratio = (Vector3.Distance(prevPoint, prevPos) + Time.fixedDeltaTime) / Vector3.Distance(prevPoint, nextPoint);
|
88
|
+
|
89
|
+
var crntPos = Vector3.Lerp(prevPoint, nextPoint, ratio);
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
// 現在の位置に移動させます
|
94
|
+
|
95
|
+
transform.position = crntPos;
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
// ポイントのインデックスを更新します
|
100
|
+
|
101
|
+
if(ratio >= 1.0f)
|
102
|
+
|
103
|
+
{
|
104
|
+
|
105
|
+
PrevPointIndex = Mathf.Clamp(PrevPointIndex + 1, 0, Points.Count);
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
}
|
112
|
+
|
113
|
+
```
|