OldBlueWater/BlueWater/Assets/02.Scripts/AssaultMode/Boat.cs
2023-08-12 01:21:26 +09:00

75 lines
2.0 KiB
C#

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class Boat : MonoBehaviour
{
private NavMeshAgent agent;
private LineRenderer lineRenderer;
private GameObject spot;
private Coroutine draw;
public delegate void LandedEventHandler();
public event LandedEventHandler OnLanded;
private void Awake()
{
agent = GetComponent<NavMeshAgent>();
lineRenderer = GetComponent<LineRenderer>();
lineRenderer.startWidth = 0.1f;
lineRenderer.endWidth = 0.1f;
lineRenderer.material.color = Color.yellow;
lineRenderer.enabled = false;
spot = Instantiate(DataManager.Inst.mouseSpot);
}
private void Update()
{
if (Input.GetMouseButtonDown(0))
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
if (Physics.Raycast(ray, out hit, 1000f))
{
agent.SetDestination(hit.point);
spot.gameObject.SetActive(true);
spot.transform.position = hit.point;
if (draw != null) StopCoroutine(draw);
draw = StartCoroutine(DrawPath());
}
}
else if (agent.remainingDistance < 0.1f)
{
spot.gameObject.SetActive(false);
lineRenderer.enabled = false;
if (draw != null) StopCoroutine(draw);
OnLanded?.Invoke();
}
}
private IEnumerator DrawPath()
{
lineRenderer.enabled = true;
yield return null;
while (true)
{
var count = agent.path.corners.Length;
lineRenderer.positionCount = count;
for (var i = 0; i < count; i++)
{
lineRenderer.SetPosition(i, agent.path.corners[i] + new Vector3(0,1,0));
}
yield return null;
}
}
}