using UnityEngine; using UnityEngine.AI; public class TaxiController : MonoBehaviour { public NavMeshAgent agent; public Transform playerTransform; public Transform taxiSpawnPoint; public Camera mainCamera; public Camera taxiCamera; private bool isCalled = false; private bool playerInTaxi = false; private Vector3 destination; private float fare = 0f; private Vector3 lastPosition; void Start() { agent = GetComponent(); taxiCamera.enabled = false; mainCamera.enabled = true; } void Update() { if (!isCalled) return; if (!playerInTaxi) { // タクシーをプレイヤーの場所に呼び寄せる agent.SetDestination(playerTransform.position); if (Vector3.Distance(transform.position, playerTransform.position) < 3f) { PlayerEnterTaxi(); } } else { // 目的地へ移動 agent.SetDestination(destination); // 運賃計算(距離差分を加算) fare += Vector3.Distance(transform.position, lastPosition); lastPosition = transform.position; if (Vector3.Distance(transform.position, destination) < 3f) { PlayerExitTaxi(); } } } public void CallTaxi(Transform player) { if (isCalled) return; playerTransform = player; transform.position = taxiSpawnPoint.position; isCalled = true; playerInTaxi = false; fare = 0f; lastPosition = transform.position; agent.enabled = true; Debug.Log("タクシーが呼ばれました。"); } public void SetDestination(Vector3 dest) { destination = dest; Debug.Log($"目的地セット: {destination}"); } void PlayerEnterTaxi() { playerInTaxi = true; // プレイヤー移動を停止&非表示など(簡略化) playerTransform.gameObject.SetActive(false); // カメラ切替 mainCamera.enabled = false; taxiCamera.enabled = true; lastPosition = transform.position; Debug.Log("プレイヤーがタクシーに乗りました。"); } void PlayerExitTaxi() { playerInTaxi = false; isCalled = false; // プレイヤーを降ろす(目的地付近に再配置) playerTransform.position = transform.position + transform.right * 2f; playerTransform.gameObject.SetActive(true); // カメラ戻す mainCamera.enabled = true; taxiCamera.enabled = false; agent.enabled = false; Debug.Log($"目的地に到着しました。運賃は {fare:F2}₮ です。"); } } 使い方の例 // 例:ゲームマネージャーやUIボタンから呼び出し public class GameManager : MonoBehaviour { public TaxiController taxi; public Transform player; void Update() { if (Input.GetKeyDown(KeyCode.T)) // Tキーでタクシー呼び出し { taxi.CallTaxi(player); } if (Input.GetKeyDown(KeyCode.D)) // Dキーで目的地セット(テスト用) { Vector3 destination = new Vector3(50, 0, 50); taxi.SetDestination(destination); } } }