English 中文(简体)
为什么我可以跳多次,即使我没有设置额外的跳跃?
原标题:Why can I jump multiple times, even tho I set up no extra jumps?

我写了这个剧本,出于某种原因,我离开地面后最多可以跳两次。这真的很奇怪,因为正如你所看到的,maxJumps被设置为1。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerMovement : MonoBehaviour
{
    private float horizontal;
    private float speed = 8f;
    private float jumpingPower = 16f;
    private bool isFacingRight = true;
    private bool isGrounded = false;
    public float maxJumps = 1;
    private float currentJumps;

    public float coyoteTimerMax = .03f;
    private float coyoteTimer;

    private float jumpBuffer;
    public float maxJumpBuffer = .04f;

    [SerializeField] private Rigidbody2D rb;
    [SerializeField] private Transform groundCheck;
    [SerializeField] private LayerMask groundLayer;

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        horizontal = Input.GetAxisRaw("Horizontal");

        if (Input.GetButtonDown("Jump"))
        {
            jumpBuffer = maxJumpBuffer;
        }
        

        if (isGrounded)
        {
            currentJumps = 0; 
            coyoteTimer = coyoteTimerMax;
            
        }
        else
        {
            coyoteTimer -= Time.deltaTime;
            
        }

        jumpBuffer -= Time.deltaTime;

        if (jumpBuffer > 0 && coyoteTimer > 0 && currentJumps < maxJumps)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
            currentJumps++;
            jumpBuffer = 0; // Reset the jump buffer after successfully jumping
        }

        if (Input.GetButtonUp("Jump") && rb.velocity.y > 0)
        {
            rb.velocity = new Vector2(rb.velocity.x, rb.velocity.y * 0.5f);
        }

        
        

        

        Flip();

        
    }

    private void FixedUpdate()
    {
        rb.velocity = new Vector2(horizontal * speed, rb.velocity.y);
    }

    void OnCollisionEnter2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Ground"))
        {
            isGrounded = true;
        }
    }

    void OnCollisionExit2D(Collision2D other)
    {
        if (other.gameObject.CompareTag("Ground"))
        {
            isGrounded = false;
        }
    }

        private void Flip()
    {
        if (isFacingRight && horizontal < 0f || !isFacingRight && horizontal > 0f)
        {
            isFacingRight = !isFacingRight;
            Vector3 localScale = transform.localScale;
            localScale.x *= -1f;
            transform.localScale = localScale;

        }
    }
}
问题回答

以下是我认为正在发生的事情:

  1. You press space, which makes you jump
  2. You jump and currentJumps increases by 1, to 1
  3. (Here is the important part): The next frame comes so fast that you don t leave the ground yet. This causes currentJumps to be reset to 0 because it still is grounded.

要解决此问题,您可以将此部分更改为:

        if (jumpBuffer > 0 && coyoteTimer > 0 && currentJumps < maxJumps)
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpingPower);
            currentJumps++;
            isGrounded = false;
            jumpBuffer = 0; // Reset the jump buffer after successfully jumping
        }

这样,它会在您跳跃后立即将isGrounded设置为false。下一帧,它不会将currentJumps设置为0,因为isGrounded=false。

您可能仍然希望保留OnCollisionExit2D,因为如果您正在摔倒,但不是从跳跃中摔倒,它会将isGrounded设置为false。

如果您有任何问题,可以留言评论。





相关问题
Finding the angle of a fleeing dodo

I want my critter AI to run directly away from the player when the player kicks it, and I don t know the math nor the syntax to calculate that angle. They are two characters moving independently ...

Game engines for iPhone vs. native iPhone sdk development

What are people s opinions and/or experiences with game engines such as Unity or Torque Engine? If one were new to iPhone game dev is it worth it to learn one of the engines? What is the performance ...

Random 2d movement (similar to flies) in Unity3d

I want to add random movement to some of my game objects similar to the way flies swarm in Unity3D. I ve developed a method using the addforce() method but would like to bypass the physics engine. ...

热门标签