複数ポイント間、移動
パラメータ説明
wayPoint
移動したい場所オブジェクトを必要なだけ、アウトレット接続します。
スタートポイントはコードで設定するので登録不要です。
speed
移動速度を設定ます。
isLeapMode
リープ動作をするかを設定します。
true;リープ動作あり
false:リープ動作なし
isLoopMode
ループするか折り返すかを設定します。
true: ループ(周回)
false: 折り返し
コード
using UnityEngine; using System.Collections; using System.Collections.Generic; public class moveFloor : MonoBehaviour { // WayPoint [SerializeField] List<Transform> wayPoint; // 移動スピード [SerializeField] float speed = 1f; // リープモード選択 [SerializeField] bool isLeapMode; // ループモード選択 [SerializeField] bool isLoopMode; // Use this for initialization IEnumerator Start() { var startPoint = new GameObject("StartPoint"); startPoint.transform.position = transform.position; wayPoint.Insert(0, startPoint.transform); bool isFlip = true; int initPoint = 1; while (true) { do { for (int i = initPoint; i < wayPoint.Count; i++) { var nextPoint = isFlip ? i : wayPoint.Count - 1 - i; var nextWayPosition = wayPoint[nextPoint].position; var moveVector = nextWayPosition - transform.position; while (moveVector.magnitude > .1f) { if (isLeapMode) transform.position = Vector3.Lerp(transform.position, nextWayPosition, Time.deltaTime * speed); else transform.Translate(moveVector.normalized * Time.deltaTime * speed); moveVector = nextWayPosition - transform.position; yield return null; } } initPoint = 0; } while (isLoopMode); isFlip = !isFlip; } } }