{"id":3464,"date":"2025-08-12T01:16:39","date_gmt":"2025-08-11T17:16:39","guid":{"rendered":"https:\/\/tobykskgd.life\/?p=3464"},"modified":"2025-08-12T01:16:42","modified_gmt":"2025-08-11T17:16:42","slug":"112","status":"publish","type":"post","link":"https:\/\/tobykskgd.life\/index.php\/112\/","title":{"rendered":"\u9762\u5411\u521d\u5b66\u8005\u7684 C# \u548c Unity \u8bfe\u7a0b\u5b66\u4e60\u4ed3\u5e93\u4e0e\u7b14\u8bb0"},"content":{"rendered":"\n<p>Udemy \u4e0a\u9762\u7684 Alex Dev \u7684\u8bfe\u7a0b Basics of C# and Unity for Complete Beginners &#8211; Part 1<\/p>\n\n\n\n<p>\u94fe\u63a5\uff1a<a href=\"https:\/\/www.udemy.com\/course\/free-part1-alexdev\/\">Free C# (programming language) Tutorial &#8211; Basics of C# and Unity for Complete Beginners &#8211; Part 1 | Udemy<\/a><\/p>\n\n\n\n<p>\u73b0\u5728\u662f 2025 \u5e74\u7684 7 \u6708\u4e2d\u65ec\uff0c\u6211\u53d1\u73b0\u8fd9\u95e8\u8bfe\u5728 Udemy \u4e0a\u5df2\u7ecf\u4e0b\u67b6\u4e86\uff0c\u4e0d\u8fc7\u4e4b\u524d\u8ba2\u9605\u7684\u8fd8\u53ef\u4ee5\u770b\uff0c\u867d\u7136\u770b\u4e0d\u4e86\u89c6\u9891\u4e86\uff0c\u4f46\u4e2a\u4eba\u89c9\u5f97\u8fd9\u95e8\u8bfe\u8fd8\u662f\u975e\u5e38\u6709\u4ef7\u503c\u7684\uff0c\u6545\u4f9d\u65e7\u9009\u62e9\u770b\u5b8c\u5e76\u5c06\u7b14\u8bb0\u4e0a\u4f20\u3002\u3010\u8fd8\u6709\u4e2a Part 2 \u4e0d\u77e5\u9053\u53bb\u54ea\u770b\u4e86 \ud83d\ude41 \u3011<\/p>\n\n\n\n<p>\u672c\u7bc7\u6587\u7ae0\u7684\u4ed3\u5e93\uff0c\u4f60\u4e5f\u53ef\u4ee5\u5728\u4ed3\u5e93\u4e2d\u67e5\u770b\u672c\u6587\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>GitHub\uff1a<a href=\"https:\/\/github.com\/TobyKSKGD\/Udemy-course---Onion-Lad\">TobyKSKGD\/Udemy-course&#8212;Onion-Lad: Udemy \u4e0a\u9762\u7684 Alex Dev \u8bfe\u7a0b Basics of C# and Unity for Complete Beginners &#8211; Part 1 \u7684\u4ed3\u5e93\u4e0e\u7b14\u8bb0<\/a><\/li>\n\n\n\n<li>Gitee\uff1a<a href=\"https:\/\/gitee.com\/tobykskgd\/udemy_course-onion_lad\">Udemy course &#8211; Onion Lad: Udemy \u4e0a\u9762\u7684 Alex Dev \u8bfe\u7a0b Basics of C# and Unity for Complete Beginners &#8211; Part 1 \u7684\u4ed3\u5e93\u4e0e\u7b14\u8bb0<\/a><\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Section 1: BEFORE YOU START<\/h2>\n\n\n\n<p>\u4e00\u4e9b\u8bfe\u7a0b\u76f8\u5173\u7684\u8bf4\u660e\u4e0e\u4ecb\u7ecd\uff0c\u65e0\u77e5\u8bc6\u8981\u70b9\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Section 2: First steps<\/h2>\n\n\n\n<p>Tools \u5de5\u5177\u680f\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/tobykskgd.life\/wp-content\/uploads\/2025\/08\/tool-bar.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"335\" height=\"46\" data-original=\"https:\/\/tobykskgd.life\/wp-content\/uploads\/2025\/08\/tool-bar.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3466\"  sizes=\"auto, (max-width: 335px) 100vw, 335px\" \/><\/div><\/figure>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u7b80\u5355\u5feb\u6377\u952e\uff08\u6309\u5de5\u5177\u680f\u7684\u4f4d\u7f6e\u987a\u5e8f\u6392\u5e8f\uff09\n<ul class=\"wp-block-list\">\n<li><code>Q<\/code>\uff1a\u89c6\u56fe\u5de5\u5177\uff08View Tool\uff09<\/li>\n\n\n\n<li><code>W<\/code>\uff1a\u79fb\u52a8\u5de5\u5177\uff08Move Tool\uff09<\/li>\n\n\n\n<li><code>E<\/code>\uff1a\u65cb\u8f6c\u5de5\u5177\uff08Rotate Tool\uff09<\/li>\n\n\n\n<li><code>R<\/code>\uff1a\u7f29\u653e\u5de5\u5177\uff08Scale Tool\uff09<\/li>\n\n\n\n<li><code>T<\/code>\uff1a\u77e9\u5f62\u5de5\u5177\uff08Rect Tool\uff09<\/li>\n\n\n\n<li><code>Y<\/code>\uff1a\u53d8\u5f62\u5de5\u5177\uff08Transform Tool\uff09<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>Hierarchy \u4e2d\u53ef\u4ee5\u521b\u5efa\u7b80\u5355\u7684\u56fe\u5f62\u53f3\u952e\u9009\u62e9 2D Object \u53ef\u4ee5\u770b\u5230\u76f8\u5173\u56fe\u5f62\u3002<\/p>\n\n\n\n<p>\u9009\u4e2d\u56fe\u5f62\u5728 Inspector \u53ef\u4ee5\u4fee\u6539\u56fe\u5f62\u7684\u76f8\u5173\u5c5e\u6027\u3002\u7b80\u5355\u7684\u5982\uff1a\u4f4d\u7f6e\uff08Position\uff09\u3001\u65cb\u8f6c\uff08Rotation\uff09\u3001\u89c4\u6a21\uff08Scale\uff09\u3001\u989c\u8272\uff08Color\uff09\u7b49\u3002Transform \u4e2d\u7684\u56fe\u50cf\u76f8\u5173\u53c2\u6570\u8c03\u6574\u53ef\u4ee5\u901a\u8fc7\u5c06\u9f20\u6807\u653e\u5728 X\u3001Y\u3001Z \u8f74\u5b57\u6bcd\u4e0a\u9762\u7136\u540e\u5de6\u53f3\u6ed1\u52a8\u6765\u5feb\u901f\u8c03\u6574\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u56fe\u50cf\u4e2d\u7684 Add Component \u53ef\u4ee5\u7ed9\u56fe\u5f62\u6dfb\u52a0\u989d\u5916\u7684\u5c5e\u6027\u3002\u6bd4\u5982\u8fd9\u91cc\u6d89\u53ca\u5230\u7684\u51e0\u4e2a\uff1aRigidbody 2D\uff08\u7c7b\u4f3c\u4e8e\u7ed9\u7269\u4f53\u52a0\u4e0a\u4e00\u4e2a\u5411\u4e0b\u7684\u91cd\u529b\uff0c\u5982\u679c\u4ece\u4e2d\u6ca1\u6709\u78b0\u5230\u5b9e\u4f53\u5c06\u4f1a\u4e00\u76f4\u5f80\u4e0b\u6389\u843d\uff09\u3001Circle Collider 2D\uff082D \u5706\u5f62\u7684\u78b0\u649e\u4f53\uff1a\u8d4b\u4e88\u56fe\u5f62\u53ef\u4ea4\u4e92\u7684\u5b9e\u4f53\uff09\u3001Box Collider 2D\uff082D \u77e9\u5f62\u7684\u78b0\u649e\u4f53\uff09\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Rigidbody 2D \u4e2d\u7684\u4e00\u4e9b\u5c5e\u6027\uff1a\n<ul class=\"wp-block-list\">\n<li><code>Gravity Scale<\/code>\uff1a<code>1<\/code> \u9ed8\u8ba4\u6709\u4e00\u4e2a\u5411\u4e0b\u7684\u91cd\u529b\uff08\u53ef\u4ee5\u901a\u8fc7\u6539\u53d8 Gravity Scale \u6765\u4fee\u6539\u4e0b\u843d\u901f\u5ea6\uff09\uff1b<code>0<\/code> \u5728\u88ab\u5176\u5b83\u7269\u4f53\u78b0\u649e\u4e4b\u524d\u4e0d\u4f1a\u53d7\u91cd\u529b\u5f71\u54cd\u3002<\/li>\n\n\n\n<li><code>Constraints<\/code>\uff1a\u5176\u4e2d\u6709 <code>Freeze Position<\/code> \u548c <code>Freeze Rotation<\/code> \u51bb\u7ed3\u4e0d\u540c\u5750\u6807\u4f5c\u7528\uff0c\u5b9e\u5728\u6765\u8bf4\u6bd4\u5982\u52fe\u9009 Freeze Position \u4e2d\u7684 X \u548c Y \u6b64\u65f6\u5982\u679c\u4e00\u4e2a\u78b0\u649e\u4f53\u4ece\u5de6\u53f3\u4efb\u4f55\u4e00\u904d\u4e0b\u843d\uff08Gravity Scale \u7684\u5c5e\u6027\u9700\u8981\u662f 0\uff09\uff0c\u5c06\u4f1a\u662f\u8be5\u56fe\u5f62\u65cb\u8f6c\u3002\u3010Maybe we can play with physics!\u3011<\/li>\n\n\n\n<li><code>Body Type<\/code>\uff1a\u7269\u4f53\u7684\u72b6\u6001\u3002\u9ed8\u8ba4\u4e3a <code>Dynamic<\/code>\uff1b\u82e5\u5c06\u5176\u6539\u4e3a <code>Static<\/code> \u90a3\u4e48\u8fd9\u4e2a\u7269\u4f53\u5c06\u9759\u6b62\u4e0d\u53d8\uff0c\u65e0\u8bba\u662f\u5426\u5bf9\u5b83\u5e94\u7528 Physics\uff0c\u6216\u8005\u901a\u8fc7 Transform \u6539\u53d8\u4f4d\u7f6e\uff1b\u53e6\u5916\u8fd8\u6709\u4e00\u79cd\u72b6\u6001\u4e3a <code>Kinematic<\/code>\uff0c\u6b64\u65f6\u7269\u4f53\u4e0d\u4f1a\u53d7\u7269\u7406\u5f71\u54cd\uff08\u91cd\u529b\u3001\u78b0\u649e\u7b49\uff09\uff0c\u4f46\u662f\u53ef\u4ee5\u901a\u8fc7\u4ee3\u7801\u7b49\u65b9\u5f0f\u6539\u53d8\u5176\u4f4d\u7f6e\u6216\u79fb\u52a8\u901f\u5ea6\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p>\u5728 Project \u4e2d\u7684 Assets \u53f3\u952e\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e9b\u4e1c\u897f\u3002\u53f3\u952e -&gt; Create -&gt; 2D -&gt; Physics Material 2D\uff0c\u5982\u6b64\u53ef\u4ee5\u6dfb\u52a0\u4e00\u4e2a 2D \u7684\u7269\u7406\u6750\u6599\u5728 Assets \u4e2d\u65b9\u4fbf\u540e\u7eed\u7684\u6279\u91cf\u4f7f\u7528\u3002<code>Physics Material 2D<\/code> \u4e2d\u6709\u4e24\u4e2a\u5c5e\u6027\uff1a<code>Friction<\/code> \u548c <code>Bounciness<\/code> \u5206\u522b\u4e3a\u6469\u64e6\u529b\u548c\u5f39\u529b\u3002\u5c06\u7f16\u8f91\u597d\u7684 Physics Material 2D \u62d6\u52a8\u5230\u5982 Rigidbody 2D \u4e2d\u7684 <code>Material<\/code> \u5c5e\u6027\u4e2d\u53ef\u4ee5\u7ed9\u8be5\u7269\u54c1\u6dfb\u4e0a\u6b64\u6750\u6599\u6240\u62e5\u6709\u7684\u5c5e\u6027\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Section 3\uff1aCharacter<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">First script, input and movement<\/h3>\n\n\n\n<p>\u5728\u7269\u4f53\u7684 Add Component \u7684 New script \u4e2d\u53ef\u4ee5\u7ed9\u7269\u4f53\u6dfb\u52a0\u811a\u672c\u3002\u5c5e\u6027\u4e2d\u53cc\u51fb\u6253\u5f00\u811a\u672c\uff08Unity \u7684\u504f\u597d\u8bbe\u7f6e\u4e2d\u9009\u62e9 Visual Studio\uff09\u3002<\/p>\n\n\n\n<p>\u867d\u7136\u521b\u5efa\u7684\u811a\u672c\u4e0e\u7269\u4f53\u7684\u76f8\u5173\u5c5e\u6027\u5728\u540c\u4e00\u4e2a\u4f4d\u7f6e\uff0c\u4f46\u662f\u811a\u672c\u65e0\u6743\u76f4\u63a5\u8bbf\u95ee\u4e0e\u63a7\u5236\u8fd9\u4e9b\u5c5e\u6027\u3002\u800c\u4e00\u822c\u7684\u505a\u6cd5\u662f\u521b\u5efa\u4e00\u4e2a\u53d8\u91cf\u3002<\/p>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a Rigidbody 2D \u7684\u53d8\u91cf\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public Rigidbody2D rb;<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u67e5\u770b\u8fd9\u4e2a\u811a\u672c\u6240\u5c5e\u7684\u7269\u4f53\u5c5e\u6027\uff0c\u53ef\u4ee5\u53d1\u73b0\u5728\u811a\u672c\u4e2d\u591a\u4e86\u4e00\u4e2a\u53eb <code>Rb<\/code> \u7684\u5c5e\u6027\uff08\u7b80\u5355\u7684\u8bf4\u5982\u679c\u662f <code>public<\/code> \u90a3\u4e48\u5c06\u5728\u5c5e\u6027\u4e2d\u53ef\u89c1\u5e76\u4e14\u53ef\u4ee5\u88ab\u5176\u5b83\u811a\u672c\u8bbf\u95ee\uff0c<code>private<\/code> \u90a3\u4e48\u5c06\u5728\u5c5e\u6027\u4e2d\u4e0d\u53ef\u89c1\u4e14\u4e0d\u53ef\u88ab\u8bbf\u95ee\u3002\u6240\u4ee5\u66f4\u5177\u8be5\u53d8\u91cf\u662f\u5426\u9700\u8981\u516c\u5f00\u800c\u51b3\u5b9a public \u6216\u662f private\uff0c\u901a\u5e38\u6765\u8bf4\uff0c\u4e3a\u4e86\u9632\u6b62\u6211\u4eec\u4e0d\u5c0f\u5fc3\u80e1\u4e71\u4fee\u6539\u4e00\u4e9b\u5730\u65b9\u7684\u4ee3\u7801\u800c\u6539\u52a8\u4e86\u5176\u5b83\u5730\u65b9\u7684\u53d8\u91cf\uff0c\u901a\u5e38\u5c06\u4f1a\u628a\u4e00\u822c\u7684\u53d8\u91cf\u8bbe\u7f6e\u4e3a private\u3002\u5f53\u7136\uff0c\u5982\u679c\u60f3\u8ba9 private \u7684\u53d8\u91cf\u51fa\u73b0\u5728 Unity \u7684\u7f16\u8f91\u5668\u5c5e\u6027\u4e2d\uff0c\u53ef\u4ee5\u5728 private \u524d\u9762\u52a0\u4e0a <code>[SerializeField]<\/code>\uff09\u3002<\/p>\n\n\n\n<p>\u6b64\u65f6 <code>Rb<\/code> \u7684\u5185\u5bb9\u4e3a <code>None<\/code>\uff0c\u6b64\u65f6\u5c06\u7269\u4f53\u7684 <code>Rigidbody 2D<\/code> \u5c5e\u6027\u62d6\u52a8\u5230\u8fd9\u4e2a <code>Rb<\/code> \u7684\u5c5e\u6027\u5185\u5bb9\u4e2d\uff0c\u5c06\u53ef\u4ee5\u5feb\u901f\u5b9e\u73b0 Rigidbody 2D \u76f8\u5173\u5c5e\u6027\u7684\u8bbf\u95ee\uff08\u7ed9\u53d8\u91cf <code>Rb<\/code> \u9644\u4e0a Rigidbody 2D \u7c7b\uff09\u3002\u5982\u679c\u7528\u4ee3\u7801\u5b9e\u73b0\u8fd9\u4e00\u64cd\u4f5c\u5c31\u662f\uff0c\u5728 <code>Start()<\/code> \u51fd\u6570\u4e2d\u5199\u4e0a\u4e00\u53e5 <code>rb = GetComponent&lt;Rigidbody2D&gt;();<\/code>\uff0c\u5f53 <code>Rb<\/code> \u88ab\u8bbe\u7f6e\u6210 <code>private<\/code> \u5728\u7f16\u8f91\u5668\u4e2d\u4e0d\u53ef\u89c1\u7684\u60c5\u51b5\u4e0b\u53ef\u4ee5\u4f7f\u7528\u3002<\/p>\n\n\n\n<p>\u5f53\u4f60\u521b\u5efa\u811a\u672c\u65f6\uff0c\u811a\u672c\u91cc\u5c06\u4f1a\u81ea\u52a8\u751f\u6210\u4e24\u4e2a\u51fd\u6570 <code>Start()<\/code> \u548c <code>Update()<\/code>\uff0c\u5b83\u4eec\u4e4b\u95f4\u7684\u533a\u522b\u5c31\u662f\uff1a<code>Start()<\/code> \u51fd\u6570\u53ea\u6709\u6e38\u620f\u542f\u52a8\u65f6\u4f1a\u88ab\u8c03\u7528\u4e00\u6b21\uff1b\u800c <code>Update()<\/code> \u51fd\u6570\u5c06\u4f1a\u5728\u6e38\u620f\u7684\u8fc7\u7a0b\u4e2d\u7684\u6bcf\u4e00\u5e27\u90fd\u88ab\u8c03\u7528\u4e00\u6b21\uff08\u6240\u4ee5\u5f53\u6e38\u620f\u4e3a 60 fps \u65f6\uff0cUpdate \u51fd\u6570\u4f1a\u88ab\u8c03\u7528 60 \u6b21\uff09\u3002<\/p>\n\n\n\n<p>\u8fd8\u6709\u4e00\u79cd\u56fa\u5b9a\u65f6\u95f4\u8c03\u7528\u4e00\u6b21\u7684\u51fd\u6570 <code>FixedUpdate()<\/code>\uff0c\u53ef\u4ee5\u5728 Unity \u7684 Edit \u4e2d\u7684 Project Settings \u91cc\u9762\u7684 Time \u4e2d\u7684 Fixed Timestep \u4e2d\u4fee\u6539\u8c03\u7528\u7684\u65f6\u95f4\u95f4\u9694\u3002\u3010\u4e3e\u4e2a\u4f8b\u5b50\uff1a\u5728\u6e38\u620f\u8f90\u5c0476\u4e2d\uff0c\u7535\u8111\u914d\u7f6e\u66f4\u9ad8\u7684\u73a9\u5bb6\u5176\u5237\u65b0\u7387\u5c31\u8d8a\u9ad8\uff0c\u5bfc\u81f4\u5176\u884c\u52a8\u901f\u5ea6\u6bd4\u7535\u8111\u914d\u7f6e\u4f4e\u7684\u4eba\u6162\u3002\u90a3\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u65b9\u5f0f\u5c31\u662f\u5728 FixedUpdate \u51fd\u6570\u4e2d\u8c03\u7528\u79fb\u52a8\u76f8\u5173\u7684\u51fd\u6570\u3002\u3011<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\u200b\npublic class MoveController : MonoBehaviour\n{\n \u00a0 \u00a0private Rigidbody2D rb;\n\u200b\n \u00a0  &#91;SerializeField] private float moveSpeed;\n \u00a0 \u00a0private float xInput;\n\u200b\n \u00a0 \u00a0\/\/ Start is called before the first frame update\n\n \u00a0 \u00a0void Start()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb = GetComponent&lt;Rigidbody2D>();\n \u00a0  }\n\u200b\n \u00a0 \u00a0\/\/ Update is called once per frame\n \u00a0 \u00a0void Update()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0xInput = Input.GetAxis(\"Horizontal\");\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);\n \u00a0  }\n}\n\u200b<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u4fbf\u662f\u4e00\u4e2a\u53ef\u4ee5\u5de6\u53f3\u79fb\u52a8\u7684\u811a\u672c\u3002\u7b80\u5355\u8bf4\u660e\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>Input.GetAxis(\"Horizontal\");<\/code>\uff1a\u8c03\u7528 <code>Input<\/code> \u4e2d\u7684 <code>GetAxis()<\/code> \u51fd\u6570\uff0c<code>\"Horizontal\"<\/code> \u53ef\u4ee5\u8bc6\u522b\u6c34\u5e73\u65b9\u5411\u4e0a\u7684\u8f93\u5165\uff08A \u6216 \u2190 \u4e3a\u5411\u5de6\uff0cD \u6216 \u2192 \u4e3a\u5411\u53f3\uff09\u3002<\/li>\n\n\n\n<li><code>rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);<\/code>\uff1a<code>Rigidbody2D<\/code> \u7c7b\u4e2d\u6709\u5404\u79cd\u5c5e\u6027\u53d8\u91cf\u53ef\u4ee5\u4f7f\u7528\uff0c\u6bd4\u5982\u8fd9\u91cc\u8c03\u7528\u7684 <code>velocity<\/code> \u5411\u91cf\u3002\u5b9e\u4f8b\u5316\u4e00\u4e2a <code>Vector2<\/code> \u7c7b\u53ef\u4ee5\u8ba9\u5176\u6709\u5411\u91cf\u7684\u5c5e\u6027\uff0c\u8fd9\u4e2a<code>Vector2<\/code> \u7c7b\u662f Unity \u5b98\u65b9\u5df2\u7ecf\u5199\u597d\u7684\uff0c\u8fd9\u91cc\u6682\u65f6\u6ca1\u6709\u8bf4\u660e\uff0c\u5177\u4f53\u7684\u53ef\u4ee5\u67e5\u8be2\u5b98\u65b9\u6587\u6863\u3002<\/li>\n\n\n\n<li><code>xInput<\/code>\uff1a\u63a7\u5236\u7269\u4f53\u7684\u65b9\u5411\u3002<\/li>\n\n\n\n<li><code>moveSpeed<\/code>\uff1a\u63a7\u5236\u7269\u4f53\u7684\u79fb\u52a8\u901f\u5ea6\u3002<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Jump of a character<\/h3>\n\n\n\n<p>\u63a5\u4e0b\u6765\u4e3a\u4e86\u65b9\u4fbf\u8bf4\u660e\uff0c\u4ec5\u7559\u4e0b\u4e00\u4e2a\u77e9\u5f62\u4f5c\u4e3a\u5e73\u53f0\uff0c\u91cd\u547d\u540d\u4e3a Platform\uff0c\u548c\u53e6\u5916\u4e00\u4e2a\u77e9\u5f62\u4f5c\u4e3a\u73a9\u5bb6\uff0c\u91cd\u547d\u540d\u4e3a Player\u3002<\/p>\n\n\n\n<p>\u53ef\u4ee5\u4f7f\u7528\u51fd\u6570 <code>Debug.log()<\/code> \u6765\u8fdb\u884c\u8c03\u8bd5\uff0c\u5728 Unity \u7684\u7ec8\u7aef\u4e2d\u4f1a\u663e\u793a log \u7684\u65e5\u5fd7\u5185\u5bb9\u3002<\/p>\n\n\n\n<p>\u7ec8\u7aef\u7684\u6253\u5f00\u65b9\u5f0f\uff1aWindow -&gt; General -&gt; Console\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (Input.GetKeyDown(KeyCode.Space))<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.Log(\"Space was pressed\");<br> &nbsp; &nbsp; &nbsp;  }<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u4ee3\u7801\u7b80\u8ff0\uff1a<\/p>\n\n\n\n<p>\u51fd\u6570 <code>Input.GetKeyDown()<\/code> \u53ef\u4ee5\u68c0\u6d4b\u6309\u4e0b\u952e\u76d8\u7684\u6309\u952e\uff0c<code>KeyCode.Space<\/code> \u503c\u8868\u793a\u6309\u4e0b\u7684\u662f\u7a7a\u683c\u95f4\u3002\u5982\u6b64\u8be5\u4ee3\u7801\u7684\u610f\u601d\u5c31\u662f\u5f53\u6309\u4e0b\u7a7a\u683c\u952e\u7684\u65f6\u5019\u5c31\u5728\u65e5\u5fd7\u4e2d\u8f93\u51fa\u8bed\u53e5 Space was pressed\u3002<\/p>\n\n\n\n<p><code>GetKey<\/code> \u7684\u884d\u751f\u8bf4\u660e\uff1a<\/p>\n\n\n\n<p>\u5728\u811a\u672c\u4e2d\u5199\u5165\u5982\u4e0b\u4ee3\u7801\uff0c\u7136\u540e\u5728 Unity \u4e2d\u6309\u7a7a\u683c\u8c03\u8bd5\uff0c\u770b\u53d1\u751f\u7684\u7ed3\u679c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (Input.GetKeyDown(KeyCode.Space))<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.Log(\"Space was pressed\");<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>if (Input.GetKey(KeyCode.Space))<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.Log(\"Space is pressed\");<br> &nbsp; &nbsp; &nbsp;  }<br>\u200b<br>if (Input.GetKeyUp(KeyCode.Space))<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Debug.Log(\"Space was released\");<br> &nbsp; &nbsp; &nbsp;  }<\/code><\/pre>\n\n\n\n<p>\u7ecf\u8fc7\u8c03\u8bd5\u5bf9\u6bd4\uff0c\u53d1\u73b0 <code>GetKey()<\/code> \u51fd\u6570\u662f\u53ea\u8981\u8be5\u6309\u952e\u6b63\u5728\u88ab\u6309\u65f6\u4f1a\u91cd\u590d\u8c03\u7528\u6b64\u51fd\u6570\uff0c\u4e5f\u5c31\u662f\u5982\u679c\u4e00\u76f4\u6309\u7740\u6309\u952e\uff0c\u8fd9\u4e2a\u51fd\u6570\u5c31\u4f1a\u4e00\u76f4\u88ab\u8c03\u7528\uff1b<code>GetKeyDown()<\/code> \u5f53\u8be5\u952e\u88ab\u6309\u65f6\u8c03\u7528\u4e00\u6b21\u8be5\u51fd\u6570\uff1b<code>GetKeyUp()<\/code> \u5f53\u8be5\u952e\u88ab\u677e\u5f00\u65f6\u8c03\u7528\u4e00\u6b21\u8be5\u51fd\u6570\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u53c2\u8003\u524d\u9762\u89d2\u8272\u79fb\u52a8\u7684\u5b9e\u73b0\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7167\u7740\u5199\u89d2\u8272\u8df3\u8dc3\u529f\u80fd\uff1a<\/p>\n\n\n\n<p>\u5148\u5b9a\u4e49\u4e00\u4e2a\u8df3\u8dc3\u80fd\u529b\uff08\u8df3\u8dc3\u9ad8\u5ea6\uff09\u4ee5\u65b9\u4fbf\u4e4b\u540e\u6e38\u620f\u4e2d\u53ef\u80fd\u7684\u4fee\u6539\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;SerializeField] private float jumpForce;<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u4f7f\u7528 <code>GetKeyDown()<\/code> \u51fd\u6570\uff0c\u5728\u6309\u4e0b\u7a7a\u683c\u65f6\uff0c\u4f7f\u7528 <code>Vector2<\/code> \u8fdb\u884c\u8df3\u8dc3\u7684\u5b9e\u73b0\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if (Input.GetKeyDown(KeyCode.Space))<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rb.velocity = new Vector2(rb.velocity.x, jumpForce);<br> &nbsp; &nbsp; &nbsp;  }<\/code><\/pre>\n\n\n\n<p>\u8bf4\u660e\uff1a<code>Vector2<\/code> \u4e2d\uff0c\u6211\u4eec\u4f1a\u5728\u9700\u8981\u52a8\u4f5c\u7684\u90a3\u4e2a\u5750\u6807\u4e0a\u8fdb\u884c\u7279\u6b8a\u7684\u8bf4\u660e\uff0c\u4f8b\u5982\u8fd9\u4e2a\u8df3\u8dc3\u529f\u80fd\u91cc\uff0c\u89d2\u8272\u5728\u8df3\u7684\u65f6\u5019 x \u8f74\u4e0a\u7684\u5750\u6807\u662f\u4e0d\u53d8\u7684\uff0c\u6240\u4ee5\u5199\u5165 <code>rb.velocity.x<\/code> \u6765\u8868\u793a\u7ef4\u6301\u524d\u4e00\u65f6\u523b\u7684\u72b6\u6001\uff0c\u6ce8\u610f\u8fd9\u91cc x \u8f74\u4e0a\u6700\u597d\u4e0d\u8981\u5199 <code>0<\/code>\uff08<code>Vector2(0, jumpForce)<\/code>\uff09\uff0c\u8fd9\u4e2a\u7684\u610f\u601d\u662f\u52300\u7684\u503c\u4e0a\uff0c\u800c\u4e0d\u662f\u4e0d\u6539\u53d8\u4efb\u4f55\u524d\u4e00\u72b6\u6001\u4e0a\u7684\u901f\u5ea6\u7b49\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u5728\u5b9e\u73b0\u4e86\u79fb\u52a8\u548c\u8df3\u8dc3\u529f\u80fd\u540e\uff0c\u4e3a\u4e86\u4f7f\u4ee3\u7801\u66f4\u52a0\u7b80\u6d01\uff0c\u53ef\u4ee5\u5c06\u5199\u7684\u8df3\u8dc3\u548c\u79fb\u52a8\u5c01\u88c5\u6210\u51fd\u6570\uff0c\u8fd9\u6837\u5728 <code>Start()<\/code> \u51fd\u6570\u548c <code>Update()<\/code> \u51fd\u6570\u4e2d\u7684\u5185\u5bb9\u5c31\u4f1a\u66f4\u6613\u8bfb\u3002\u5728 Visual Studio \u4e2d\uff0c\u53ef\u4ee5\u9009\u62e9\u8981\u5c01\u88c5\u7684\u8bed\u53e5\uff0c\u7136\u540e\u53f3\u952e\u9009\u62e9\u5feb\u901f\u64cd\u4f5c\u6216\u91cd\u6784&#8230; \u6216\u6309 Ctrl + . \uff0c\u7136\u540e\u9009\u62e9\u63d0\u53d6\u65b9\u6cd5\uff08\u6216\u6309 Alt + Enter\uff09\uff0c\u518d\u7ed9\u65b9\u6cd5\u547d\u540d\uff0c\u6700\u540e\u6309 Enter \u5373\u53ef\u5feb\u901f\u6784\u5efa\u65b9\u6cd5\u3002\u5982\u4e0b\u89d2\u8272\u7684\u64cd\u4f5c\u4ee3\u7801\u5c31\u4f1a\u66f4\u7b80\u6d01\u6613\u61c2\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\u200b\npublic class MoveController : MonoBehaviour\n{\n \u00a0 \u00a0private Rigidbody2D rb;\n\u200b\n \u00a0  &#91;SerializeField] private float moveSpeed;\n \u00a0  &#91;SerializeField] private float jumpForce;\n \u00a0 \u00a0private float xInput;\n\u200b\n \u00a0 \u00a0\/\/ Start is called before the first frame update\n\n \u00a0 \u00a0void Start()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb = GetComponent&lt;Rigidbody2D>();\n \u00a0 \u00a0 \u00a0 \u00a0jumpForce = 4;\n \u00a0  }\n\u200b\n \u00a0 \u00a0\/\/ Update is called once per frame\n \u00a0 \u00a0void Update()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0xInput = Input.GetAxis(\"Horizontal\");\n \u00a0 \u00a0 \u00a0 \u00a0Movement();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0if (Input.GetKeyDown(KeyCode.Space))\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Jump();\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void Movement()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void Jump()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(rb.velocity.x, jumpForce);\n \u00a0  }\n}\n\u200b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Collision detection<\/h3>\n\n\n\n<p>\u6309\u7167\u4ee5\u4e0a\u65b9\u6cd5\u7b80\u5355\u5b9e\u73b0\u4e86\u89d2\u8272\u8df3\u8dc3\u7684\u529f\u80fd\uff0c\u4f46\u662f\u8c03\u5f0f\u7684\u65f6\u5019\u4f1a\u53d1\u73b0\uff0c\u53ea\u8981\u73a9\u5bb6\u4e00\u76f4\u6309\u7a7a\u683c\u952e\uff0c\u89d2\u8272\u5c31\u4f1a\u4e0d\u65ad\u8df3\u8dc3\u3002\u56e0\u6b64\u9700\u8981\u7ed9\u89d2\u8272\u52a0\u4e0a\u8df3\u8dc3\u7684\u9650\u5236\uff0c\u8ba9\u89d2\u8272\u6bcf\u6b21\u53ea\u80fd\u8df3\u4e00\u4e0b\u3002\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\u7684\u601d\u8def\u662f\u78b0\u649e\u4f53\u68c0\u6d4b\u3002<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u7ed9\u5e73\u53f0\u7684\u5c42\u547d\u4e00\u4e2a\u540d\uff0c\u5728\u4e4b\u524d\u521b\u5efa\u7684 platform \u4e2d\u7684 Layer \u4e2d\u5c55\u5f00\u9009\u62e9 Add Layer&#8230;\uff0c\u5728 Layer 3 \u4e2d\u952e\u5165 <code>Ground<\/code> \u5bf9\u5e73\u53f0\u5c42\u547d\u540d\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u56de\u5230\u811a\u672c\u4e2d\uff0c\u5b9a\u4e49\u4e00\u4e2a\u5730\u9762\u68c0\u6d4b\u534a\u5f84\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public float groundCheckRadius;<\/code><\/pre>\n\n\n\n<p>\u63a5\u7740\u518d\u5b9a\u4e49\u4e00\u4e2a\u65b0\u7684\u65b9\u6cd5 <code>OnDrawGizmos()<\/code>\uff0c\u8fd9\u4e2a\u65b9\u6cd5\u5176\u5b9e\u5728 Unity \u4e2d\u662f\u6709\u7684\uff0c\u4f46\u662f\u9ed8\u8ba4\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u4f9b\u8bbe\u8ba1\u8005\u81ea\u884c\u5b9a\u4e49\uff0c\u8fd9\u4e2a\u65f6\u5019\u9f20\u6807\u653e\u5728 OnDrawGizmos \u4e0a\u9762\uff0c\u53ef\u4ee5\u770b\u5230\u63d0\u793a\uff0cUnity: \u5982\u679c\u4f60\u60f3\u8981\u7ed8\u5236\u53ef\u9009\u53d6\u5e76\u59cb\u7ec8\u7ed8\u5236\u7684 gizmos\uff0c\u8bf7\u5b9e\u73b0\u6b64 OnDrawGizmos\u3002\u4e8e\u662f\u5728\u65b9\u6cd5\u4e2d\u6211\u4eec\u8c03\u7528 <code>Gizmos<\/code> \u7c7b\u4e2d\u7684 <code>DrawWireSphere()<\/code> \u65b9\u6cd5\uff0c\u5982\u679c\u6b64\u65f6\u4e5f\u5c06\u9f20\u6807\u653e\u5728 DrawWireSphere \u4e0a\uff0c\u53ef\u4ee5\u770b\u5230\u6b64\u65b9\u6cd5\u7684\u89e3\u91ca\uff1a\u5927\u6982\u610f\u601d\u5c31\u662f\u7ed8\u5236\u4e00\u4e2a\u8f93\u5165\u7684\u4e2d\u5fc3\u548c\u534a\u5f84\u7684\u7ebf\u6846\u7403\u4f53\uff08\u5706\uff09\u3002OnDrawGizmos \u65b9\u6cd5\u7684\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void OnDrawGizmos()<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;Gizmos.DrawWireSphere(transform.position, groundCheckRadius);<br> &nbsp;  }<\/code><\/pre>\n\n\n\n<p>\u610f\u601d\u4e5f\u5c31\u662f\u4ee5 <code>transform.position<\/code> \u4e3a\u4e2d\u5fc3\u753b\u4e00\u4e2a\u534a\u5f84\u4e3a <code>groundCheckRadius<\/code> \u7684\u5706\u5f62\u3002\u63a5\u4e0b\u53bb\u5c06\u4f1a\u770b\u5230\u8fd9\u4e2a\u65b9\u6cd5\u7684\u4f5c\u7528\uff0c\u73b0\u5728\u5728 Unity \u4e2d\u7684 Player \u4e2d\u4fee\u6539\u6211\u4eec\u521a\u521a\u52a0\u5165\u7684 Ground Check Radius \u53d8\u91cf\uff0c\u53ef\u4ee5\u770b\u5230\u8fd9\u4e2a\u7ed8\u5236\u7684\u5706\u3002<\/p>\n\n\n\n<p>\u7ecf\u8fc7\u8c03\u8bd5\uff0c\u6211\u4eec\u53d1\u73b0 <code>OnDrawGizmos()<\/code> \u65b9\u6cd5\u5728 Player \u6574\u4e2a\u56fe\u5f62\u4e0a\u753b\u4e86\u5706\uff0c\u4f46\u662f\u6211\u4eec\u68c0\u6d4b\u8df3\u8dc3\u5e76\u4e0d\u662f\u68c0\u6d4b\u6574\u4e2a\u89d2\u8272\uff0c\u800c\u662f\u4ec5\u4ec5\u68c0\u6d4b\u89d2\u8272\u811a\u4e0b\u7684\u5185\u5bb9\uff0c\u56e0\u6b64\u53f3\u952e Player\uff0cCreate Empty \u5728 Player \u4e0b\u65b0\u5efa\u4e00\u4e2a\u5c42\uff0c\u547d\u540d\u4e3a GroundCheck\u3002<\/p>\n\n\n\n<p>\u518d\u7136\u540e\uff0c\u5728\u811a\u672c\u4e2d\u65b0\u5b9a\u4e49\u4e00\u4e2a <code>Transform<\/code> \u7c7b\u578b\u7684\u53d8\u91cf <code>groundCheck<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public Transform groundCheck;<\/code><\/pre>\n\n\n\n<p>\u6b64\u65f6\u56de\u5230 Unity\uff0c\u6211\u4eec\u5c06 Player \u79fb\u52a8\u5230\u4e0e Platform \u63a5\u89e6\uff0c\u67e5\u770b Console \u4f1a\u53d1\u73b0\u4e00\u76f4\u62a5\u9519\uff0c\u6211\u4eec\u9700\u8981\u5c06\u6211\u4eec\u521a\u521a\u65b0\u5efa\u7684 GroundCheck \u62d6\u52a8\u5230 Player \u4e2d\uff08\u811a\u672c\u4e0b\uff09\u7684 Ground Check \u4e2d\u3002\u7136\u540e\u6211\u4eec\u70b9\u51fb GroundCheck \u9009\u62e9\u79fb\u52a8\u5de5\u5177\uff0c\u5c06\u5176\u79fb\u52a8\u5230\u89d2\u8272\u811a\u4e0b\uff0c\u5e76\u4e14\u5728 Player \u4e0b\u4fee\u6539 <code>groundCheckRadius<\/code>\uff08Ground Check Radius\uff09 \u7684\u534a\u5f84\u5230\u4e00\u4e2a\u5408\u9002\u7684\u5927\u5c0f\uff0c\u5982\u4e0b\u6240\u793a\uff1a<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/tobykskgd.life\/wp-content\/uploads\/2025\/08\/groundCheck.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"564\" height=\"449\" data-original=\"https:\/\/tobykskgd.life\/wp-content\/uploads\/2025\/08\/groundCheck.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3467\" style=\"width:288px;height:auto\"  sizes=\"auto, (max-width: 564px) 100vw, 564px\" \/><\/div><\/figure>\n<\/div>\n\n\n<p>\u63a5\u4e0b\u6765\uff0c\u4e3a\u4e86\u8868\u793a\u662f\u5426\u68c0\u6d4b\u5230 Player \u78b0\u5230\u78b0\u649e\u4f53\uff0c\u518d\u65b0\u5b9a\u4e49\u4e00\u4e2a bool \u53d8\u91cf <code>groundDetected<\/code> \u6765\u8868\u793a\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public bool groundDetected;<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\uff0c\u65b0\u5b9a\u4e49\u4e00\u4e2a\u78b0\u649e\u68c0\u6d4b\u7684\u65b9\u6cd5 <code>CollisionChecks()<\/code> \uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void CollisionChecks()<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;groundDetected = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius);<br> &nbsp;  }<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc\u8c03\u7528\u4e86 <code>Physics2D<\/code> \u7c7b\u4e0b\u7684 <code>OverlapCircle()<\/code> \u65b9\u6cd5\u3002\u8fd9\u91cc\u7684\u610f\u601d\u5c31\u662f\uff0c\u5982\u679c\u4ee5 <code>groundCheck.position<\/code> \u4e3a\u4e2d\u5fc3\uff0c<code>groundCheckRadius<\/code> \u4e3a\u534a\u5f84\u7684\u5706\u4e2d\u68c0\u6d4b\u5230\u4e86\u78b0\u649e\u4f53\u5c31\u8f93\u51fa <code>1<\/code>\uff0c\u5426\u5219\u5c31\u8f93\u51fa <code>0<\/code>\u3002<\/p>\n\n\n\n<p>\u63a5\u7740\u5728 <code>Update()<\/code> \u65b9\u6cd5\u4e0b\u8c03\u7528\u4e00\u4e0b <code>CollisionChecks()<\/code> \u65b9\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>CollisionChecks();<\/code><\/pre>\n\n\n\n<p>\u56de\u5230 Unity \u4e2d\u8c03\u8bd5\uff0c\u4f1a\u53d1\u73b0 Player \u4e2d\u7684 Ground Detected \u5728\u63a5\u89e6\u5230\u5e73\u53f0\u540e\u6253\u4e0a \u221a\uff0c\u4e5f\u5c31\u662f\u8fd9\u4e2a\u65f6\u5019\u7684\u72b6\u6001\u662f 1\u3002\u4f46\u662f\u4f1a\u53d1\u73b0\u4e4b\u540e\u5982\u679c\u89d2\u8272\u518d\u79bb\u5f00\u5e73\u53f0\uff0c\u8fd9\u4e2a Ground Detected \u7684\u72b6\u6001\u4e00\u76f4\u4e0d\u4f1a\u6539\u53d8\uff0c\u8fd9\u660e\u663e\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002<\/p>\n\n\n\n<p>\u4e8e\u662f\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u5c42\uff0c\u65b0\u5b9a\u4e49\u4e00\u4e2a <code>LayerMask<\/code> \u7c7b\u578b\u7684\u53d8\u91cf <code>whatIsGround<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public LayerMask whatIsGround;<\/code><\/pre>\n\n\n\n<p>\u5728 <code>Physics2D.OverlapCircle()<\/code> \u4e2d\u52a0\u5165 <code>whatIsGround<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void CollisionChecks()<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;groundDetected = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);<br> &nbsp;  }<\/code><\/pre>\n\n\n\n<p>\u56de\u5230 Unity\uff0c\u627e\u5230 Player \u811a\u672c\u4e2d\u7684 What Is Ground \u8fd9\u4e2a\u9879\uff0c\u9009\u62e9\u4e4b\u524d\u547d\u540d\u7684 Ground \u5c42\u3002\u6b64\u65f6\u8fdb\u884c\u8c03\u8bd5\u4f1a\u53d1\u73b0\uff0cPlayer \u5728 Platform \u4e0a\u4f1a\u7ed9 Ground Detected \u4fe1\u53f7\uff0c\u5c06 Player \u4ece Platform \u4e0a\u79fb\u8d70\uff0c\u8fd9\u4e2a \u221a \u5c31\u4f1a\u6d88\u5931\u3002<\/p>\n\n\n\n<p>\u5982\u6b64\u6574\u4e2a\u78b0\u649e\u4f53\u68c0\u6d4b\u5c31\u5b9e\u73b0\u4e86\uff0c\u6211\u4eec\u53ea\u9700\u8981\u5229\u7528 <code>groundDetected<\/code> \u7ed9 <code>Jump()<\/code> \u51fd\u6570\u52a0\u4e00\u4e2a\u6761\u4ef6\u8bed\u53e5\u5373\u53ef\uff01<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void Jump()<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;if (groundDetected)<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;rb.velocity = new Vector2(rb.velocity.x, jumpForce);<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<\/code><\/pre>\n\n\n\n<p>\u4ee3\u7801\u65b9\u9762\uff0c\u6211\u4eec\u53ef\u4ee5\u505a\u7684\u66f4\u7b80\u6d01\u4e00\u4e9b\uff1a<\/p>\n\n\n\n<p>\u9996\u5148\uff0c\u6211\u4eec\u5b9a\u4e49\u4e86\u4e00\u5806\u65b0\u7684\u53d8\u91cf\uff0c\u8fd9\u4e9b\u53d8\u91cf\u90fd\u51fa\u73b0\u5728 Unity \u7f16\u8f91\u5668\u4e2d\u4f1a\u5f88\u4e71\uff0c\u6240\u4ee5\u6309\u7167\u4e4b\u524d\u4e00\u6837\uff0c\u53ef\u4ee5\u5148\u9690\u85cf\u8fd9\u4e9b\u53d8\u91cf\u3002\u53e6\u5916\u56e0\u4e3a\u8fd9\u4e9b\u53d8\u91cf\u5b9e\u73b0\u7684\u529f\u80fd\u76ee\u7684\u662f\u4e00\u81f4\u7684\uff0c\u6240\u4ee5\u53ef\u4ee5\u5c06\u8fd9\u4e9b\u53d8\u91cf\u5f52\u4e3a\u4e00\u7c7b\uff0c\u5728\u8fd9\u4e9b\u53d8\u91cf\u5b9a\u4e49\u4e4b\u524d\u52a0\u5165\u4e00\u53e5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Header(\"Collision chect\")]<\/code><\/pre>\n\n\n\n<p>\u8fd9\u53e5\u8bdd\u7684\u6548\u679c\u5982\u4e0b\uff08\u7b2c\u4e00\u884c\u7684 Collision chect\uff09\uff1a<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/tobykskgd.life\/wp-content\/uploads\/2025\/08\/bar.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"633\" height=\"152\" data-original=\"https:\/\/tobykskgd.life\/wp-content\/uploads\/2025\/08\/bar.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3468\"  sizes=\"auto, (max-width: 633px) 100vw, 633px\" \/><\/div><\/figure>\n\n\n\n<p>\u6574\u7406\u4e4b\u540e\u7684\u53d8\u91cf\u5b9a\u4e49\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&#91;Header(\"Collision chect\")]<br>&#91;SerializeField] private float groundCheckRadius;<br>&#91;SerializeField] private Transform groundCheck;<br>&#91;SerializeField] private LayerMask whatIsGround;<br>private bool groundDetected;<\/code><\/pre>\n\n\n\n<p>\u53e6\u5916\u7684\u4e00\u4e2a\u5c0f\u6280\u5de7\u662f\uff0c\u53d8\u91cf\u540d\u7684\u7edf\u4e00\u4fee\u6539\uff0c\u4e00\u4e2a\u6e38\u620f\u7684\u53d8\u91cf\u540d\u662f\u975e\u5e38\u5e9e\u5927\u7684\uff0c\u5982\u679c\u4e00\u4e2a\u4e00\u4e2a\u4fee\u6539\u975e\u5e38\u8017\u65f6\u4e14\u5bb9\u6613\u51fa\u9519\uff0c\u6b64\u65f6\u53ea\u8981\u9009\u4e2d\u8fd9\u4e2a\u53d8\u91cf\u540d\u5b57\uff0c\u7136\u540e Ctrl + R \u53ef\u4ee5\u4e00\u952e\u7edf\u4e00\u4fee\u6539\u53d8\u91cf\u540d\u3002\u6bd4\u5982\u8fd9\u91cc\u5c06 <code>groundDetected<\/code> \u6539\u6210 <code>isGrounded<\/code>\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0b\u4fbf\u662f\u76ee\u524d\u89d2\u8272\u52a8\u4f5c\u76f8\u5173\u7684\u811a\u672c\u5185\u5bb9\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using UnityEngine;\n\u200b\npublic class MoveController : MonoBehaviour\n{\n \u00a0 \u00a0private Rigidbody2D rb;\n\u200b\n \u00a0  &#91;SerializeField] private float moveSpeed;\n \u00a0  &#91;SerializeField] private float jumpForce;\n \u00a0 \u00a0private float xInput;\n\u200b\n \u00a0  &#91;Header(\"Collision chect\")]\n \u00a0  &#91;SerializeField] private float groundCheckRadius;\n \u00a0  &#91;SerializeField] private Transform groundCheck;\n \u00a0  &#91;SerializeField] private LayerMask whatIsGround;\n \u00a0 \u00a0private bool isGrounded;\n\u200b\n \u00a0 \u00a0\/\/ Start is called before the first frame update\n\n \u00a0 \u00a0void Start()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb = GetComponent&lt;Rigidbody2D>();\n \u00a0 \u00a0 \u00a0 \u00a0jumpForce = 4;\n \u00a0  }\n\u200b\n \u00a0 \u00a0\/\/ Update is called once per frame\n \u00a0 \u00a0void Update()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0CollisionChecks();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0xInput = Input.GetAxis(\"Horizontal\");\n \u00a0 \u00a0 \u00a0 \u00a0Movement();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0if (Input.GetKeyDown(KeyCode.Space))\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Jump();\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void CollisionChecks()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void Movement()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void Jump()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0if (isGrounded)\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(rb.velocity.x, jumpForce);\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void OnDrawGizmos()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);\n \u00a0  }\n}\n\u200b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Section 4: Animation<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">Move and Idle animation<\/h3>\n\n\n\n<p>\u76ee\u524d\u4e3a\u6b62\uff0c\u6211\u4eec\u7684\u89d2\u8272\u662f\u4e00\u4e2a\u77e9\u5f62\uff0c\u6211\u4eec\u9700\u8981\u5c06\u89d2\u8272\u5f62\u8c61\u5316\u3002\u81f3\u6b64\uff0c\u6211\u4eec\u9700\u8981\u5bfc\u5165\u672c\u5957\u8bfe\u7a0b\u7ed9\u7684\u8d44\u6e90\uff0c\u90a3\u8d44\u6e90\u5df2\u7ecf\u653e\u5230\u4e86 git \u4ed3\u5e93\u91cc\u7684 <code>.\/resource<\/code> \u76ee\u5f55\u5f53\u4e2d\u53ef\u4f9b\u4e0b\u8f7d\u3002\u91cc\u9762\u662f\u4e00\u4e2a <code>Unity package file<\/code> \u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u5c06\u5176\u62d6\u5165\u5f53\u524d\u7684 Unity \u5de5\u4f5c\u533a\uff0c\u6216\u76f4\u63a5\u53cc\u51fb\u6253\u5f00\u3002\u6b64\u65f6\u4f1a\u5f39\u51fa\u4e00\u4e2a\u7a97\u53e3\uff0c\u5176\u4e2d\u5305\u542b\u672c\u8bfe\u7a0b\u6240\u9700\u7684\u6240\u6709\u5185\u5bb9\u3002\u5982\u4e0b\u56fe\u6240\u793a\uff0c\u4f1a\u5f39\u51fa\u6765\u8fd9\u6837\u7684\u7a97\u53e3\uff0c\u9009\u62e9\u6240\u6709\uff0c\u7136\u540e\u7684\u5355\u51fb <code>Import<\/code> \u5bfc\u5165\u3002<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full is-resized\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/tobykskgd.life\/wp-content\/uploads\/2025\/08\/Improt-Unity-Package.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"527\" height=\"865\" data-original=\"https:\/\/tobykskgd.life\/wp-content\/uploads\/2025\/08\/Improt-Unity-Package.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3469\" style=\"width:246px;height:auto\"  sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/div><\/figure>\n<\/div>\n\n\n<p>Unity \u7684\u8d44\u6e90\u7ba1\u7406\u5668 Project \u4e2d\u8fdb\u5165 <code>Graphics &gt; Onion Lad<\/code>\uff0c\u91cc\u9762\u6709\u5df2\u7ecf\u88c1\u526a\u597d\u7684\u4eba\u7269 <code>Hurt<\/code>\uff0c\u4e0d\u8fc7\u4e00\u822c\u5de5\u4f5c\u4e2d\u6211\u4eec\u5f97\u5230\u7684\u90fd\u662f\u50cf <code>Idle<\/code> \u8fd9\u6837\u672a\u88c1\u526a\u8fc7\u7684\u8d44\u6e90\uff0c\u63a5\u4e0b\u6765\u5148\u8bf4\u660e\u5982\u4f55\u88c1\u526a\u4eba\u7269\u3002<\/p>\n\n\n\n<p>\u4e3a\u4e86\u66f4\u597d\u7684\u6f14\u793a\uff0c\u9996\u5148\u91cd\u7f6e\u4e4b\u524d\u7684\u8bbe\u7f6e\uff0c\u70b9\u51fb\u8d44\u6e90\u5e93\u4e2d\u7684 <code>Idle<\/code>\uff0c\u5728\u65c1\u8fb9\u7684 <code>Inspector<\/code> \u4e2d\uff0c\u770b\u5230\u4e24\u4e2a\u4eba\u7269\u7684\u56fe\u7247\uff0c\u65c1\u8fb9\u6709\u4e2a\u4e09\u70b9\u7684\u56fe\u6807\uff08\u662f\u7b2c\u4e8c\u4e2a\uff09\uff0c\u70b9\u51fb <code>Reset<\/code>\u3002\u73b0\u5728\u5f00\u59cb\uff0c<code>Inspector<\/code> \u4e2d\u5148\u5c06 <code>Sprite Mode<\/code> \u4e2d\u7684 <code>Single<\/code> \u6539\u6210 <code>Multiple<\/code>\u3002\u7136\u540e\u70b9\u51fb <code>Sprite Editor<\/code> \u8fdb\u5165\u7f16\u8f91\u5668\uff08\u82e5\u5f39\u7a97\uff0c\u9009\u62e9 Apply\uff09\uff0c\u8fd9\u91cc\u6211\u4eec\u53ef\u4ee5\u6309\u5355\u5143\u683c\u5927\u5c0f\u6765\u5207\u7247\u7c7b\u578b\u7f51\u7edc\uff0c\u70b9\u51fb\u5de6\u4e0a\u89d2\u7684 <code>Slice<\/code>\uff0c<code>Type<\/code> \u6539\u4e3a <code>Grid By Cell Size<\/code>\uff0c\u5c06 <code>Pixel Size<\/code> \u7684 X,Y \u6539\u4e3a <code>16, 16<\/code>\uff08\u8fd9\u91cc\u6709\u4e24\u4e2a\u89d2\u8272\uff0c\u4e5f\u5c31\u662f\u4e24\u4e2a\u7ea2\u6846\u8981\u5c3d\u91cf\u6b63\u597d\u5730\u5305\u62ec\u4f4f\u4e24\u4e2a\u89d2\u8272\uff09\uff0c\u6700\u540e\u70b9\u51fb <code>Slice<\/code> \u786e\u8ba4\uff0c\u6700\u540e\u70b9\u51fb\u53f3\u4e0a\u89d2\u7684 <code>Apply<\/code> \u5e94\u7528\u3002<\/p>\n\n\n\n<p>\u70b9\u51fb <code>Idle<\/code> \u53f3\u8fb9\u7684\u767d\u8272\u7bad\u5934\u4f1a\u53d1\u73b0\u5176\u5305\u542b\u4e86\u4e24\u4e2a\u89d2\u8272\u56fe\u7247\u8d44\u6e90\uff0c\u9009\u62e9\u5176\u4e2d\u4e00\u4e2a\u62d6\u5165\u5de6\u4e0a\u89d2\u7684\u573a\u666f <code>Scene<\/code> \u4e2d\uff0c\u6b64\u65f6\u4f1a\u53d1\u73b0\u89d2\u8272\u7279\u522b\u5c0f\uff0c\u6211\u4eec\u5230 <code>Project<\/code> \u4e2d\u70b9\u51fb <code>Idle<\/code>\uff0c\u5c06 <code>Inspector<\/code> \u4e2d\u7684 <code>Pixels Per Unit<\/code> \u6539\u4e3a <code>16<\/code>\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u8fd9\u4e2a\u89d2\u8272\uff08Onion Lad \u4e4b\u540e\u5c31\u79f0\u4e3a\u201c\u6d0b\u8471\u5c0f\u4f19\u201d\u597d\u4e86\uff09\u5c31\u53d8\u5927\u4e86\uff0c\u4f46\u53e6\u4e00\u4e2a\u95ee\u9898\u662f\u4ed6\u73b0\u5728\u6709\u70b9\u6a21\u7cca\uff0c\u4e3a\u4e86\u89e3\u51b3\u8fd9\u4e2a\u95ee\u9898\uff0c\u6211\u4eec\u8981\u8fdb\u884c\u538b\u7f29\uff0c\u5728\u53f3\u8fb9\u7684 <code>Inspector<\/code> \u4e2d\u7684 <code>Default<\/code> \u4e2d\u7684 <code>Compression<\/code> \u91cc\u9009\u62e9 <code>None<\/code>\uff0c\u7136\u540e\u4e0a\u9762\u4e00\u70b9\u7684 <code>Filiter Mode<\/code> \u9009\u62e9 <code>Point(no filiter)<\/code>\uff0c\u70b9\u51fb\u53f3\u4e0b\u89d2\u7684 <code>Apply<\/code>\uff0c\u73b0\u5728\u770b\u8d77\u6765\u60c5\u51b5\u5c31\u597d\u591a\u4e86\u3002<\/p>\n\n\n\n<p>\u8bbe\u7f6e\u5b8c\u4e86\u89d2\u8272\uff0c\u6211\u4eec\u5148\u5728\u573a\u666f\u4e2d\u5220\u9664\u4ed6\uff0c\u7136\u540e\u5728 <code>Hierarchy<\/code> \u91cc\u70b9\u51fb <code>Player<\/code>\uff0c\u6211\u4eec\u60f3\u5c06\u4e4b\u524d\u8bfe\u7a0b\u4e2d\u521b\u5efa\u7684\u8fd9\u4e2a\u89d2\u8272\u5f62\u8c61\u6539\u4e3a\u8fd9\u4e2a\u6d0b\u8471\u5c0f\u4f19\uff0c\u4e8e\u662f\u6211\u4eec\u5728 <code>Player<\/code> \u7684 <code>Inspector<\/code> \u4e2d\u70b9\u51fb <code>Sprite Renderer<\/code> \u5c55\u5f00\uff0c\u5c06 <code>Project<\/code> \u4e2d\u7684\u6d0b\u8471\u5c0f\u4f19\u56fe\u7247\u62d6\u52a8\u5230 <code>Sprite<\/code> \u4e2d\u3002\u6b64\u65f6\u5df2\u7ecf\u53ef\u4ee5\u770b\u89c1\u6d0b\u8471\u5c0f\u4f19\u4e86\uff0c\u4f46\u56e0\u4e3a\u8fd9\u4e2a\u7ec4\u4ef6\u5e94\u7528\u7684\u662f\u4e4b\u524d\u7684\u8bbe\u7f6e\uff0c\u8fd9\u91cc\u7684\u62c9\u4f38\u3001\u989c\u8272\u7b49\u6709\u4e9b\u9519\u8bef\uff0c\u6211\u4eec\u7a0d\u4f5c\u8c03\u6574\u5373\u53ef\u3002\u5728 <code>Inspector<\/code> \u7684 <code>Transform<\/code> \u4e2d\u7684\u53f3\u8fb9\u6709\u4e09\u4e2a\u70b9\u7684\u6309\u94ae\uff0c\u70b9\u51fb\u7136\u540e\u9009\u62e9 <code>Reset<\/code> \u91cd\u7f6e\u5927\u5c0f\uff0c\u7136\u540e\u989c\u8272\u6539\u6210\u767d\u8272\u5373\u53ef\uff08<code>Inspecto<\/code> -&gt; <code>Sprite Renderer<\/code> -&gt; <code>Color<\/code>\uff09\u3002\u73b0\u5728\u8fd9\u6837\u5c31\u597d\u591a\u4e86\uff01<\/p>\n\n\n\n<p>\u63a5\u7740\u6211\u4eec\u8bbe\u7f6e\u78b0\u649e\u4f53\uff0c<code>Inspector<\/code> \u4e2d\u7684 <code>Box Collider 2D<\/code> \u7684\u53f3\u8fb9\u4e09\u4e2a\u70b9\u70b9\u51fb\uff0c\u7136\u540e\u9009\u62e9 <code>Remove Component<\/code>\uff0c\u5220\u9664\u4e4b\u524d\u7684\u78b0\u649e\u4f53\u3002\u7136\u540e\u70b9\u51fb\u6700\u4e0b\u9762\u7684 <code>Add Component<\/code> \u641c\u7d22\u6dfb\u52a0 <code>Capsule Collider 2D<\/code>\uff0c\u5c06 Edit Collider \u65c1\u8fb9\u7684\u56fe\u6807\u70b9\u4eae\uff0c\u5728\u573a\u666f\u4e2d\u9f20\u6807\u624b\u52a8\u6539\u5927\u5c0f\uff0c\u5c06\u5927\u5c0f\u6b63\u597d\u5408\u9002\u7684\u8986\u76d6\u6d0b\u8471\u5c0f\u4f19\u3002\u6b64\u65f6\u8c03\u8bd5\u6211\u4eec\u7684\u6e38\u620f\uff0c\u73b0\u5728\u6211\u4eec\u6709\u4e86\u89d2\u8272\u53ef\u4ee5\u56db\u5904\u8d70\u52a8\u4e86\uff01<\/p>\n\n\n\n<p>\u4f46\u662f\u4f1a\u53d1\u73b0\uff0c\u6b64\u65f6\u6211\u4eec\u7684\u89d2\u8272\u6ca1\u6709\u52a8\u753b\uff0c\u4e0b\u9762\u5c06\u7b80\u5355\u63cf\u8ff0 Animation \u8fd9\u90e8\u5206\u3002\u56e0\u4e3a\u7bc7\u5e45\u9650\u5236\uff0c\u4e0d\u4f1a\u5728\u6b64\u8bf4\u660e\u89d2\u8272\u7684\u8fd0\u52a8\u52a8\u753b\u662f\u600e\u4e48\u505a\u51fa\u6765\u7684\uff0c\u52a8\u753b\u5df2\u7ecf\u5236\u4f5c\u5b8c\u653e\u5230\u4e86\u672c\u8bfe\u7a0b\u7684\u6253\u5305\u8d44\u6e90\u91cc\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\u6211\u4eec\u73b0\u5728\u8981\u505a\u7684\u5c31\u662f\u5728 <code>Project<\/code> \u8d44\u6e90\u5e93\u4e2d\u524d\u5f80 <code>Assets<\/code> \u521b\u5efa\u540d\u4e3a <code>Animation<\/code> \u7684\u6587\u4ef6\u5939\uff0c\u7136\u540e\u5728 <code>Animation<\/code> \u4e2d\u521b\u5efa\u52a8\u753b\u63a7\u5236\u5668\uff08\u53f3\u952e -&gt; Create -&gt; Animator Controller\uff09\uff0c\u5c06\u5176\u547d\u540d\u4e3a <code>Player_AC<\/code>\u3002\u7136\u540e\u6211\u4eec\u70b9\u51fb <code>Hierarchy<\/code> \u7684 <code>Player<\/code>\uff0c\u5c06\u521b\u5efa\u7684 <code>Player_AC<\/code> \u62d6\u5230 <code>Inspector<\/code> \u7684\u6700\u4e0b\u9762\uff0c\u4e3a\u5176\u521b\u5efa\u4e00\u4e2a\u65b0\u7ec4\u4ef6\u3002\u8fd9\u4e2a\u52a8\u753b\u63a7\u5236\u5668\u5141\u8bb8\u6211\u4eec\u5728\u8fd9\u4e2a\u5bf9\u8c61\u4e0a\u521b\u5efa\u52a8\u753b\uff0c\u67e5\u770b\u52a8\u753b\u63a7\u5236\u5668\u7684\u65b9\u6cd5\u4e3a\uff1a\u5de6\u4e0a\u89d2Unity \u7684\u83dc\u5355\u680f\u4e2d\u7684 Window -&gt; Animation -&gt; Animator\u3002\u7136\u540e\u6211\u8fd8\u9700\u8981\u4e00\u4e2a\u67e5\u770b\u52a8\u753b\u7684 Animation \u7a97\u53e3\uff0c\u6253\u5f00\u65b9\u6cd5\u7c7b\u4f3c\uff1a\u5de6\u4e0a\u89d2Unity \u7684\u83dc\u5355\u680f\u4e2d\u7684 Window -&gt; Animation -&gt; Animation\u3002\u63a5\u4e0b\u91cc\u70b9\u51fb <code>Animation<\/code> \u4e2d\u7684 Create \u6765\u521b\u5efa\u52a8\u753b\uff0c\u6b64\u65f6\u4f1a\u5f39\u51fa\u521b\u5efa\u6587\u4ef6\u7684\u7a97\u53e3\uff0c\u9009\u62e9\u521a\u521a\u521b\u5efa\u7684 <code>Animation<\/code> \u6587\u4ef6\uff0c\u5c06\u6587\u4ef6\u547d\u540d\u4e3a\uff1a<code>playerIdle<\/code>\u3002\u7136\u540e\u5230 Project \u4e2d\u7684 Assets &gt; Graphics &gt; Onion Lad \u4e0b\u627e\u5230 Idle\uff0c\u70b9\u5f00\u767d\u8272\u5706\u5f62\u7bad\u5934\uff0c\u5c06\u4e24\u4e2a\u56fe\u7247\u62d6\u52a8\u5230 Animation \u7a97\u53e3\u4e2d\u3002\u5728 <code>Animation<\/code> \u4e2d\u64ad\u653e\u52a8\u753b\uff0c\u56de\u5230 <code>Scene<\/code> \u573a\u666f\u4e2d\uff0c\u770b\u5230\u6d0b\u8471\u5c0f\u4f19\u7728\u773c\u592a\u5feb\u4e86\uff0c\u8fd9\u91cc\u9700\u8981\u66f4\u6539 <code>Samples<\/code> \u7684\u503c\uff0c\u8fd9\u91cc\u53ef\u4ee5\u5148\u7b80\u5355\u7406\u89e3\u4e3a\u64ad\u653e\u901f\u5ea6\u3002\u5982\u679c\u6ca1\u6709\u770b\u5230 <code>Samples<\/code> \u8fd9\u4e2a\u503c\u7684\u9009\u9879\uff0c\u9700\u8981\u70b9\u51fb\u84dd\u8272\u65f6\u95f4\u8f74\u6700\u53f3\u8fb9\u7684\u4e09\u4e2a\u70b9\uff0c\u52fe\u9009\u4e0a <code>Show Sample Rate<\/code> \u5373\u53ef\u3002\u8fd9\u91cc\u5c06 <code>Samples<\/code> \u7684\u503c\u8bbe\u7f6e\u4e3a <code>4<\/code>\u3002\u67e5\u770b <code>Animator<\/code>\uff0c\u4f1a\u770b\u5230 <code>playerIdle<\/code> \u9ed8\u8ba4\u88ab\u8bbe\u7f6e\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u901a\u8fc7\u8c03\u8bd5\u4f1a\u53d1\u73b0\uff0c\u4e0d\u7ba1\u6211\u4eec\u505a\u4ec0\u4e48\u52a8\u4f5c\uff0c\u89d2\u8272\u4e00\u76f4\u5904\u4e8e\u7a7a\u95f2\u72b6\u6001\uff0c\u4e8e\u662f\u6211\u4eec\u8981\u4e3a\u5176\u6dfb\u52a0\u5176\u5b83\u52a8\u753b\u3002\u505a\u6cd5\u5982\u4e0b\uff1a\u70b9\u51fb <code>Hierarchy<\/code> \u91cc\u7684 <code>Player<\/code>\uff0c\u5de6\u4e0b\u89d2\u70b9\u51fb <code>Animation<\/code>\uff0c\u518d\u70b9\u51fb <code>playerIdle<\/code> \u9009\u62e9 <code>Create New Clip...<\/code>\uff0c\u5728 Assets &gt; Animation \u4e2d\u65b0\u5efa\u4e00\u4e2a\u52a8\u753b\u6587\u4ef6 <code>playerMove<\/code>\u3002\u5c06\u7d20\u6750\u91cc\u7684 <code>Run &amp; Jump<\/code> \u7684\u4e24\u4e2a\u7d20\u6750\u62d6\u5165\u5de6\u4e0b\u89d2\u7684\u52a8\u753b\u680f\u4e2d\uff0c\u7136\u540e\u5c06\u91c7\u6837\u7387 <code>Samples<\/code> \u6539\u6210 <code>6<\/code>\u3002\u76ee\u524d\u8fd9\u53ea\u662f\u9ed8\u8ba4\u8bbe\u7f6e\uff0c\u5e76\u4e14\u6211\u4eec\u6ca1\u6709\u4efb\u4f55\u7c7b\u578b\u7684\u73a9\u5bb6\u79fb\u52a8\u8fc7\u6e21\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5728\u5de6\u4e0a\u89d2\u7684 <code>Animation<\/code> \u4e2d\u53f3\u952e\u70b9\u51fb <code>playerIdle<\/code> \u9009\u62e9 <code>Make Transition<\/code> \u5230 <code>playerMove<\/code> \u4e2d\uff0c\u4f7f\u8f6c\u6362\u5230\u73a9\u5bb6\u79fb\u52a8\u3002\u4f46\u8fd9\u4e0d\u4f1a\u8d77\u4f5c\u7528\uff0c\u56e0\u4e3a\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u6761\u4ef6\u6765\u77e5\u9053\u4f55\u65f6\u8f6c\u6362\u73a9\u5bb6\u79fb\u52a8\u3002\u4e8e\u662f\u6211\u4eec\u9700\u8981\u521b\u5efa\u4e00\u4e2a\u53c2\u6570\uff1a\u70b9\u51fb\u5de6\u4e0a\u89d2\u7684 <code>Parameters<\/code> \u70b9\u51fb\u52a0\u53f7\u6dfb\u52a0\u4e00\u4e2a\u53c2\u6570\uff0c\u7c7b\u578b\u9009\u62e9 <code>Bool<\/code>\uff0c\u53d6\u540d\u4e3a <code>isMoving<\/code>\u3002\u70b9\u51fb <code>Animation<\/code> \u4e2d\u8fde\u63a5 <code>playerIdle<\/code> \u548c <code>playerMove<\/code> \u72b6\u6001\u7684\u7bad\u5934\uff0c\u5728\u53f3\u8fb9\u7684 <code>Inspector<\/code> \u4e2d\u7684 <code>Condiditions<\/code> \u70b9\u51fb\u52a0\u53f7\uff0c<code>isMoving<\/code> \u9009\u62e9 <code>true<\/code> \u8868\u793a\u5f53 <code>isMoving<\/code> \u8fd9\u4e2a\u5e03\u6717\u503c\u4e3a <code>true<\/code> \u65f6\uff0c\u8fd9\u4e2a\u7bad\u5934\u6210\u7acb\uff0c\u4e5f\u5c31\u662f\u8ba9\u89d2\u8272\u7684\u72b6\u6001\u4ece <code>playerIdle<\/code> \u5230 <code>playerMove<\/code>\u3002\u53cd\u4e4b\u4ece <code>playerMove<\/code> \u72b6\u6001\u5230 <code>playerIdle<\/code> \u72b6\u6001\u4e5f\u662f\u4e00\u6837\uff0c\u53f3\u952e <code>playerMove<\/code> \u9009\u62e9 <code>Make Transition<\/code> \u8fde\u63a5 <code>playerIdle<\/code>\uff0c\u70b9\u51fb\u8fd9\u4e2a\u7bad\u5934\uff0c\u5c06\u53f3\u4fa7\u7684 <code>Conditions<\/code> \u4e0b\u7684 <code>isMoving<\/code> \u6539\u6210 <code>false<\/code>\u3002\u73b0\u5728\u8ba9\u6211\u4eec\u5173\u6ce8\u8fd9\u4e2a <code>isMoving<\/code> \u6761\u4ef6\u53c2\u6570\u5982\u4f55\u6539\u53d8\uff0c\u8ba9\u6211\u4eec\u56de\u5230 <code>MoveController.cs<\/code> \u8fd9\u4e2a\u811a\u672c\uff1a<\/p>\n\n\n\n<p>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u53d8\u91cf <code>anim<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private Animator anim;<\/code><\/pre>\n\n\n\n<p>\u4e0e\u4e4b\u524d\u7684 <code>rb<\/code> \u4e00\u6837\uff0c\u6211\u4eec\u53ef\u4ee5\u5728 <code>Start()<\/code> \u65b9\u6cd5\u4e0b\u83b7\u53d6\u52a8\u753b\u7ec4\u4ef6\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anim = GetComponent&lt;Animator&gt;();<\/code><\/pre>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u6709\u4e86\u8fd9\u4e2a\u52a8\u753b\u5668\u7684\u7ec4\u4ef6\uff0c\u6211\u4eec\u53ef\u4ee5\u7528\u5b83\u6765\u6539\u53d8\u5b83\u7684\u53c2\u6570\u3002\u9996\u5148\u521b\u5efa\u4e00\u4e2a\u5e03\u5c14\u503c <code>isMoving<\/code>\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>public bool isMoving;<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u5728 <code>Update()<\/code> \u8fd9\u4e2a\u65b9\u6cd5\u4e0b\uff08\u6ce8\u610f\uff1a\u7b2c\u4e00\u4e2a\u53c2\u6570\u5f15\u53f7\u91cc\u7684\u8fd9\u4e2a\u503c\u5fc5\u987b\u548c\u524d\u9762 <code>Animation<\/code> \u4e0b\u521b\u5efa\u7684\u5e03\u5c14\u503c\u540d\u5b57\u4e00\u6a21\u4e00\u6837\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anim.SetBool(\"isMoving\", isMoving);<\/code><\/pre>\n\n\n\n<p>\u56de\u5230 Unity\uff0c\u8fd0\u884c\u6d4b\u8bd5\uff0c\u901a\u8fc7\u52fe\u9009 <code>Inspector<\/code> \u4e2d <code>Move Controller(Script)<\/code> \u4e0b\u7684 <code>isMoving<\/code> \u53c2\u6570\uff0c\u4f1a\u53d1\u73b0\u89d2\u8272\u5728\u4e24\u4e2a\u72b6\u6001\u4e0b\u7684\u53d8\u5316\u3002\u4e0d\u8fc7\u4ed4\u7ec6\u770b\u4f1a\u53d1\u73b0\uff0c\u6211\u4eec\u52fe\u9009 <code>isMoving<\/code> \u53c2\u6570\u4f7f\u89d2\u8272\u72b6\u6001\u53d8\u5316\u662f\u6709\u5ef6\u8fdf\u7684\uff0c\u8fd9\u662f\u7531\u4e8e\u4e24\u4e2a\u7bad\u5934\u7684\u8f6c\u53d8\u800c\u53d1\u751f\u7684\uff0c\u6211\u4eec\u53ef\u4ee5\u5355\u51fb\u7bad\u5934\uff0c\u8f6c\u5230\u8bbe\u7f6e\u5e76\u68c0\u67e5\u9000\u51fa\u65f6\u95f4\uff0c\u56e0\u4e3a\u6211\u4eec\u6709 <code>0.25<\/code> \u79d2\u7684\u9000\u51fa\u65f6\u95f4\u548c\u8fc7\u6e21\uff0c\u6211\u4eec\u5c06 <code>Exit Time<\/code> \u53d6\u6d88\u52fe\u9009\uff0c\u70b9\u5f00 <code>Settings<\/code>\uff0c\u518d\u5c06 <code>Transition Duration(s)<\/code> \u6539\u6210 <code>0<\/code> \u5373\u53ef\u3002<\/p>\n\n\n\n<p>\u73b0\u5728\u6211\u4eec\u9700\u8981\u63a7\u5236 isMoving \u8fd9\u4e2a\u5e03\u5c14\u503c\uff0c\u4e0d\u662f\u7528\u9f20\u6807\u5728\u68c0\u67e5\u5668\u4e2d\uff0c\u800c\u662f\u6839\u636e\u89d2\u8272\u7684\u79fb\u52a8\u3002\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u89d2\u8272\u7684\u79fb\u52a8\u901f\u5ea6\u6765\u8fdb\u884c\u72b6\u6001\u7684\u63a7\u5236\u3002\u5728 Update() \u65b9\u6cd5\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>isMoving = rb.velocity.x != 0;<\/code><\/pre>\n\n\n\n<p>\u4e5f\u5c31\u662f\u5f53\u89d2\u8272\u7684\u79fb\u52a8\u7b49\u4e8e rb \u7684\u901f\u5ea6\uff0c\u800c\u4e0d\u662f\u7b49\u4e8e 0\uff0c\u90a3\u4e48\u5b83\u5c31\u662f\u5728\u79fb\u52a8\u3002<\/p>\n\n\n\n<p>\u56de\u5230 Unity \u6d4b\u8bd5\uff0c\u5c31\u4f1a\u53d1\u73b0\u89d2\u8272\u7684\u72b6\u6001\u8f6c\u6362\u6b63\u5e38\u4e86\uff0c\u73b0\u5728\u8fd8\u6709\u4e00\u4e2a\u5c0f\u95ee\u9898\uff1a\u6211\u4eec\u9700\u8981\u5728\u89d2\u8272\u5411\u5de6\u79fb\u52a8\u7684\u65f6\u5019\u7ffb\u8f6c\u89d2\u8272\u3002\u8fd9\u4e2a\u5c31\u5728\u540e\u9762\u7684\u7ae0\u8282\u4e0b\u5b8c\u6210\u3002\u672c\u5c0f\u8282\u7684\u6700\u540e\uff0c\u6211\u4eec\u6765\u6574\u7406\u4e00\u4e0b\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>isMoving = rb.velocity.x != 0;<br>anim.SetBool(\"isMoving\", isMoving);<\/code><\/pre>\n\n\n\n<p>\u9996\u5148\u63d0\u53d6\u4e00\u4e0b\u68c0\u6d4b\u72b6\u6001\u7684\u4ee3\u7801\uff0c\u5bf9\u7740\u4e0a\u8ff0\u4e24\u884c\u4ee3\u7801\u6309\u4e0b Alt + Enter \u63d0\u53d6\u65b9\u6cd5 <code>Extract method<\/code>\uff0c\u5c06\u5176\u547d\u540d\u4e3a <code>AnimationControllers()<\/code>\u3002<\/p>\n\n\n\n<p>\u7136\u540e\u5c31\u662f\u5148\u524d\u521b\u5efa\u7684 <code>isMoving<\/code> \u53d8\u91cf\uff0c\u5b9e\u9645\u4e0a\u6211\u4eec\u53ea\u5728\u4e00\u4e2a\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u5b83\uff0c\u6240\u4ee5\u6211\u4eec\u5220\u9664\u524d\u9762 <code>isMoving<\/code> \u7684 public \u53d8\u91cf\uff0c\u7136 \u540e\u5728<code>AnimationControllers()<\/code> \u65b9\u6cd5\u4e0b\u521b\u5efa\u4e00\u4e2a\u4e34\u65f6\u53d8\u91cf\uff0c\u8be5\u53d8\u91cf\u4ec5\u5728\u51fd\u6570\u5185\u90e8\u4f7f\u7528\u3002\u5c06 <code>AnimationControllers()<\/code> \u6539\u4e3a\u5982\u4e0b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void AnimationControllers()<br>{<br> &nbsp; &nbsp;bool isMoving = rb.velocity.x != 0;<br> &nbsp; &nbsp;anim.SetBool(\"isMoving\", isMoving);<br>}<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u4e3a\u5f53\u524d\u7684\u63a7\u5236\u811a\u672c\u4e0b\u7684\u6240\u6709\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using UnityEngine;\n\u200b\npublic class MoveController : MonoBehaviour\n{\n \u00a0 \u00a0private Rigidbody2D rb;\n \u00a0 \u00a0private Animator anim;\n\u200b\n \u00a0  &#91;SerializeField] private float moveSpeed;\n \u00a0  &#91;SerializeField] private float jumpForce;\n \u00a0 \u00a0private float xInput;\n\u200b\n \u00a0  &#91;Header(\"Collision chect\")]\n \u00a0  &#91;SerializeField] private float groundCheckRadius;\n \u00a0  &#91;SerializeField] private Transform groundCheck;\n \u00a0  &#91;SerializeField] private LayerMask whatIsGround;\n \u00a0 \u00a0private bool isGrounded;\n\u200b\n \u00a0 \u00a0\/\/ Start is called before the first frame update\n\n \u00a0 \u00a0void Start()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb = GetComponent&lt;Rigidbody2D>();\n \u00a0 \u00a0 \u00a0 \u00a0anim = GetComponent&lt;Animator>();\n \u00a0 \u00a0 \u00a0 \u00a0jumpForce = 4;\n \u00a0  }\n\u200b\n \u00a0 \u00a0\/\/ Update is called once per frame\n \u00a0 \u00a0void Update()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0AnimationControllers();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0CollisionChecks();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0xInput = Input.GetAxis(\"Horizontal\");\n \u00a0 \u00a0 \u00a0 \u00a0Movement();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0if (Input.GetKeyDown(KeyCode.Space))\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Jump();\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void AnimationControllers()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0bool isMoving = rb.velocity.x != 0;\n \u00a0 \u00a0 \u00a0 \u00a0anim.SetBool(\"isMoving\", isMoving);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void CollisionChecks()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void Movement()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void Jump()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0if (isGrounded)\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(rb.velocity.x, jumpForce);\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void OnDrawGizmos()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);\n \u00a0  }\n}\n\u200b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Animations and blend tree<\/h3>\n\n\n\n<p>\u672c\u5c0f\u8282\u4e3b\u8981\u8bb2\u8ff0\u6df7\u5408\u6811\uff08blend tree\uff09\u76f8\u5173\u7684\u6982\u5ff5\u3002<\/p>\n\n\n\n<p>\u9996\u5148\u6211\u4eec\u5236\u4f5c\u4e00\u68f5\u7528\u4e8e\u8df3\u8dc3\u548c\u843d\u4e0b\u7684\u6df7\u5408\u6811\u3002\u8fd9\u91cc\u7684\u8df3\u8dc3\u52a8\u4f5c\u7d20\u6750\u4f7f\u7528\u7684\u662f Standing<em>&amp;<\/em>Jump_Punching (16 x 16) \u8fd9\u4e2a\u7d20\u6750\uff0c\u53c2\u8003\u524d\u9762\u7684\u65b9\u6cd5\u5728 \u53f3\u8fb9\u7684 Sprite Editor \u4e0b Slice \u7528 Grid by Cell Size \u5c06 Pixel Size \u5207\u6210 16 \u00d7 16\u3002\u548c\u524d\u9762\u4e00\u6837\uff0c\u5de6\u4e0b\u89d2\u6211\u4eec\u5728 Assets &gt; Animation \u4e0b\u521b\u5efa\u4e24\u4e2a\u65b0\u7684\u52a8\u753b\uff0c\u5206\u522b\u4e3a <code>playerJump<\/code> \u548c <code>playerFall<\/code>\uff0c\u7136\u540e\u5206\u522b\u5c06\u4e24\u4e2a\u52a8\u753b\u7d20\u6750\u62d6\u5165\u5de6\u4e0b\u89d2\u7684\u65f6\u95f4\u8f74\u3002<\/p>\n\n\n\n<p>\u70b9\u51fb\u5de6\u4e0a\u89d2\u7684 Animator \u9009\u4e2d\u4e24\u4e2a\u65b0\u5efa\u7684 <code>playerJump<\/code> \u548c <code>playerFall<\/code>\uff0c\u53f3\u952e Delete \u5220\u9664\uff0c\u7136\u540e\u53f3\u952e\u7a7a\u767d\u9009\u62e9 Create State &gt; From New Blend Tree\uff0c\u8fd9\u6837\u6211\u4eec\u521b\u5efa\u4e86\u4e00\u4e2a\u6df7\u5408\u6811\uff0c\u6211\u4eec\u5c06\u5176\u547d\u540d\u4e3a <code>Jump\/Fall<\/code>\u3002\u6b64\u65f6\u770b\u53f3\u4e0a\u89d2\u7684 Parameters \u53c2\u6570\u8fd9\u91cc\u591a\u4e86\u4e00\u4e2a <code>Blend<\/code>\uff0c\u6211\u4eec\u5c06\u5176\u91cd\u547d\u540d\u4e3a <code>yVelocity<\/code>\u3002\u53cc\u51fb Jump\/Fall \u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u4e2a\u6df7\u5408\u6811\u4e2d\u770b\u5230\u8fd9\u4e2a\u53c2\u6570\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5de6\u53f3\u62c9\u52a8\u6765\u6539\u53d8\u53c2\u6570 yVelocity\u3002\u6b64\u65f6\uff0c\u6211\u4eec\u70b9\u51fb\u53f3\u8fb9\u68c0\u67e5\u5668\u4e2d\u7684\u52a0\u53f7\u4e24\u6b21 Add Motion Field \u6765\u521b\u5efa\u4e24\u4e2a Motion\uff0c\u7136\u540e\u70b9\u51fb\u4e24\u4e2a\u65b0\u7684 Motion \u53f3\u8fb9\u5706\u5708\uff0c\u5206\u522b\u4e00\u6b21\u9009\u62e9 <code>playerFall<\/code> \u548c <code>playerJump<\/code>\u3002\u7136\u540e\u53d6\u6d88\u52fe\u9009 Automate Thresholds \u5206\u522b\u6539\u53d8 <code>playerFall<\/code> \u7684\u9608\u503c Threshol \u4e3a <code>-1<\/code> \u548c <code>playerJump<\/code> \u7684 Threshol \u4e3a <code>1<\/code>\uff0c\u8fd9\u91cc\u7684\u610f\u601d\u662f y \u8f74\u4e0a\u7684\u901f\u5ea6\u662f -1 \u4e5f\u5c31\u662f\u4eba\u7269\u5411\u4e0b\u843d\u4e0b\uff0c\u7136\u540e y \u8f74\u4e0a\u7684\u901f\u5ea6\u662f 1 \u4e5f\u5c31\u662f\u4eba\u7269\u5411\u4e0a\u8df3\u8dc3\u3002\u6211\u4eec\u53ef\u4ee5\u628a\u53f3\u4e0b\u89d2\u7684 Blend Tree \u8fd9\u4e00\u680f\u62c9\u4e0a\u6765\uff0c\u7136\u540e\u70b9\u51fb\u5f00\u59cb\u901a\u8fc7\u6ed1\u52a8\u5de6\u4e0a\u89d2 yVelocity \u7684\u503c\u8fdb\u884c\u6d4b\u8bd5\u3002<\/p>\n\n\n\n<p>\u63a5\u4e0b\u6765\u6211\u4eec\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u63a7\u5236\u8fd9\u4e2a yVelocity \u53c2\u6570\u3002\u8fdb\u5165 MoveController.cs \u811a\u672c\uff0c\u5728 AnimationControllers() \u65b9\u6cd5\u4e0b\u8bbe\u7f6e yVelocity \u7684\u503c\u5373\u53ef\uff08\u8fd9\u91cc\u5f15\u53f7\u4e0b\u7684\u8bcd\u548c\u524d\u9762\u52a8\u753b\u5668\u4e2d\u521b\u5efa\u7684\u540d\u5b57\u4e5f\u8981\u4e00\u6837\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anim.SetFloat(\"yVelocity\", rb.velocity.y);<\/code><\/pre>\n\n\n\n<p>\u63a5\u7740\u6211\u4eec\u9700\u8981\u786e\u4fdd\u53ea\u6709\u5f53\u89d2\u8272\u6ca1\u6709\u63a5\u89e6\u5730\u9762\u65f6\uff0c\u624d\u4f1a\u8fdb\u5165 Jump\/Fall \u8fd9\u4e2a\u6df7\u5408\u6811\uff0c\u6240\u4ee5\u6211\u4eec\u5728\u52a8\u753b\u5668\u65b0\u5efa\u4e00\u4e2a\u53c2\u6570 <code>isGrounded<\/code>\uff08\u53ef\u4ee5\u5728\u5de6\u8fb9\u62d6\u52a8 isGround \u53c2\u6570\u8ba9\u5176\u5728\u4e09\u4e2a\u53c2\u6570\u7684\u6700\u4e0a\u9762\uff0c\u8fd9\u4e5f\u662f\u5728\u53c2\u6570\u591a\u7684\u65f6\u5019\u4e00\u4e2a\u7b80\u5355\u7684\u5c0f\u6280\u5de7\uff09\u3002\u7136\u540e\u6211\u4eec\u7528\u4e00\u4e2a\u7bad\u5934\u8fde\u63a5 <code>playerIdle<\/code> \u548c <code>Jump\/Fall<\/code> \u4e24\u4e2a\u72b6\u6001\u3002\u4e3a\u4e86\u63a7\u5236\u4e24\u4e2a\u72b6\u6001\u7684\u8f6c\u6362\uff0c\u6211\u4eec\u70b9\u51fb\u7bad\u5934\u5728 Condiditons \u4e0b\u70b9\u51fb\u52a0\u53f7\u9009\u62e9 <code>isGrounded<\/code> \u53c2\u6570\uff0c\u5982\u679c\u8fd9\u91cc\u6709 <code>isMoving<\/code> \u53c2\u6570\u53ef\u4ee5\u70b9\u51fb\u51cf\u53f7\u5220\u9664\uff0c\u6211\u4eec\u5c06 <code>isGrounded<\/code> \u8fd9\u91cc\u7684\u53c2\u6570\u8bbe\u7f6e\u4e3a <code>false<\/code>\uff0c\u610f\u601d\u4e5f\u5c31\u662f\u5982\u679c\u89d2\u8272\u6ca1\u6709\u63a5\u89e6\u5730\u9762\u8fd9\u4e2a\u7bad\u5934\u624d\u751f\u6548\uff0c\u4e5f\u5c31\u662f <code>playerIdle<\/code> \u5230 <code>Jump\/Fall<\/code>\u3002\u548c\u524d\u9762\u4e00\u6837\uff0c\u4e3a\u4e86\u9632\u6b62\u52a8\u753b\u5ef6\u8fdf\uff0c\u6211\u4eec\u53d6\u6d88\u52fe\u9009 <code>Has Exit Time<\/code>\uff0c\u7136\u540e\u5c06 <code>Transition Duratior<\/code> \u6539\u4e3a <code>0<\/code>\u3002\u7136\u540e <code>Jump\/Fall<\/code> \u56de\u5230 <code>playerIdle<\/code> \u4e5f\u540c\u7406\uff0c\u5728\u4e2d\u95f4\u52a0\u5165\u7bad\u5934\uff0c\u7136\u540e Conditions \u7684\u53c2\u6570 <code>isGrounded<\/code> \u4e3a <code>true<\/code>\uff0c\u53d6\u6d88\u9000\u51fa\u65f6\u95f4\u7b49\u3002\u7136\u540e\u6211\u4eec\u5728\u4ee3\u7801\u4e2d\u5206\u914d isGrounded \u8fd9\u4e2a\u5e03\u5c14\u503c\uff0c\u4e5f\u662f\u540c\u7406\uff0c\u5728 AnimationControllers() \u65b9\u6cd5\u4e2d\u8bbe\u7f6e\uff08\u8fd9\u91cc\u5f15\u53f7\u4e0b\u7684\u8bcd\u548c\u524d\u9762\u52a8\u753b\u5668\u4e2d\u521b\u5efa\u7684\u540d\u5b57\u4e5f\u8981\u4e00\u6837\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anim.SetBool(\"isGrounded\", isGrounded);<\/code><\/pre>\n\n\n\n<p>\u4e0d\u8fc7\u8fd9\u6837\u5c31\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u901a\u8fc7\u52a8\u753b\u5668\u4f1a\u770b\u5230\u53ef\u4ee5\u4ece\u7a7a\u95f2\u72b6\u6001\u5230\u8df3\u8dc3\uff0c\u4f46\u4e0d\u80fd\u4ece\u79fb\u52a8\u72b6\u6001\u5230\u8df3\u8dc3\u3002\u56e0\u4e3a\u6211\u4eec\u6709\u4ece\u7a7a\u95f2\u5230\u8df3\u8dc3\u843d\u4e0b\u7684\u8fc7\u6e21\uff0c\u4f46\u6ca1\u6709\u4ece\u79fb\u52a8\u5230\u843d\u4e0b\u7684\u8fc7\u6e21\u3002\u8fd9\u91cc\u6709\u4e00\u4e2a\u7b80\u5355\u7684\u65b9\u6cd5\u6765\u89e3\u51b3\uff1a\u53bb\u6389 <code>playerIdle<\/code> \u5230 <code>Jump\/Fall<\/code> \u7684\u7bad\u5934\uff0c\u7136\u540e\u5c06 <code>Any State<\/code> \u8fde\u63a5 <code>Jump\/Fall<\/code>\u3002\u7136\u540e\u8fd9\u4e2a\u7bad\u5934\u7684\u8bbe\u7f6e\u4e00\u6837\uff0c\u9996\u5148 Conditions \u5c06 <code>isGrounded<\/code> \u9009\u62e9\u4e3a <code>false<\/code>\uff0c\u7136\u540e\u53d6\u6d88\u52fe\u9009 <code>Has Exit Time<\/code>\uff0c\u7136\u540e\u5c06 <code>Transition Duratior<\/code> \u6539\u4e3a <code>0<\/code>\uff0c\u5e76\u4e14\u5c06 <code>Can Transition To Self<\/code> \u53d6\u6d88\u52fe\u9009\uff0c\u8fd9\u91cc\u7684\u610f\u601d\u5c31\u662f\u5f53\u4efb\u4f55\u72b6\u6001\u4e0b\u89d2\u8272\u6ca1\u6709\u63a5\u89e6\u5730\u9762\u5c31\u8fdb\u5165 <code>Jump\/Fall<\/code> \u8fd9\u4e2a\u6df7\u5408\u6811\uff0c\u800c\u4e0d\u662f\u4ec5\u5f53\u89d2\u8272\u5728\u7a7a\u95f2\u72b6\u6001\u4e0b\u4e14\u89d2\u8272\u6ca1\u6709\u63a5\u89e6\u5730\u9762\u624d\u8fdb\u5165\u8df3\u8dc3\u72b6\u6001\u3002<\/p>\n\n\n\n<p>\u5f53\u6211\u4eec\u4e86\u89e3\u4e86\u4ec0\u4e48\u662f\u6df7\u5408\u6811\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u89d2\u8272\u7684\u7a7a\u95f2\u72b6\u6001\u548c\u79fb\u52a8\u72b6\u6001\u4e5f\u6574\u5408\u4e3a\u4e00\u4e2a\u6df7\u5408\u6811\uff0c\u4ee5\u6b64\u6765\u7b80\u5316\u6574\u4e2a\u52a8\u753b\u5668\u3002\u4e8e\u662f\u6211\u4eec\u9996\u5148\u5220\u9664 <code>playerIdle<\/code> \u548c <code>playerMove<\/code> \u4e24\u4e2a\u72b6\u6001\uff0c\u7136\u540e\u65b0\u5efa\u4e00\u4e2a\u6df7\u5408\u6811\uff0c\u5c06\u5176\u547d\u540d\u4e3a <code>Idle\/Move<\/code>\uff0c\u53cc\u51fb\u8fd9\u4e2a\u6df7\u5408\u6811\u8fdb\u5165\uff0c\u65b0\u5efa\u4e00\u4e2a\u53c2\u6570 <code>xVelocity<\/code>\uff0c\u7136\u540e\u5728\u53f3\u8fb9\u7684\u68c0\u67e5\u5668\u4e2d\u5c06\u53c2\u6570\u6539\u4e3a <code>xVelocity<\/code>\u3002\u7136\u540e\u5728\u68c0\u67e5\u5668\u4e2d\u6dfb\u52a0\u4e09\u4e2a <code>Motion<\/code>\uff0c\u5206\u522b\u5c06\u5176\u6dfb\u52a0\u4e3a <code>playerMove<\/code>\u3001<code>playerIdle<\/code>\u3001<code>playerMove<\/code>\uff0c\u7136\u540e\u53d6\u6d88\u52fe\u9009 Automate Thresholds\uff0c\u5c06\u4e09\u4e2a\u53c2\u6570\u5206\u522b\u6539\u4e3a <code>-1<\/code>\u3001<code>0<\/code>\u3001<code>1<\/code>\u3002\u5728\u52a8\u753b\u5668\u4e2d\u5c06 <code>Idle\/Move<\/code> \u8bbe\u7f6e\u4e3a\u9ed8\u8ba4\u72b6\u6001\u53f3\u952e Set as Layer Default State\u3002\u6700\u540e\u5c06\u52a8\u753b\u5668\u6539\u4e3a\u4e0b\u56fe\u6240\u793a\uff0c\u7bad\u5934\u8bbe\u7f6e\u53c2\u8003\u524d\u9762\u7684\u65b9\u6cd5\u3002<\/p>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/tobykskgd.life\/wp-content\/uploads\/2025\/08\/Animation.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"838\" height=\"343\" data-original=\"https:\/\/tobykskgd.life\/wp-content\/uploads\/2025\/08\/Animation.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-3470\"  sizes=\"auto, (max-width: 838px) 100vw, 838px\" \/><\/div><\/figure>\n\n\n\n<p>\u6700\u540e\u9700\u8981\u505a\u7684\u5c31\u662f\u5728\u4ee3\u7801\u4e2d\u8bbe\u7f6e <code>xVelocity<\/code> \u53c2\u6570\u5373\u53ef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>anim.SetFloat(\"xVelocity\", rb.velocity.x);<\/code><\/pre>\n\n\n\n<p>\u53e6\u5916\u7531\u4e8e\u7b80\u5316\u4e86\u52a8\u753b\u5668\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u524d\u9762\u7684\u8fd9\u4e24\u884c\u4ee3\u7801\u7ed9\u4e00\u5e76\u5220\u9664\u4e86\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>bool isMoving = rb.velocity.x != 0;<br>anim.SetBool(\"isMoving\", isMoving);<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u4fbf\u662f\u6df7\u5408\u6811\u7684\u8fd0\u7528\u3002\u4e0b\u9762\u662f\u76ee\u524d\u79fb\u52a8\u811a\u672c\u7684\u5168\u90e8\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using UnityEngine;\n\u200b\npublic class MoveController : MonoBehaviour\n{\n \u00a0 \u00a0private Rigidbody2D rb;\n \u00a0 \u00a0private Animator anim;\n\u200b\n \u00a0  &#91;SerializeField] private float moveSpeed;\n \u00a0  &#91;SerializeField] private float jumpForce;\n \u00a0 \u00a0private float xInput;\n\u200b\n \u00a0  &#91;Header(\"Collision chect\")]\n \u00a0  &#91;SerializeField] private float groundCheckRadius;\n \u00a0  &#91;SerializeField] private Transform groundCheck;\n \u00a0  &#91;SerializeField] private LayerMask whatIsGround;\n \u00a0 \u00a0private bool isGrounded;\n\u200b\n \u00a0 \u00a0\/\/ Start is called before the first frame update\n\n \u00a0 \u00a0void Start()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb = GetComponent&lt;Rigidbody2D>();\n \u00a0 \u00a0 \u00a0 \u00a0anim = GetComponent&lt;Animator>();\n \u00a0 \u00a0 \u00a0 \u00a0jumpForce = 4;\n \u00a0  }\n\u200b\n \u00a0 \u00a0\/\/ Update is called once per frame\n \u00a0 \u00a0void Update()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0AnimationControllers();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0CollisionChecks();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0xInput = Input.GetAxis(\"Horizontal\");\n \u00a0 \u00a0 \u00a0 \u00a0Movement();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0if (Input.GetKeyDown(KeyCode.Space))\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Jump();\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void AnimationControllers()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0anim.SetFloat(\"xVelocity\", rb.velocity.x);\n \u00a0 \u00a0 \u00a0 \u00a0anim.SetFloat(\"yVelocity\", rb.velocity.y);\n \u00a0 \u00a0 \u00a0 \u00a0anim.SetBool(\"isGrounded\", isGrounded);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void CollisionChecks()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void Movement()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void Jump()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0if (isGrounded)\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(rb.velocity.x, jumpForce);\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void OnDrawGizmos()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);\n \u00a0  }\n}\n\u200b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h3 class=\"wp-block-heading\">Character flip<\/h3>\n\n\n\n<p>\u6700\u540e\uff0c\u6211\u4eec\u5c06\u5728\u79fb\u52a8\u65f6\u7ffb\u8f6c\u6211\u4eec\u7684\u89d2\u8272\u3002\u56e0\u6b64\uff0c\u5f53\u4f60\u5411\u5de6\u8dd1\u65f6\uff0c\u4ed6\u4e5f\u5411\u5de6\u3002\u6211\u4eec\u8981\u505a\u7684\u5c31\u662f\u63a7\u5236\u89d2\u8272\u5411\u5de6\u79fb\u52a8\u65f6\u7684\u65cb\u8f6c\u3002\u6bd4\u5982\u73b0\u5728\u6211\u4eec\u7684\u89d2\u8272\u5411\u53f3\u770b\uff0c\u6b64\u65f6\u4ed6\u7684 <code>Rotation<\/code> \u7684 <code>Y<\/code> \u4e3a <code>0<\/code>\uff0c\u4e5f\u5c31\u662f\u4ed6\u5728 <code>y<\/code> \u8f74\u4e0a\u7684\u65cb\u8f6c\u4e3a <code>0<\/code>\uff0c\u5f53\u6211\u4eec\u5c06\u5176\u6539\u4e3a <code>180<\/code>\uff0c\u5c31\u4f1a\u53d1\u73b0\u89d2\u8272\u5411\u5de6\u4e86\u3002\u8fd9\u5c31\u662f\u6211\u4eec\u9700\u8981\u5728\u4ee3\u7801\u4e2d\u505a\u7684\u4e8b\u60c5\u3002<\/p>\n\n\n\n<p>\u6211\u4eec\u5728\u4ee3\u7801\u4e2d\u521b\u5efa\u4e00\u4e2a\u5e03\u5c14\u503c <code>facingRight<\/code> \uff08\u9ed8\u8ba4\u60c5\u51b5\u4e0b\u89d2\u8272\u5c06\u671d\u53f3\uff09\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private bool facingRight = true;<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u5199\u4e00\u4e2a\u7ffb\u8f6c\u65b9\u6cd5\uff0c\u5c31\u662f\u5f53\u6267\u884c\u8fd9\u4e2a\u65b9\u6cd5\u65f6 <code>facingRight<\/code> \u8fd9\u4e2a\u5e03\u5c14\u503c\u4e0d\u7b49\u4e8e\u5b83\u81ea\u5df1\u4e5f\u5c31\u662f\u76f8\u53cd\uff0c\u7136\u540e\u65cb\u8f6c\u89d2\u8272\u8fdb\u884c\u7ffb\u8f6c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void Flip() <br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;facingRight = !facingRight;<br> &nbsp;      transform.Rotate(0, 180, 0);<br> &nbsp;  }<\/code><\/pre>\n\n\n\n<p>\u7136\u540e\u6211\u4eec\u521b\u5efa\u4e00\u4e2a\u65b0\u65b9\u6cd5 FlipController() \u8c03\u7528 Flip() \u65b9\u6cd5\u5373\u53ef\uff0c\u5177\u4f53\u5b9e\u73b0\u4e3a\u5f53\u89d2\u8272\u5411\u5de6\u79fb\u52a8\u65f6\uff0c\u5176 x \u8f74\u4e0a\u7684\u901f\u5ea6\u4e3a\u8d1f\u6570\uff0c\u7136\u540e\u6b64\u65f6\u5982\u679c facingRight \u4e5f\u4e3a true \u4e5f\u5c31\u662f\u6b64\u65f6\u89d2\u8272\u671d\u53f3\uff0c\u6211\u4eec\u5c31\u8fdb\u884c\u7ffb\u8f6c\uff0c\u5411\u53f3\u65f6\u53cd\u4e4b\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private void FlipController()<br> &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp;if (rb.velocity.x &lt; 0 &amp;&amp; facingRight)<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Flip();<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp; &nbsp; &nbsp; &nbsp;else if (rb.velocity.x &gt; 0 &amp;&amp; !facingRight)<br> &nbsp; &nbsp; &nbsp;  {<br> &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Flip();<br> &nbsp; &nbsp; &nbsp;  }<br> &nbsp;  }<\/code><\/pre>\n\n\n\n<p>\u6700\u540e\u6211\u4eec\u5728 Update() \u65b9\u6cd5\u4e2d\u8c03\u7528 FlipController() \u65b9\u6cd5\u5373\u53ef\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>FlipController();<\/code><\/pre>\n\n\n\n<p>\u4ee5\u4e0a\u4fbf\u662f\u7ffb\u8f6c\u89d2\u8272\u6240\u9700\u8981\u7684\u5168\u90e8\u5185\u5bb9\u3002\u4e0b\u9762\u662f\u672c\u6b21\u8bfe\u7a0b\u4e2d\u89d2\u8272\u79fb\u52a8\u811a\u672c\u7684\u5b8c\u6574\u4ee3\u7801\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>using UnityEngine;\n\u200b\npublic class MoveController : MonoBehaviour\n{\n \u00a0 \u00a0private Rigidbody2D rb;\n \u00a0 \u00a0private Animator anim;\n\u200b\n \u00a0  &#91;SerializeField] private float moveSpeed;\n \u00a0  &#91;SerializeField] private float jumpForce;\n \u00a0 \u00a0private float xInput;\n\u200b\n \u00a0  &#91;Header(\"Collision chect\")]\n \u00a0  &#91;SerializeField] private float groundCheckRadius;\n \u00a0  &#91;SerializeField] private Transform groundCheck;\n \u00a0  &#91;SerializeField] private LayerMask whatIsGround;\n \u00a0 \u00a0private bool isGrounded;\n \u00a0 \u00a0private bool facingRight = true;\n\u200b\n \u00a0 \u00a0\/\/ Start is called before the first frame update\n\n \u00a0 \u00a0void Start()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb = GetComponent&lt;Rigidbody2D>();\n \u00a0 \u00a0 \u00a0 \u00a0anim = GetComponent&lt;Animator>();\n \u00a0 \u00a0 \u00a0 \u00a0jumpForce = 4;\n \u00a0  }\n\u200b\n \u00a0 \u00a0\/\/ Update is called once per frame\n \u00a0 \u00a0void Update()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0AnimationControllers();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0CollisionChecks();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0FlipController();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0xInput = Input.GetAxis(\"Horizontal\");\n \u00a0 \u00a0 \u00a0 \u00a0Movement();\n\u200b\n \u00a0 \u00a0 \u00a0 \u00a0if (Input.GetKeyDown(KeyCode.Space))\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Jump();\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void AnimationControllers()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0anim.SetFloat(\"xVelocity\", rb.velocity.x);\n \u00a0 \u00a0 \u00a0 \u00a0anim.SetFloat(\"yVelocity\", rb.velocity.y);\n \u00a0 \u00a0 \u00a0 \u00a0anim.SetBool(\"isGrounded\", isGrounded);\n \u00a0  }\n\u200b\n\u200b\n \u00a0 \u00a0private void Movement()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(xInput * moveSpeed, rb.velocity.y);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void Jump()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0if (isGrounded)\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0rb.velocity = new Vector2(rb.velocity.x, jumpForce);\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void Flip() \n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0facingRight = !facingRight; \/\/ works as a switcher\n \u00a0 \u00a0 \u00a0 \u00a0transform.Rotate(0, 180, 0);\n \u00a0  }\n\u200b\n \u00a0 \u00a0private void FlipController()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0if (rb.velocity.x &lt; 0 &amp;&amp; facingRight)\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Flip();\n \u00a0 \u00a0 \u00a0  }\n \u00a0 \u00a0 \u00a0 \u00a0else if (rb.velocity.x > 0 &amp;&amp; !facingRight)\n \u00a0 \u00a0 \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0Flip();\n \u00a0 \u00a0 \u00a0  }\n \u00a0  }\n \u00a0 \u00a0private void CollisionChecks()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0isGrounded = Physics2D.OverlapCircle(groundCheck.position, groundCheckRadius, whatIsGround);\n \u00a0  }\n \u00a0 \u00a0private void OnDrawGizmos()\n \u00a0  {\n \u00a0 \u00a0 \u00a0 \u00a0Gizmos.DrawWireSphere(groundCheck.position, groundCheckRadius);\n \u00a0  }\n}\n\u200b<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">Section 5: Not the end<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">What is next?<\/h3>\n\n\n\n<p>\u4ed3\u5e93\u4e2d <code>.\/Onion Lad<\/code> \u4e0b\u7684 <code>Udemy course - Onion Lad.exe<\/code> \u6587\u4ef6\u53ef\u4ee5\u5728 64 \u4f4d\u7684 Windows \u7cfb\u7edf\u4e0b\u8fd0\u884c\u672c\u6b21\u8bfe\u7a0b\u7684\u5bfc\u51fa\u5185\u5bb9\u4f9b\u53c2\u8003\u3002<\/p>\n\n\n\n<p>\u4ee5\u4e0a\u4fbf\u662f\u5bf9 Unity \u5f15\u64ce\u548c C# \u8bed\u8a00\u7684\u521d\u89c1\uff0c\u76f8\u4fe1\u6709\u4e86\u672c\u8282\u8bfe\u7a0b\uff0c\u5bf9\u5728 Unity \u73af\u5883\u5f00\u53d1\u5e94\u8be5\u6709\u4e86\u4e00\u4e2a\u6574\u4f53\u7684\u611f\u53d7\uff0c\u660e\u767d\u4e86 Unity \u5f15\u64ce\u7684\u7279\u8272\u4ee5\u6b64\u6765\u5224\u65ad\u662f\u5426\u5408\u9002\u81ea\u5df1\u7684\u5f00\u53d1\u3002\u90a3\u4e48\u63a5\u4e0b\u91cc\u6211\u89c9\u5f97\u5c31\u53ef\u4ee5\u5165\u624b\u4e00\u4e9b\u7b80\u5355\u7684\u9879\u76ee\u4e86\uff01<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Congrats on finishing the course!<\/h2>\n","protected":false},"excerpt":{"rendered":"<p>Udemy \u4e0a\u9762\u7684 Alex Dev \u7684\u8bfe\u7a0b Basics of C# and Unity for Compl [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3476,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[48,3,37,24],"class_list":["post-3464","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-xxbj","tag-unity","tag-xxbj","tag-jc","tag-game"],"_links":{"self":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/3464","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/comments?post=3464"}],"version-history":[{"count":6,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/3464\/revisions"}],"predecessor-version":[{"id":3475,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/posts\/3464\/revisions\/3475"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/media\/3476"}],"wp:attachment":[{"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/media?parent=3464"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/categories?post=3464"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/tobykskgd.life\/index.php\/wp-json\/wp\/v2\/tags?post=3464"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}