2D游戏角色移动

发布时间 2023-08-17 21:31:45作者: Jet清远

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角色实现二段跳的代码,具体的动画切换可根据需求进行添加