{"id":1093,"date":"2017-04-21T17:16:18","date_gmt":"2017-04-21T09:16:18","guid":{"rendered":"http:\/\/39.100.100.179\/?p=1093"},"modified":"2017-04-25T11:49:40","modified_gmt":"2017-04-25T03:49:40","slug":"%e4%b8%8d%e5%ae%8c%e7%be%8e%e8%a7%a3%e5%86%b3%e7%be%a4%e4%bd%93%e5%af%bb%e8%b7%af%e6%97%b6%e7%89%a9%e4%bd%93%e4%b9%8b%e9%97%b4%e6%8b%a5%e6%8c%a4%e3%80%81%e7%a2%b0%e6%92%9e%e9%97%ae%e9%a2%98","status":"publish","type":"post","link":"http:\/\/www.u3d8.com\/?p=1093","title":{"rendered":"(\u4e0d\u5b8c\u7f8e)\u89e3\u51b3\u7fa4\u4f53\u5bfb\u8def\u65f6\u7269\u4f53\u4e4b\u95f4\u62e5\u6324\u3001\u78b0\u649e\u95ee\u9898"},"content":{"rendered":"<p>\u8bf4\u4e0d\u5b8c\u7f8e\uff0c\u662f\u56e0\u4e3a\u4f1a\u51fa\u73b0\u4e9b\u95ee\u9898<\/p>\n<p>1.\u6709\u4e2a\u522b\u7269\u4f53\u6ca1\u5230\u8fbe\u76ee\u6807\u70b9\u9644\u8fd1\u5c31\u505c\u6b62\u4e86<\/p>\n<p>2.\u5f53\u4e24\u6b21\u76ee\u6807\u70b9\u8ddd\u79bb\u8f83\u8fd1\u65f6\uff0c\u7269\u4f53\u53ef\u80fd\u90e8\u5206\u4f1a\u91cd\u53e0<\/p>\n<p>\u5b9e\u73b0\u65b9\u6cd5\uff1a<\/p>\n<p>\u4f7f\u7528Nav Mesh Obstacle<\/p>\n<p>\u5728\u5bfb\u8def\u8fc7\u7a0b\u4e2d\uff0c\u5173\u95ed\u969c\u788d\u7ec4\u4ef6\uff0c<\/p>\n<p>\u5bfb\u8def\u7ed3\u675f\uff0c\u5173\u95ed\u5bfb\u8def\u7ec4\u4ef6\uff0c\u6253\u5f00\u969c\u788d\u7ec4\u4ef6\u3002<\/p>\n<p><a href=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/04\/PathFinding.gif\" data-lightbox=\"image_lg\"><img title=\"(\u4e0d\u5b8c\u7f8e)\u89e3\u51b3\u7fa4\u4f53\u5bfb\u8def\u65f6\u7269\u4f53\u4e4b\u95f4\u62e5\u6324\u3001\u78b0\u649e\u95ee\u9898 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\" alt=\"(\u4e0d\u5b8c\u7f8e)\u89e3\u51b3\u7fa4\u4f53\u5bfb\u8def\u65f6\u7269\u4f53\u4e4b\u95f4\u62e5\u6324\u3001\u78b0\u649e\u95ee\u9898 - \u7b2c1\u5f20  | u3d8\u6280\u672f\u5206\u4eab\"  loading=\"lazy\" class=\"aligncenter size-full wp-image-1095\"  data-src=\"http:\/\/www.u3d8.com\/wp-content\/uploads\/2017\/04\/PathFinding.gif\" width=\"585\" height=\"330\" \/><\/a><\/p>\n<p>\u5efa\u8bae\u4e0d\u8981\u5728\u516c\u53f8\u9879\u76ee\u4e2d\u4f7f\u7528\uff0c\u4e5f\u671f\u5f85\u6709\u7f51\u53cb\u80fd\u5206\u4eab\u5176\u5b83\u66f4\u597d\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<pre class=\"lang:c# decode:true\">using UnityEngine;\r\nusing System.Collections;\r\nusing System.Collections.Generic;\r\n\r\npublic class NewBehaviourScript : MonoBehaviour\r\n{\r\n    public GameObject targetGo;\r\n\r\n    public List&lt;NavMeshAgent&gt; agents;\r\n\r\n    void Start()\r\n    {\r\n        StartCoroutine( IUpdate() );\r\n    }\r\n\r\n    IEnumerator IUpdate()\r\n    {\r\n        while ( true )\r\n        {\r\n            \/\/ \u6309\u4e0b\u9f20\u6807\uff0c\u5f00\u59cb\u5bfb\u8def\r\n            if ( Input.GetMouseButtonDown( 0 ) )\r\n            {\r\n                Ray ray = Camera.main.ScreenPointToRay( Input.mousePosition );\r\n                RaycastHit hitInfo;\r\n                if ( Physics.Raycast( ray, out hitInfo ) )\r\n                {\r\n                    targetGo.transform.position = hitInfo.point + new Vector3(0, 0.5f, 0);\r\n\r\n                    for ( int i = 0; i &lt; agents.Count; i++ )\r\n                    {\r\n                        agents[i].GetComponent&lt;NavMeshObstacle&gt;().enabled = false;\r\n                        yield return null;      \/\/ \u8fd9\u91cc\u5fc5\u987b\u8981\u7b49\u5f85\u4e00\u5e27\uff0c\u5426\u5219 \u4e0b\u4e00\u6b21\u5bfb\u8def\u5c31\u4f1a\u9519\u4f4d\u3002\r\n                        agents[i].enabled = true;\r\n                        agents[i].SetDestination( hitInfo.point );\r\n                    }\r\n                }\r\n            }\r\n\r\n            yield return null;\r\n\r\n            \/\/ \u52a8\u6001\u8bbe\u7f6e\u4f18\u5148\u7ea7\r\n            if ( agents[0].enabled )\r\n            {\r\n                agents.Sort( ( x, y ) =&gt;\r\n                {\r\n                    if ( x.enabled &amp;&amp; y.enabled )\r\n                    {\r\n                        return x.remainingDistance.CompareTo( y.remainingDistance );\r\n                    }\r\n                    return 0;\r\n                } );\r\n\r\n                for ( int i = 0; i &lt; agents.Count; i++ )\r\n                {\r\n                    agents[i].avoidancePriority = 50 + i;\r\n                }\r\n            }\r\n\r\n            \/\/ \u5224\u65ad\u5230\u8fbe\u7ed3\u675f\u70b9 \u6253\u5f00\u969c\u788d\u7ec4\u4ef6\r\n            for ( int i = 0; i &lt; agents.Count; i++ )\r\n            {\r\n                if ( agents[i].enabled &amp;&amp; agents[i].remainingDistance &gt; 0.5f &amp;&amp; agents[i].remainingDistance &lt; 0.8f )\r\n                {\r\n                    agents[i].Stop();\r\n                    agents[i].enabled = false;\r\n                    agents[i].GetComponent&lt;NavMeshObstacle&gt;().enabled = true;\r\n                }\r\n            }\r\n        }\r\n    }\r\n}<\/pre>\n<p>\u9879\u76ee\u4f7f\u7528\u7248\u672c\uff1aUnity5.3.6 \u00a0 \u00a0GitHub\u4e0b\u8f7d\u5730\u5740\uff1a<\/p>\n<p><a href=\"https:\/\/github.com\/654306663\/NavMeshAgent\">https:\/\/github.com\/654306663\/NavMeshAgent<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u8bf4\u4e0d\u5b8c\u7f8e\uff0c\u662f\u56e0\u4e3a\u4f1a\u51fa\u73b0\u4e9b\u95ee\u9898 1.\u6709\u4e2a\u522b\u7269\u4f53\u6ca1\u5230\u8fbe\u76ee\u6807\u70b9\u9644\u8fd1\u5c31\u505c\u6b62\u4e86 2.\u5f53\u4e24\u6b21 &hellip; <a href=\"http:\/\/www.u3d8.com\/?p=1093\">\u7ee7\u7eed\u9605\u8bfb <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[245,243,244],"_links":{"self":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1093"}],"collection":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1093"}],"version-history":[{"count":5,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1093\/revisions"}],"predecessor-version":[{"id":1119,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=\/wp\/v2\/posts\/1093\/revisions\/1119"}],"wp:attachment":[{"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1093"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1093"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.u3d8.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1093"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}