回答編集履歴
2
修正
answer
CHANGED
@@ -16,18 +16,20 @@
|
|
16
16
|
|
17
17
|
`Vector3`にしてますが、`Vector2`に変えてもいけるかと。あと、Time.fixedDeltaTimeは必要に応じて変えて下さい。
|
18
18
|
|
19
|
+
(追記) インデックスの更新ミスってたので直しました。
|
20
|
+
|
19
21
|
```c#
|
20
22
|
using System.Collections.Generic;
|
21
23
|
using UnityEngine;
|
22
24
|
|
23
25
|
public class MoveToPoints : MonoBehaviour
|
24
26
|
{
|
25
|
-
public float
|
27
|
+
public float Speed = 5f;
|
26
28
|
public List<Vector3> Points = new List<Vector3>()
|
27
29
|
{
|
28
30
|
new Vector3(0, 0, 0),
|
29
|
-
new Vector3(0, 0,
|
31
|
+
new Vector3(0, 0, 5),
|
30
|
-
new Vector3(
|
32
|
+
new Vector3(5, 0, 0),
|
31
33
|
};
|
32
34
|
int PrevPointIndex = 0;
|
33
35
|
|
@@ -35,23 +37,23 @@
|
|
35
37
|
{
|
36
38
|
// ポイントを取得します
|
37
39
|
var prevPoint = Points[PrevPointIndex];
|
38
|
-
var nextPoint = Points[
|
40
|
+
var nextPoint = Points[(PrevPointIndex < (Points.Count - 1)) ? (PrevPointIndex + 1) : 0];
|
39
41
|
|
40
42
|
// 位置を取得します
|
41
43
|
var prevPos = this.transform.position;
|
42
44
|
|
43
45
|
// 現在の位置を計算します
|
44
|
-
var ratio = (Vector3.Distance(prevPoint, prevPos) + Time.fixedDeltaTime) / Vector3.Distance(prevPoint, nextPoint);
|
46
|
+
var ratio = (Vector3.Distance(prevPoint, prevPos) + Time.fixedDeltaTime * Speed) / Vector3.Distance(prevPoint, nextPoint);
|
45
47
|
var crntPos = Vector3.Lerp(prevPoint, nextPoint, ratio);
|
46
48
|
|
47
49
|
// 現在の位置に移動させます
|
48
50
|
transform.position = crntPos;
|
49
51
|
|
50
52
|
// ポイントのインデックスを更新します
|
51
|
-
if(ratio >= 1.0f)
|
53
|
+
if (ratio >= 1.0f)
|
52
54
|
{
|
53
|
-
PrevPointIndex =
|
55
|
+
PrevPointIndex = (PrevPointIndex < (Points.Count - 1)) ? (PrevPointIndex + 1) : 0;
|
54
|
-
}
|
56
|
+
}
|
55
57
|
}
|
56
58
|
}
|
57
59
|
```
|
1
コードを追記しました
answer
CHANGED
@@ -9,6 +9,49 @@
|
|
9
9
|
>
|
10
10
|
> 引用元: [Vector3.Lerp - unity DOCUMENTATION](https://docs.unity3d.com/ja/current/ScriptReference/Vector3.Lerp.html)
|
11
11
|
|
12
|
-
とあります。言い換えると、`Vector3.Lerp(地点Aの位置, 地点Bの位置, AからBへの進み具合(0-1の値))`というメソッドです。なので、`距離: Time.time * speed`を進み具合(割合)として渡すのは変ですよね。
|
12
|
+
とあります。言い換えると、`Vector3.Lerp(地点Aの位置, 地点Bの位置, AからBへの進み具合(0-1の値))`というメソッドです。なので、`距離: Time.time * speed`を進み具合(割合)として渡すのは変ですよね。少なくとも`((自地点と地点Aの距離) + Time.time * speed)/(地点Aと地点Bの距離)`を進み具合(割合)として渡してあげるべきです。
|
13
13
|
|
14
|
+
######サンプルコード
|
15
|
+
**※もっといい方法があるかもしれませんのでその点ご留意ください**
|
16
|
+
|
17
|
+
`Vector3`にしてますが、`Vector2`に変えてもいけるかと。あと、Time.fixedDeltaTimeは必要に応じて変えて下さい。
|
18
|
+
|
19
|
+
```c#
|
20
|
+
using System.Collections.Generic;
|
21
|
+
using UnityEngine;
|
22
|
+
|
23
|
+
public class MoveToPoints : MonoBehaviour
|
24
|
+
{
|
25
|
+
public float speed = 0.5f;
|
26
|
+
public List<Vector3> Points = new List<Vector3>()
|
27
|
+
{
|
28
|
+
new Vector3(0, 0, 0),
|
29
|
+
new Vector3(0, 0, 10),
|
30
|
+
new Vector3(10,0, 0),
|
31
|
+
};
|
32
|
+
int PrevPointIndex = 0;
|
33
|
+
|
34
|
+
private void FixedUpdate()
|
35
|
+
{
|
36
|
+
// ポイントを取得します
|
37
|
+
var prevPoint = Points[PrevPointIndex];
|
38
|
+
var nextPoint = Points[Mathf.Clamp(PrevPointIndex + 1, 0, Points.Count)];
|
39
|
+
|
40
|
+
// 位置を取得します
|
41
|
+
var prevPos = this.transform.position;
|
42
|
+
|
43
|
+
// 現在の位置を計算します
|
14
|
-
|
44
|
+
var ratio = (Vector3.Distance(prevPoint, prevPos) + Time.fixedDeltaTime) / Vector3.Distance(prevPoint, nextPoint);
|
45
|
+
var crntPos = Vector3.Lerp(prevPoint, nextPoint, ratio);
|
46
|
+
|
47
|
+
// 現在の位置に移動させます
|
48
|
+
transform.position = crntPos;
|
49
|
+
|
50
|
+
// ポイントのインデックスを更新します
|
51
|
+
if(ratio >= 1.0f)
|
52
|
+
{
|
53
|
+
PrevPointIndex = Mathf.Clamp(PrevPointIndex + 1, 0, Points.Count);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
}
|
57
|
+
```
|