少年修仙传客户端代码仓库
client_Hale
2019-04-15 f99a0cd6ed9f5df666b19549e6a7de9bf9b9e9c8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using UnityEngine;
using UnityEngine.AI;
 
public class PathFinder
{
    public static Vector3 AdjustPosition(Vector3 position)
    {
        NavMeshHit _navMeshHit;
        position.y = 0;
        if (NavMesh.SamplePosition(position, out _navMeshHit, 10, NavMesh.AllAreas))
        {
            return _navMeshHit.position;
        }
        return position;
    }
 
    private static NavMeshPath _path = new NavMeshPath();
    public static bool WalkAble(Vector3 source, Vector3 dest, float dist = 0.01f)
    {
        source.y = 0;
        dest.y = 0;
        NavMesh.CalculatePath(source, dest, -1, _path);
        if (_path.corners != null && _path.corners.Length > 0)
        {
            return MathUtility.DistanceSqrtXZ(dest, _path.corners[_path.corners.Length - 1]) < dist;
        }
        return false;
    }
 
    public static float CalculateDistance(Vector3 start, Vector3 dest)
    {
        float _dis = float.MaxValue;
        start.y = 0;
        dest.y = 0;
        NavMesh.CalculatePath(start, dest, -1, _path);
        if (_path.corners != null && _path.corners.Length > 1)
        {
            _dis = 0;
            for (int i = 0; i < _path.corners.Length - 1; ++i)
            {
                _dis += Vector3.Distance(_path.corners[i], _path.corners[i + 1]);
            }
        }
        return _dis;
    }
 
    public static bool TryGetValidDest(Vector3 start, Vector3 dest, ref Vector3 validDest)
    {
        dest = AdjustPosition(dest);
        start.y = 0;
        dest.y = 0;
        NavMesh.CalculatePath(start, dest, -1, _path);
        if (_path.corners != null && _path.corners.Length > 0)
        {
            validDest = _path.corners[_path.corners.Length - 1];
            return true;
        }
        return false;
    }
}