11.移動可能なタイルをタップした際にキャラを移動する処理を実装する
・Vector3Int 構造体 ・Camera.ScreenToWorldPoint メソッド ・GridLayout.WorldToCell メソッド ・Tilemap.GetColliderType メソッド ・NavMeshAgent に用意されている変数とメソッド
Unity 公式スクリプトリファレンス
Vector3Int
https://docs.unity3d.com/ja/current/ScriptReferenc...
// タップ(マウスクリック)の位置を取得してワールド座標に変換 Camera.main.ScreenToWorldPoint(Input.mousePosition);
Unity 公式スクリプトリファレンス
Camera.ScreenToWorldPoint
https://docs.unity3d.com/ja/current/ScriptReferenc...
// タップ(マウスクリック)の位置を取得してワールド座標に変換し、それをさらにタイルのセル座標に変換 gridPos = grid.WorldToCell(Camera.main.ScreenToWorldPoint(Input.mousePosition));
Unity 公式スクリプトリファレンス
Grid
https://docs.unity3d.com/ja/current/ScriptReferenc...
Unity 公式スクリプトリファレンス
GridLayout.WorldToCell
https://docs.unity3d.com/ja/current/ScriptReferenc...
// 目的地の更新 //agent.SetDestination(nextPos); // どちらの処理で設定をしても問題なし agent.destination = nextPos;
Unity 公式スクリプトリファレンス
NavMeshAgent.destination
https://docs.unity3d.com/jp/current/ScriptReferenc...
Unity 公式スクリプトリファレンス
NavMeshAgent.SetDestination
https://docs.unity3d.com/jp/current/ScriptReferenc...
Unity 公式マニュアル
NavMeshAgent に目的地へ移動することを伝える
https://docs.unity3d.com/ja/current/Manual/nav-Mov...
// 移動速度の設定 agent.speed = 3.0f;
Unity 公式スクリプトリファレンス
NavMeshAgent.speed
https://docs.unity3d.com/jp/current/ScriptReferenc...
// 3D 用の設定をオフにする。2D なので、この処理がないと、変な位置に勝手に移動する agent.updateRotation = false; agent.updateUpAxis = false;
Unity 公式スクリプトリファレンス
NavMeshAgent.updateRotation
https://docs.unity3d.com/jp/current/ScriptReferenc...
// 3D 用の設定をオフにする。2D なので、この処理がないと、変な位置に勝手に移動する agent.updateRotation = false; agent.updateUpAxis = false;
Unity 公式スクリプトリファレンス
NavMeshAgent .updateUpAxis
https://docs.unity3d.com/jp/current/ScriptReferenc...