简单AI自动寻路
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class AutoWayTest : MonoBehaviour {
static AutoWayTest [] autoWayTest;
public List<AutoWayTest> connectWayPoint;
void Awake () {
autoWayTest = FindObjectsOfType<AutoWayTest> ();
ConnectAroundPoint ();
}
void ConnectAroundPoint()
{
connectWayPoint = new List<AutoWayTest> ();
foreach(AutoWayTest wayPoint in autoWayTest)
if(wayPoint.transform!=transform&&Vector3.SqrMagnitude(wayPoint.transform.position-transform.position)<256&&!Physics.CapsuleCast(transform.position+Vector3.down,transform.position+Vector3.up,0.2f,
wayPoint.transform.position-transform.position,Vector3.Distance(wayPoint.transform.position,transform.position)))
{
connectWayPoint.Add(wayPoint);
}
}
void OnDrawGizmosSelected()
{
Gizmos.color = Color.red;
foreach(AutoWayTest wayPoint in connectWayPoint)
{
Gizmos.DrawLine(transform.position,wayPoint.transform.position);
}
}
public static AutoWayTest FindClosePoint(Vector3 pos)
{
AutoWayTest closePoint = null;
float nearDistance = 1000f;
foreach(AutoWayTest wayPoint in autoWayTest)
{
float distance =Vector3.SqrMagnitude(wayPoint.transform.position-pos);
if(distance<nearDistance)
{
closePoint=wayPoint;
nearDistance=distance;
}
}
return closePoint;
}
}