void Start() { rb = GetComponent<Rigidbody2D>(); anim = GetComponent<Animator>(); } void FixedUpdate() { Movement(); jump(); SwitchAnimation(); } void Update() { if (Input.GetButtonDown("Jump") && jumpCount > 0) { jumpPressed = true; } } void Movement() { horizontalMove = Input.GetAxis("Horizontal"); switchFace = Input.GetAxisRaw("Horizontal"); rb.velocity = new Vector2(horizontalMove * speed, rb.velocity.y); if (horizontalMove != 0) { anim.SetFloat("speed", Mathf.Abs(horizontalMove)); } if (switchFace != 0) { transform.localScale = new Vector3(-switchFace, 1, 1); } } void jump() { isOnGround = Physics2D.OverlapCircle(groundCheck.position, groundCheckDistance, ground); if (isOnGround) { jumpCount = 2; isJump = false; } if (isOnGround && jumpPressed) { isJump = true; rb.velocity = new Vector2(rb.velocity.x, jumpForce); jumpCount--; jumpPressed = false; } else if (!isOnGround && jumpPressed && jumpCount > 0) { rb.velocity = new Vector2(rb.velocity.x, jumpForce); jumpCount--; jumpPressed = false; } }
此为2D角色实现二段跳的代码,具体的动画切换可根据需求进行添加
翻译
搜索
复制
标签:游戏,角色,jumpCount,void,2D,rb,velocity,isOnGround,jumpPressed From: https://www.cnblogs.com/qingyuan0213/p/17638903.html