简体   繁体   中英

Unity 2d player jumping infinitely

Im new to unity and this is my first game but my player is jumping infinitely and I've watched a lot of tutorials and still dont know how to fix it.

heres my code

public float moveSpeed = 5f;

void Update()
{

    Jump();
    Vector3 movement = new Vector3(Input.GetAxis("Horizontal"), 0f, 0f);
    transform.position += movement * Time.deltaTime * moveSpeed;

    Vector3 characterScale = transform.localScale;
    if (Input.GetAxis("Horizontal") < 0)
    {
        characterScale.x = 1;
    }


    if (Input.GetAxis("Horizontal") > 0)
    {
        characterScale.x = -1;
    }
    transform.localScale = characterScale;
}

void Jump()
{
    
    if (Input.GetButtonDown("Jump"))
    {
        gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 15f), ForceMode2D.Impulse);
    }

    
}

}

There are a couple of things you have there. First you have Input.GetButtonDown("Jump") This means while the player is holding the button down it will execute that script inside the if statement, which applies a force. So this will run every frame and every frame while the player is holding down the button it will apply that force. You can try to do Input.GetButtonUp("Jump") which will be true when the player lets go of the button, then it will apply that force. Now you can keep the GetButtonDown its no problem if thats the feel you are going foor.

But the real problem and the second this is, you need to check if the player is touching the ground or not. If he is touching the ground then you can apply that force. If he is not touching the ground then dont apply the force.

There are couple of ways to go about this, the easiest way is to create a new Layer and call it Ground or something.

层

You click that drop down and click on Add layer.. then you can add a layer, then go back to ground gameobject and assign that layer to that. Now am assuming that the ground has a collider on it so the player doesnt go through it.

After that you need a reference to the player collider. In the Start() method you can add this:

private Collider2D myCollider;

void Start()
{
   // This will get a reference to the collider 2d on the player
   myCollider = GetComponent<Collider2D>();
}

void Update()
{
   .
   .
   .
   // This means the player is touching the ground layer.
   if (myCollider.IsTouchingLayers(LayerMask.GetMask("Ground")))
   {
      if (Input.GetButtonDown("Jump"))
       {
          gameObject.GetComponent<Rigidbody2D>().AddForce(new Vector2(0f, 15f), ForceMode2D.Impulse);
       }
   }
}

So what this script does, it gets a reference to the player collider, and then checks if that player collider is touching the layer called ground. The ground needs to have a collider as well not just to prevent the player from fall through the level, but also to trigger this boolean to be true or false. True if they are touching each other, false if they are not. So, if they are not touching the ground then it doesnt matter how many times the player will press that button, he will not jump. Once they do then it applies the jump force if they are pressing Jump.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM