You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
2.3 KiB

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PlayerController : MonoBehaviour
{
public NavMeshAgent agent;
public bool showPath;
public bool showAhead;
// Start is called before the first frame update
void Start()
{
agent = GetComponent<NavMeshAgent>();
agent.updateRotation = false;
agent.updateUpAxis = false;
}
// Update is called once per frame
void Update()
{
if (Input.GetMouseButtonUp(0))
{
var target = Camera.main.ScreenToWorldPoint(Input.mousePosition);
target.z = 0;
agent.destination = target;
}
}
public static void DebugDrawPath(Vector3[] corners)
{
if (corners.Length < 2) { return; }
int i = 0;
for (; i < corners.Length - 1; i++)
{
Debug.DrawLine(corners[i], corners[i + 1], Color.blue);
}
Debug.DrawLine(corners[0], corners[1], Color.red);
}
private void OnDrawGizmos()
{
DrawGizmos(agent, showPath, showAhead);
}
public static void DrawGizmos(NavMeshAgent agent, bool showPath, bool showAhead)
{
if (Application.isPlaying)
{
if (showPath && agent.hasPath)
{
var corners = agent.path.corners;
if (corners.Length < 2) { return; }
int i = 0;
for (; i < corners.Length - 1; i++)
{
Debug.DrawLine(corners[i], corners[i + 1], Color.blue);
Gizmos.color = Color.blue;
Gizmos.DrawSphere(agent.path.corners[i + 1], 0.03f);
Gizmos.color = Color.blue;
Gizmos.DrawLine(agent.path.corners[i], agent.path.corners[i + 1]);
}
Debug.DrawLine(corners[0], corners[1], Color.blue);
Gizmos.color = Color.red;
Gizmos.DrawSphere(agent.path.corners[1], 0.03f);
Gizmos.color = Color.red;
Gizmos.DrawLine(agent.path.corners[0], agent.path.corners[1]);
}
if (showAhead)
{
Gizmos.color = Color.yellow;
Gizmos.DrawRay(agent.transform.position, agent.transform.up * 0.5f);
}
}
}
}