{"id":2158,"date":"2020-03-29T17:45:53","date_gmt":"2020-03-29T09:45:53","guid":{"rendered":"https:\/\/www.clonefactor.com\/wordpress\/?p=2158"},"modified":"2020-03-29T17:48:27","modified_gmt":"2020-03-29T09:48:27","slug":"%e4%be%bf%e7%95%b6%e7%b4%9a%e7%9a%84%e6%80%aa%e7%89%a9-locomotion-navmeshagent","status":"publish","type":"post","link":"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/animator-%e5%ad%b8%e7%bf%92%e7%ad%86%e8%a8%98\/2158\/","title":{"rendered":"\u4fbf\u7576\u7d1a\u7684\u602a\u7269 Locomotion + NavMeshAgent"},"content":{"rendered":"\n<h2>\u524d\u8a00<\/h2>\n<p>\u9019\u7bc7\u66f4\u50cf\u662f\u4e00\u500b\u7406\u7a0b\u7891\u7684\u6771\u897f, \u7b97\u662f\u70ba\u81ea\u5df1\u5728\u516c\u5728\u79c1\u7684\u96fb\u8166 AI \u7279\u5225\u662f\u6575\u4ebaEnemy \u6216 NPC \u7684\u4e00\u500b\u5c0f\u7e3d\u7d50.<\/p>\n<p><a href=\"https:\/\/docs.unity3d.com\/ScriptReference\/AI.NavMeshAgent.html\">NavMeshAgent<\/a> \u662f\u4e00\u500b\u5167\u5efa\u7684\u5c0b\u8def\u7cfb\u7d71 Pathfinding System U3D 5\u4ee3\u4e4b\u5f8c\u7684\u9084\u662f\u76f8\u7576\u53ef\u9760\u7684,\u00a0<br \/><del>\u9019\u908a\u8981\u984d\u5916\u7684\u5ba3\u50b3\u4e00\u4e0b Unity \u6709\u4e00\u500b\u5c0f\u5c08\u6848 <a href=\"https:\/\/github.com\/Unity-Technologies\/NavMeshComponents\">NavMeshComponents<\/a> \u4f86\u52a0\u5f37\u5957\u4ef6\u7684\u529f\u80fd.<\/del><br \/><del>\u5728\u4f60\u60f3\u4f7f\u7528\u4efb\u4f55\u5176\u4ed6\u7684 Pathfinding \u65b9\u5f0f\u4e4b\u524d\u6700\u597d\u5148\u4e86\u89e3\u4e00\u4e0b\u9019\u500b\u5c0f\u5c08\u6848.<\/del><\/p>\n<p>\u6309 : \u5728 U3D 2018 \u7248\u5f8c NavMeshComponents \u5927\u90e8\u4efd\u529f\u80fd\u5df2\u7d93\u8b8a\u70ba\u5167\u5efa. Yes,\u53ef\u4ee5\u900f\u904e NavMeshBuilder \u4f86\u9032\u884c runtime map bake.<\/p>\n<p>\u00a0<\/p>\n<h2>\u600e\u6a23\u958b\u59cb?!<\/h2>\n<p>\u70ba NPC \u5011\u5957\u52d5\u756b\u4ee5\u7d93\u9a57\u4f86\u8aaa\u6211\u5011\u7d93\u5e38\u9700\u8981\u53d6\u5f97\u4e00\u500b GameObject \u7684\u4e0b\u4ee5\u8cc7\u8a0a<\/p>\n<ul>\n<li>\u4e0a\u4e00\u5e40\n<ul>\n<li>\u4f4d\u7f6e (Position)<\/li>\n<li>\u9762\u5411 (Rotation)<\/li>\n<\/ul>\n<\/li>\n<li>\u00a0\u79fb\u52d5\u5411\u91cf(Vector)\n<ul>\n<li>\u65b9\u5411 (Direction)<\/li>\n<li>\u901f\u5ea6(Speed)<\/li>\n<li>\u8ddd\u96e2(Distance)<\/li>\n<\/ul>\n<\/li>\n<li>\u4ee3\u8868\u7ad9\u7acb\u5e73\u9762\u7684\u4ee3\u8868 (Vector -GroundNormal)<\/li>\n<\/ul>\n<p>\u5728\u5beb\u8907\u96dc AI \u7684\u6642\u5019\u5982\u679c\u7d93\u5e38\u8a08\u7b97\u4ee5\u4e0a\u7684\u6771\u897f\u662f\u5f88\u82b1\u8cc7\u6e90\u7684,<br \/>e.g. vector \u7684 distance \u7b97\u5f0f\u4e2d\u7684 \u5e73\u65b9\u6839(Square Root Calculations) \u662f\u8f03\u6162\u7684\u51fd\u6578\u8a08\u7b97\u65b9\u5f0f<br \/>\u4f46\u662f\u5982\u679c\u8003\u616e\u5230\u5176\u6cdb\u7528\u6027\u7684\u8a71, \u6bcf\u5e40\u53ea\u8a08\u7b97\u4e00\u6b21\u518d\u65bc\u591a\u500b\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u7684\u8a71\u5012\u662f\u6c92\u6709\u751a\u9ebc\u5927\u554f\u984c.<\/p>\n<p>\u7d9c\u5408\u4ee5\u4e0a\u7684\u9700\u8981\u6211\u81ea\u5df1\u5beb\u4e86\u4e00\u500b\u4fbf\u7576\u5f0f\u7684\u6578\u64da\u96c6\u5408\u5eab\u4f86\u8a08\u7b97\u4e26\u66ab\u5b58\u7279\u5b9a GameObject \u7684\u76f8\u95dc\u8cc7\u8a0a, \u6709\u9019\u500b\u505a\u57fa\u5c64\u7684\u8a71\u5176\u5f8c\u9700\u8981\u4f7f\u7528\u6642\u76f4\u63a5\u5f15\u7528\u9019\u500b\u5143\u4ef6\u5c31\u53ef\u4ee5\u8df3\u904e\u5927\u90e8\u4efd\u7684\u904b\u7b97\u4f86\u53d6\u5f97\u7b54\u6848.<br \/>PS: \u7576\u4e2d\u7684 Gizmos \u662f\u81ea\u5df1\u7528\u4f86\u7e6a\u756b\u7dda\u689d\u7684 API, \u7528\u4f5c\u756b\u9762 debug \u7528\u9014.<\/p>\n<h2>\u4e00\u4e9b\u57fa\u790e<\/h2>\n<p>\u9019\u5176\u5be6\u4e00\u822c\u7a0b\u5e8f\u90fd\u6c92\u6709\u7279\u5225\u5728\u610f\u9019\u64cb\u4e8b, \u4f46\u5728\u88fd\u4f5c A \u7d1a\u5927\u4f5c\u7684\u6642\u5019\u662f\u5f88\u91cd\u8981\u7684\u4e00\u74b0. \u9664\u975e~ \u4f60\u80fd\u5920\u63a5\u53d7 NPC \u7684\u8173\u6b65\u5728\u5730\u9762\u4e0a\u6f02\u79fb.<br \/>\u5148\u8aaa\u660e\u4e00\u4e9b\u7c21\u55ae\u7684\u55ae\u4f4d\u6982\u5ff5, \u9084\u771f\u7684\u6eff\u591a\u4eba\u4e0d\u77e5\u9053\u7684.<br \/>\u9996\u5148 U3D \u5f15\u64ce\u7684 1 Unit \u5176\u53c3\u8003\u5c31\u662f 1 \u7c73. \u5728 Editor \u4e2d\u5efa\u7acb\u4e00\u500b\u9810\u8a2d\u7684 Cube \u5c31\u662f 1m x 1m x 1m \u7684\u5927\u5c0f.<br \/><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2159\" src=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2019\/10\/defaultCube_1m.jpg\" alt=\"\" width=\"457\" height=\"432\" srcset=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2019\/10\/defaultCube_1m.jpg 457w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2019\/10\/defaultCube_1m-300x284.jpg 300w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2019\/10\/defaultCube_1m-317x300.jpg 317w\" sizes=\"auto, (max-width: 457px) 100vw, 457px\" \/><\/p>\n<h3>\u800c NavMeshAgent<\/h3>\n<h4><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2161\" src=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2019\/10\/U3D2018.3.6_NavMeshAgent.jpg\" alt=\"\" width=\"448\" height=\"354\" srcset=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2019\/10\/U3D2018.3.6_NavMeshAgent.jpg 448w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2019\/10\/U3D2018.3.6_NavMeshAgent-300x237.jpg 300w, https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2019\/10\/U3D2018.3.6_NavMeshAgent-359x284.jpg 359w\" sizes=\"auto, (max-width: 448px) 100vw, 448px\" \/><br \/>Steering<\/h4>\n<ul>\n<li>Speed \u4ea6\u662f\u9019\u500b AI \u5728\u6c92\u6709\u5e72\u64fe\u4e4b\u4e0b\u6240\u80fd\u884c\u8d70\u7684\u6700\u9ad8\u901f\u5ea6 (\u5047\u8a2d\u76f4\u7dda\u53ca\u6c92\u6709\u4efb\u4f55\u7269\u7406\u56e0\u7d20)<\/li>\n<li>Angular Speed \u662f AI \u6700\u5feb\u7684\u65cb\u8f49\u901f\u5ea6.<\/li>\n<li>Acceleration \u662f\u52a0\u901f\u5ea6\u561b, \u975c\u6b62\u52a0\u901f, \u8f49\u89d2\u6f02\u79fb \u6703\u770b\u9019\u500b\u503c.<\/li>\n<li>Stopping Distance \u5230\u9054\u76ee\u7684\u5730\u6709\u591a\u7cbe\u78ba.<\/li>\n<li>Auto Braking \u5dee\u4e0d\u591a\u5f8c\u9054\u7684\u8ddd\u96e2\u6703\u6709\u6e1b\u901f\u7684\u60c5\u6cc1, \u4f86\u907f\u514d\u8d85\u8d8a\u7d42\u9ede Overshoot<\/li>\n<\/ul>\n<h2>\u4e00\u500b\u7269\u4ef6\u8dd1\u591a\u5feb?!<\/h2>\n<p>\u8981\u77e5\u9053NavMeshAgent \u8dd1\u591a\u5feb\u624d\u80fd\u77e5\u9053\u8981\u653e\u751a\u9ebc\u52d5\u756b. (\u5ee2\u8a71),\u00a0Speed, Time, Distance \u7684\u63db\u7b97\u500b\u4eba\u8a8d\u70ba\u662f\u5fc5\u4fee\u7684.<br \/>\u7d9c\u5408\u4ee5\u4e0a, \u6211\u5011\u53ea\u8981\u8a18\u9304\u4ee5\u4e0b\u7684\u8cc7\u8a0a\u5373\u53ef<\/p>\n<ul>\n<li>\u6642\u9593 &#8211; \u7531\u4e0a\u5e40\u7684 Update \u5230\u73fe\u5728\u7684 Update \u7684\u6642\u9593, \u5c31\u662f Time.deltaTime, \u9019\u662f\u7cfb\u7d71\u7d66\u7684\u76f4\u63a5\u7528\u5c31\u53ef\u4ee5<\/li>\n<li>\u65b9\u4f4d &#8211; \u7531\u4e0a\u5e40\u5230\u73fe\u5728\u7684\u7684\u79fb\u52d5\u8ddd\u96e2 lastPosition &#8211; currentPosition, \u9019\u4e00\u652f Vector3 \u5c31\u542b\u6709\u65b9\u5411\u8cc7\u8a0a.<\/li>\n<li>\u8ddd\u96e2 &#8211; \u7531&#8221;\u65b9\u4f4d&#8221;\u63db\u7b97\u51fa\u7684 length \u5c31\u662f\u79fb\u52d5\u8ddd\u96e2. \u5373\u662f (lastPosition &#8211; currentPosition).magnitude<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Vector3 lastPosition;\nprivate void Update()\n{\n    float time = Time.deltaTime;\n    Vector3 currentPosition = transform.position;\n    \n    Vector3 movement = lastPosition - currentPosition;\n    float distance = movement.magnitude;\n    float speed = distance \/ time;\n    \n    \/\/\n    \/\/ Update the lastPosition at the end\n    lastPosition = currentPosition;\n}<\/pre>\n<p>\u6191\u85c9 distance, time, speed \u7684\u904b\u7b97\u6211\u5011\u53ef\u4ee5\u505a\u5f88\u591a\u4e8b.<br \/>\u5305\u62ec\u52d5\u756b\u7684\u5207\u63db\u53ca\u63d2\u653e, \u4e00\u4e9b\u904a\u6232\u7684\u89f8\u767c\u6a5f\u5236\u7b49\u7b49.<\/p>\n<h2>\u600e\u6a23\u70ba AI \u8a2d\u4e00\u500b\u7c21\u55ae\u7684 Locomotion.<\/h2>\n<p>Locomotion \u7c21\u800c\u8a00\u4e4b\u5c31\u662f\u4e00\u5957\u57fa\u65bc\u4e0d\u540c\u65b9\u5411\u4e0b\u7684\u57fa\u790e\u884c\u8d70\u52d5\u756b\u7d44 (\u8a73\u898b <a href=\"https:\/\/www.clonefactor.com\/wordpress\/program\/unity3d\/devlog\/2042\/\">\u6211\u7684\u4eba\u5f62\u63a7\u5236\u958b\u767c\u7b46\u8a18(\u4e00) Locomotion<\/a>)<br \/>\u55ef, \u8df3 (jump) \u548c\u653b\u64ca (Attack) \u662f<span style=\"color: #ff0000;\"><strong>\u4e0d\u5305\u62ec<\/strong><\/span>\u5728\u5167\u7684.<br \/>\u5728\u73fe\u4ee4\u7684 3D \u904a\u6232\u4f86\u8aaa, \u5168\u65b9\u5411 Locomotion \u5df2\u7d93\u662f\u57fa\u672c\u4e86.<br \/>\u6709 8 \u500b\u4e0d\u540c\u65b9\u5411\u7684\u9aa8\u9abc\u884c\u8d70\u52d5\u756b\u5c31\u53ef\u4ee5\u5728 Animator \u88e1\u505a\u5230\u4e0b\u9762\u7684\u6548\u679c.<br \/>(PS: \u5982\u679c\u4e0d\u77e5\u9053\u9019\u500b\u756b\u9762, \u81ea\u5df1\u5230 Animator \u88e1\u627e\u4e00\u4e0b\u5982\u4f55\u52a0\u5165 <a href=\"https:\/\/docs.unity3d.com\/Manual\/class-BlendTree.html\"><strong>Blend Tree<\/strong><\/a>)<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2184\" src=\"https:\/\/www.clonefactor.com\/wordpress\/wp-content\/uploads\/2019\/12\/basic_locomotion_demo.gif\" alt=\"\" width=\"290\" height=\"519\" \/><\/p>\n<p>\u7136\u5f8c\u5c31\u5982\u540c\u524d\u9762\u6240\u63d0\u5230\u7684\u628a Speed \u653e\u9032\u53bb\u5c31\u53ef\u4ee5.<br \/>\u57fa\u672c\u4e0a\u628a\u524d\u9762\u7684\u4ee3\u78bc\u6539\u4e00\u9ede..<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Vector3 lastPosition;\nprivate void Update()\n{\n    float time = Time.deltaTime;\n    Vector3 currentPosition = transform.position;\n    \n    Vector3 movement = lastPosition - currentPosition;\n    Vector3 localMovement = transform.InverseTransformVector(movement);\n    Vector3 local3DSpeed = localMovement \/ time;\n    \n    animator.SetFloat(\"LocalX\", local3DSpeed.x);\n    animator.SetFloat(\"LocalZ\", local3DSpeed.z); \/\/ X &amp; Z = Horizontal movement;\n    \n    animator.SetFloat(\"LocalY\", local3DSpeed.y); \/\/ Up \/ Down movement\n    \n    \/\/\n    \/\/ Update the lastPosition at the end\n    lastPosition = currentPosition;\n}<\/pre>\n<p>local3DSpeed \u5c31\u662f\u57fa\u65bc\u79fb\u52d5\u7269\u672c\u8eab\u7684 3 \u8ef8\u901f\u5ea6, <br \/>\u5047\u8a2d Animator \u7684 Blend Tree \u8a2d\u5b9a\u6c92\u6709\u51fa\u932f\u7684\u8a71, \u9019\u652f Vector \u5c31\u4ee3\u8868\u4e86\u4eba\u7269\u7684\u6c34\u5e73\u79fb\u52d5\u901f\u5ea6 (X + Z) \u8ef8.<br \/>\u5230\u6b64\u5c31\u53ef\u4ee5\u5efa\u69cb\u4e00\u500b\u6709\u57fa\u672c 360\u5ea6\u884c\u8d70\u7684\u602a\u7269.<\/p>\n<p>\u5982\u679c\u5c0d Vector \u6982\u5ff5\u4e0d\u719f\u6089\u7684\u53ef\u4ee5\u518d\u6eab\u7fd2\u4e00\u4e0b<a href=\"https:\/\/www.clonefactor.com\/wordpress\/program\/math\/2186\/\">\u6383\u76f2\u7cfb\u5217 : Vector \u77e2\u91cf<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u524d\u8a00 \u9019\u7bc7\u66f4\u50cf\u662f\u4e00\u500b\u7406\u7a0b\u7891\u7684\u6771\u897f, \u7b97\u662f\u70ba\u81ea\u5df1\u5728\u516c\u5728\u79c1\u7684\u96fb\u8166 AI \u7279\u5225\u662f\u6575\u4ebaEnemy \u6216 NPC &hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[105,107,106],"tags":[108],"class_list":["post-2158","post","type-post","status-publish","format-standard","hentry","category-animator-","category-charactercontroller","category-devlog","tag-locomotion"],"_links":{"self":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2158","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/comments?post=2158"}],"version-history":[{"count":6,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2158\/revisions"}],"predecessor-version":[{"id":2217,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/posts\/2158\/revisions\/2217"}],"wp:attachment":[{"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/media?parent=2158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/categories?post=2158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.clonefactor.com\/wordpress\/wp-json\/wp\/v2\/tags?post=2158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}