少年修仙传客户端代码仓库
client_Wu Xijin
2019-03-05 efa5f8d07fc3321f6ac5f5d97fb422db28d0886f
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
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)
    {
        source.y = 0;
        dest.y = 0;
        NavMesh.CalculatePath(source, dest, -1, _path);
        if (_path.corners != null && _path.corners.Length > 0)
        {
            return true;
        }
        return false;
    }
 
    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;
    }
}