概要
今回は各点を結ぶ線描画を加えます。線を加えることで移動経路がわかりやすくなります。
以下画像
開発環境
IDE:Rider
Unity:2020.3.42(LTS)
OS:Windows10
実装のポイント
GizmoクラスとMonoBehaviourクラスのOnDrawGizmosメソッドを使います。
Gizmosクラスはシーンの視覚的にデバッグしやすくするために使われます。
OnDrawGizmosメソッドはシーンのGUIをいじった時に実行されるメソッドです。再生、非再生問わずに実行されます。
コード部分
Waypoint.cs
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
// 座標を定義しているクラス、オブジェクトにアタッチする
public class Waypoint : MonoBehaviour
{
// Start is called before the first frame update
[SerializeField] private Vector3[] points;
public Vector3[] Points => points;
void Start()
{
}
// Update is called once per frame
void Update()
{
}
+ private void OnDrawGizmos()
+ {
+ for (int i = 0; i < points.Length; i++)
+ {
+ if (i < points.Length - 1)
+ {
+ Gizmos.color = Color.yellow;
+ // 隣接する2点間の距離を引く
+ Gizmos.DrawLine(points[i], points[i + 1]);
+ }
+ }
+ }
}
参考
Gizmoクラス
OnDrawGizmosメソッドについて
github コミット分