U3DC.COM | 优三帝研究院

Menu

简单AI自动寻路

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class AutoWayTest : MonoBehaviour {
    static AutoWayTest [] autoWayTest;
    public List<AutoWayTestconnectWayPoint;
    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;
    }
}

打赏
— 于 共写了1532个字
— 文内使用到的标签:

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据