{"id":812,"date":"2020-06-21T15:47:44","date_gmt":"2020-06-21T07:47:44","guid":{"rendered":"http:\/\/summid.icu\/?p=812"},"modified":"2023-06-01T15:18:59","modified_gmt":"2023-06-01T07:18:59","slug":"unity2djumpcode","status":"publish","type":"post","link":"https:\/\/summid.icu\/index.php\/2020\/06\/21\/unity2djumpcode\/","title":{"rendered":"Unity2D\u6a2a\u677f\u6e38\u620f\u89d2\u8272\u8df3\u8dc3\u4f18\u5316\u4ee3\u7801"},"content":{"rendered":"<p><!--more--><\/p>\n<p><span style=\"font-family: 'Microsoft YaHei', 'Microsoft JhengHei', \u9ed1\u4f53;\">\u4e00\u5207\u90fd\u8981\u4eceUpdate()\u548cFixedUpdate()\u8fd9\u4e24\u4e2a\u6298\u78e8\u602a\u8bb2\u8d77\uff1a<\/span><\/p>\n<ul>\n<li><span style=\"font-family: 'Microsoft YaHei', 'Microsoft JhengHei', \u9ed1\u4f53;\">Update()\u5728\u6e38\u620f\u8fd0\u884c\u65f6\uff0c\u6bcf\u4e00\u5e27\u90fd\u4f1a\u8c03\u7528\u4e00\u6b21\u8be5\u65b9\u6cd5\u3002\u4f46\u5728\u4e0d\u540c\u7684\u673a\u5668\u4e0a\uff0c\u6e38\u620f\u8fd0\u884c\u6548\u7387\uff0c\u5e27\u7387\u662f\u4e0d\u540c\u7684\uff1b\u5c31\u7b97\u5728\u4e00\u53f0\u673a\u5668\u4e0a\u8dd1\uff0c\u5e27\u7387\u4e5f\u662f\u6709\u6d6e\u52a8\u7684\u3002\u8fd9\u5c31\u5bfc\u81f4\u5bf9Update()\u7684\u8c03\u7528\u5e76\u4e0d\u662f\u4e25\u683c\u5468\u671f\u6027\u7684\uff0c\u5982\u679c\u5c06\u6e38\u620f\u4e2d\u5bf9\u521a\u4f53\u7684\u8fd0\u7b97\u653e\u5728Update()\u91cc\u2014\u2014\u6bd4\u5982\u89d2\u8272\u7684\u79fb\u52a8\u3001\u8df3\u8dc3\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u89d2\u8272\u7684\u79fb\u52a8\u901f\u5ea6\u3001\u8df3\u8dc3\u9ad8\u5ea6\u6709\u53d8\u5316\uff0c\u8fd9\u4e0d\u662f\u6211\u4eec\u60f3\u8981\u7684\u3002<\/span><\/li>\n<li><span style=\"font-family: 'Microsoft YaHei', 'Microsoft JhengHei', \u9ed1\u4f53;\">FixedUpdate()\u5219\u662f\u4ee5\u56fa\u5b9a\u5468\u671f\u8c03\u7528\uff08\u6bcf0.02S\u8c03\u7528\u4e00\u6b21\uff09\uff0c\u5373\u4f7f\u5e27\u7387\u4e0d\u540c\uff0c\u5b83\u7684\u8c03\u7528\u9891\u7387\u4e5f\u662f\u4e0d\u53d8\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u4e00\u822c\u5c06\u521a\u4f53\u7684\u8fd0\u7b97\u653e\u5728FixedUpdate()\u4e2d\u3002<\/span><\/li>\n<\/ul>\n<p><span style=\"font-family: 'Microsoft YaHei', 'Microsoft JhengHei', \u9ed1\u4f53;\">\u4f46\u8fd9\u4e5f\u4f1a\u5bfc\u81f4\u65b0\u7684\u95ee\u9898\uff1a<\/span><br \/><span style=\"font-family: 'Microsoft YaHei', 'Microsoft JhengHei', \u9ed1\u4f53;\">\u5982\u679c\u6211\u4eec\u5c06\u89d2\u8272\u7684\u8df3\u8dc3\u529f\u80fd\u653e\u5728FixedUpdate()\u4e2d\uff0c\u5305\u62ec\u73a9\u5bb6\u8f93\u5165\u68c0\u6d4b\u548c\u5b9e\u73b0\u4ee3\u7801\u4e00\u5e76\u653e\u5728FixedUpdate()\u91cc\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u201c\u6309\u4e0b\u8df3\u8dc3\u952e\u4f46\u662f\u89d2\u8272\u6ca1\u53cd\u5e94\u201d\u7684\u60c5\u51b5\u3002<\/span><br \/><span style=\"font-family: 'Microsoft YaHei', 'Microsoft JhengHei', \u9ed1\u4f53;\">\u8fd9\u5e94\u8be5\u662fFixedUpdate()\u65b9\u6cd5\u7684\u5f0a\u7aef\uff0c\u5bfc\u81f4\u6211\u4eec\u6709\u65f6\u6309\u4e0b\u8df3\u8dc3\u952e\u540e\uff0c\u8f93\u5165\u5e76\u6ca1\u6709\u88abFixedUpdate()\u63a5\u6536\u3002<\/span><br \/><span style=\"font-family: 'Microsoft YaHei', 'Microsoft JhengHei', \u9ed1\u4f53;\">\u56e0\u6b64\u6211\u4eec\u53ef\u4ee5\u5c06\u8f93\u5165\u68c0\u6d4b\u653e\u5728Update()\u91cc\uff0c\u5c06\u8df3\u8dc3\u4ee3\u7801\u653e\u5728FixedUpdate()\u91cc\u3002<\/span><\/p>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">using System.Collections;\nusing System.Collections.Generic;\nusing UnityEngine;\n\npublic class FinalMovement : MonoBehaviour\n{\n    private Rigidbody2D rb;\n    private Animator anim;\n    private Collider2D Ccoll;\n\n    public LayerMask ground;\n    public Transform groundCheck;\n\n    private float speed, jumpForce;\n    private bool isJump, isGround;\n    private int jumpCount;\n    private bool jumpPressed;\/\/\u5f53\u8df3\u8dc3\u952e\u6309\u4e0b\u7684\u65f6\u5019\n\n    private void Awake()\n    {\n        rb = GetComponent&lt;Rigidbody2D>();\n        anim = GetComponent&lt;Animator>();\n        Ccoll = GetComponent&lt;CircleCollider2D>();\n        speed = 7f;\n        jumpForce = 7f;\n        jumpCount = 2;\n    }\n\n    void Update()\n    {\n        if(Input.GetButtonDown(\"Jump\") &amp;&amp; jumpCount>0)\n        {\n            jumpPressed = true;\n        }\n    }\n\n    private void FixedUpdate()\n    {\n        Movement();\n\n        \/\/\u68c0\u6d4b\u662f\u5426\u5728\u5730\u9762\n        isGround=Ccoll.IsTouchingLayers(ground);\n        Jump();\n\n        switchAnim();\n    }\n\n    private void Movement()\n    {\n        float horizontalMove = Input.GetAxisRaw(\"Horizontal\");\n\n        rb.velocity = new Vector2(horizontalMove * speed, rb.velocity.y);\/\/\u8bbe\u7f6e\u901f\u5ea6\n        if(horizontalMove!=0)\n        {\n            transform.localScale = new Vector3(horizontalMove, 1, 1);\/\/\u66f4\u6539\u671d\u5411\n        }\n    }\n\n    private void Jump()\n    {\n        if(isGround)\n        {\n            jumpCount = 2;\n            isJump = false;\n        }\n        if (jumpPressed &amp;&amp; isGround)\n        {\n            isJump = true;\n            rb.velocity = new Vector2(rb.velocity.x, jumpForce);\n            jumpCount--;\n            jumpPressed = false;\/\/\u522b\u5fd8\u4e86\u8bbe\u7f6e\n        }\n        else if (jumpPressed &amp;&amp; !isGround &amp;&amp; jumpCount > 0)\/\/\u5728\u7a7a\u4e2d\u6309\u4e0b\u8df3\u8dc3\u952e\n        {\n            rb.velocity = new Vector2(rb.velocity.y, jumpForce);\n            jumpCount--;\n            jumpPressed = false;\n        }\n    }\n\n    private void switchAnim()\n    {\n        anim.SetFloat(\"Running\", Mathf.Abs(rb.velocity.x));\n        if(Ccoll.IsTouchingLayers(ground))\n        {\n            anim.SetBool(\"Falling\", false);\n        }\n        else if(rb.velocity.y>0)\/\/\u8df3\u8dc3\n        {\n            anim.SetBool(\"Jumping\", true);\n            anim.SetBool(\"Falling\", false);\n        }\n        else if(rb.velocity.y&lt;0)\/\/\u4e0b\u843d\n        {\n            anim.SetBool(\"Jumping\", false);\n            anim.SetBool(\"Falling\", true);\n        }\n    }\n}<\/pre>\n\n\n\n<hr class=\"wp-block-separator is-style-wide\"\/>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[17],"tags":[],"class_list":["post-812","post","type-post","status-publish","format-standard","hentry","category-unity3d"],"_links":{"self":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts\/812","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/comments?post=812"}],"version-history":[{"count":6,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts\/812\/revisions"}],"predecessor-version":[{"id":824,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/posts\/812\/revisions\/824"}],"wp:attachment":[{"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/media?parent=812"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/categories?post=812"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/summid.icu\/index.php\/wp-json\/wp\/v2\/tags?post=812"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}