Search Unity

Why can the player jump as many times as they want? [c#]

Discussion in '2D' started by aRandomWiseGuy, Jun 18, 2017.

  1. aRandomWiseGuy

    aRandomWiseGuy

    Joined:
    Jul 31, 2015
    Posts:
    4
    Can not figure how to make it so the player can only jump once.
    Anyone any idea what I'm doing wrong?
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. [RequireComponent(typeof(Rigidbody2D))]
    5. public class SAExamplePlayerController : MonoBehaviour
    6. {
    7.     public Animator animator;
    8.     public SpriteRenderer sprite;
    9.     public BoxCollider2D boxCol;
    10.     public float runSpeed;
    11.     public float jumpForce;
    12.  
    13.     public Rigidbody2D Body { get; private set; }
    14.     public bool IsJumping   { get; private set; }
    15.     public bool IsGrounded  { get; private set; }
    16.  
    17.     private void Awake()
    18.     {
    19.         Body = GetComponent<Rigidbody2D>();
    20.     }
    21.  
    22.     private void OnCollisionEnter2D(Collision2D col)
    23.     {
    24.         foreach(ContactPoint2D contact in col.contacts)
    25.         {
    26.             if(contact.normal.y == 1.0f)
    27.             {
    28.                 IsGrounded = true;
    29.                 animator.SetBool("IsGrounded", IsGrounded);
    30.                 break;
    31.             }
    32.         }
    33.     }
    34.  
    35.     private void OnCollisionExit2D(Collision2D col)
    36.     {
    37.         foreach (ContactPoint2D contact in col.contacts)
    38.         {
    39.             if (contact.normal.y == 1.0f)
    40.             {
    41.                 IsGrounded = false;
    42.                 animator.SetBool("IsGrounded", IsGrounded);
    43.                 break;
    44.             }
    45.         }
    46.     }
    47.  
    48.     private void Update()
    49.     {
    50.  
    51.         float horizontal = Input.GetAxisRaw("Horizontal");
    52.        
    53.         if (horizontal > 0.0f)
    54.         {
    55.             Body.velocity = new Vector2(runSpeed, Body.velocity.y);
    56.             sprite.flipX = false;
    57.             animator.SetBool("Running", true);
    58.         }
    59.         else if (horizontal < 0.0f)
    60.         {
    61.             Body.velocity = new Vector2(-runSpeed, Body.velocity.y);
    62.             sprite.flipX = true;
    63.             animator.SetBool("Running", true);
    64.         }
    65.         else
    66.         {
    67.             Body.velocity = new Vector2(0.0f, Body.velocity.y);
    68.             animator.SetBool("Running", false);
    69.         }
    70.  
    71.         if(Input.GetButtonDown("Jump"))
    72.             Jump();
    73.  
    74.         animator.SetFloat("VelocityY", Body.velocity.y);
    75.     }
    76.    
    77.     public void Jump()
    78.     {
    79.         if(!IsJumping && IsGrounded)
    80.         {
    81.             Body.AddForce(Vector2.up * jumpForce, ForceMode2D.Force);
    82.         }
    83.     }
    84. }
    85.  
     
  2. capnjake

    capnjake

    Joined:
    Nov 12, 2013
    Posts:
    53
    Where does your player start initially? Have you done any debugging? In general, using Raycasts is more reliable for collision detection.
     
  3. daxiongmao

    daxiongmao

    Joined:
    Feb 2, 2016
    Posts:
    412
    I don't see where you change your isjumping variable