Search Unity

Player sometimes double jumping when not intended.

Discussion in 'Scripting' started by jwalden, Jan 24, 2018.

  1. jwalden

    jwalden

    Joined:
    May 30, 2017
    Posts:
    62
    Hey guys, I found that my player was able to stick to the walls by accident when holding down one of the movement keys. Not sure why this happened, but I used a physics material to remove friction from the walls so that this wouldn't occur.
    I then later added back friction so that I could allow my play to attach to walls and jump off of them.
    The problem is, when my player is attached to the wall and I jump off of it in a particular way (depending on how I release my A and D keys) the jump doesn't reduce the jump count, and they will be allowed a second jump.

    Below is my entire code (not yet cleaned up):

    Code (CSharp):
    1. public class PlayerController : MonoBehaviour
    2. {
    3.     Rigidbody2D rb;
    4.     GameController gControl;
    5.     [SerializeField]
    6.     float moveSpeed;
    7.  
    8.     public int health;
    9.     public float jumpForce;
    10.     [SerializeField]
    11.     int maxJump = 1;
    12.     [SerializeField]
    13.     int currentJump;
    14.     //jump parameters
    15.     public float fallMultiplier = 3.5f;
    16.     public float lowJumpMultiplier = 2f;
    17.     //checks for when we begin to fall, and counts up
    18.     public int fallStart = 0;
    19.     //measures the damage we will take depending on the number fallStart count reaches.
    20.     public int fallDamage = 0;
    21.     //checks
    22.     bool facingRight;
    23.     bool attack;
    24.     [SerializeField]
    25.     bool grounded;
    26.     [SerializeField]
    27.     bool wallAttached;
    28.     [SerializeField]
    29.     bool canWallJump;
    30.     [SerializeField]
    31.     bool onWall;
    32.     /// <summary>
    33.     /// air and falling bools
    34.     /// </summary>
    35.     [SerializeField]
    36.     bool isFalling;
    37.     bool TakeFallDamage;
    38.     bool haveTakenFallDamage;
    39.     //checks when doublejump has happened - sets damage back to 0 and returns false again when falling
    40.     [SerializeField]
    41.     bool fallDamageReset;
    42.     public bool doubleJEnabled;
    43.     public bool buttonCheck;
    44.     //public checks (must be accessible)
    45.  
    46.     public bool alive;
    47.     //whether or not they are slipper
    48.     public PhysicsMaterial2D walls;
    49.  
    50.     //damage list for falling
    51.     private Dictionary<int, int> fallDamageMap = new Dictionary<int, int>();
    52.  
    53.     private void Awake()
    54.     {
    55.         InitializeFallDamageMap();
    56.     }
    57.  
    58.     void Start()
    59.     {
    60.  
    61.         rb = GetComponent<Rigidbody2D>();
    62.         //pAnimator = GetComponent<Animator>();
    63.         facingRight = true;
    64.         isFalling = false;
    65.         alive = true;
    66.         TakeFallDamage = false;
    67.         fallDamageReset = false;
    68.         currentJump = maxJump;
    69.         doubleJEnabled = false;
    70.         health = 100;
    71.     }
    72.  
    73.     private void InitializeFallDamageMap()
    74.     {
    75.         fallDamageMap.Add(75, 20);
    76.         fallDamageMap.Add(90, 35);
    77.         fallDamageMap.Add(100, 60);
    78.         fallDamageMap.Add(110, 85);
    79.         fallDamageMap.Add(120, 110);
    80.         fallDamageMap.Add(130, 140);
    81.         fallDamageMap.Add(140, 170);
    82.         fallDamageMap.Add(150, 200);
    83.         fallDamageMap.Add(160, 240);
    84.         fallDamageMap.Add(170, 280);
    85.  
    86.     }
    87.  
    88.     private void FixedUpdate()
    89.     {
    90.  
    91.         if (canWallJump)
    92.         {
    93.             walls.friction = 1;
    94.         }
    95.         else if (!canWallJump)
    96.         {
    97.             walls.friction = 0;
    98.         }
    99.  
    100.        
    101.     }
    102.  
    103.     private void Update()
    104.     {
    105.         //if alive, all these functions are valid
    106.  
    107.         if (alive)
    108.         {
    109.             AttackInput();
    110.             PMove();
    111.             //JumpInput();
    112.             Jump();
    113.             WallJump();
    114.             Flip();
    115.             Attacks();
    116.             Falling();
    117.             ResetValues();
    118.         }
    119.  
    120.         if (!alive)
    121.         {
    122.             print("you died");
    123.         }
    124.  
    125.         if (health <= 0)
    126.         {
    127.             health = 0;
    128.             alive = false;
    129.         }
    130.     }
    131.  
    132.     void PMove()
    133.     {
    134.         float h = Input.GetAxis("Horizontal");
    135.         PHandleMovement(h);
    136.     }
    137.  
    138.     void PHandleMovement (float horizontal)
    139.     {
    140.         var DeltaPosition = moveSpeed * Time.deltaTime;
    141.         rb.velocity = new Vector2(horizontal * DeltaPosition, rb.velocity.y);
    142.        
    143.     }
    144.  
    145.     void Jump() {
    146.  
    147.         var DeltaJump = jumpForce * Time.fixedDeltaTime;
    148.  
    149.  
    150.         if (Input.GetKeyDown(KeyCode.Space) && currentJump > 0)
    151.         {
    152.             //rb.velocity = Vector2.up * DeltaJump;
    153.             rb.AddForce(transform.up * DeltaJump, ForceMode2D.Impulse);
    154.             currentJump--;
    155.             fallDamageReset = true;
    156.         }
    157.  
    158.         if (doubleJEnabled)
    159.         {
    160.             maxJump = 2;
    161.         }
    162.  
    163.         if (grounded)
    164.         {
    165.             fallDamageReset = false;
    166.         }
    167.  
    168.         if (rb.velocity.y < 0)
    169.         {
    170.             rb.velocity += Vector2.up * Physics2D.gravity.y * (fallMultiplier - 1) * Time.deltaTime;
    171.             fallDamageReset = false;
    172.         }
    173.        
    174.     }
    175.  
    176.  
    177.     void WallJump ()
    178.     {
    179.    
    180.  
    181.         if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.D))
    182.         {
    183.             buttonCheck = true;
    184.         } else
    185.         {
    186.             buttonCheck = false;
    187.         }
    188.  
    189.  
    190.         if (onWall && buttonCheck)
    191.         {
    192.             wallAttached = true;
    193.  
    194.         }
    195.         else
    196.         {
    197.             wallAttached = false;
    198.  
    199.         }
    200.  
    201.  
    202.     }
    203.  
    204.  
    205.     void Flip()
    206.     {
    207.         Vector3 tScale = transform.localScale;
    208.  
    209.  
    210.         if (facingRight)
    211.         {
    212.             tScale.x = 1;
    213.             transform.localScale = tScale;
    214.         }
    215.  
    216.         if (!facingRight)
    217.         {
    218.             tScale.x = -1;
    219.             transform.localScale = tScale;
    220.         }
    221.     }
    222.  
    223.     void Attacks ()
    224.     {
    225.         //if attack and our animation is no longer attack, then we can attack again
    226.         if (attack /*&& !this.pAnimator.GetCurrentAnimatorStateInfo(0).IsTag("attack)*/)
    227.         {
    228.             //pAnimator.SetTrigger ("attack");
    229.             //if player running, and we attack as we are running, set speed to 1/8
    230.             //thus moving ever so slightly forward as we attack.
    231.          
    232.         }
    233.     }
    234.  
    235.     void Falling ()
    236.     {
    237.         if (rb.velocity.y < -0.1)
    238.         {
    239.             isFalling = true;
    240.         }
    241.         else
    242.         {
    243.             isFalling = false;
    244.         }
    245.  
    246.         //fallstart measure our fall count
    247.         if (isFalling)
    248.         {
    249.             fallStart++;
    250.             rb.gravityScale = 0.3f;
    251.         }
    252.         //fallstart resets if no longer falling
    253.         else if (!isFalling)
    254.         {
    255.             fallStart = 0;
    256.             rb.gravityScale = 1.1f;
    257.         }
    258.  
    259.         foreach (int k in fallDamageMap.Keys)
    260.         {
    261.             if (fallStart >= k)
    262.             {
    263.                 fallDamage = fallDamageMap[k];
    264.             }
    265.         }
    266.  
    267.         if (fallDamage > 0)
    268.         {
    269.             TakeFallDamage = true;
    270.  
    271.             if (TakeFallDamage && grounded)
    272.             {
    273.                 health -= fallDamage;
    274.                 TakeFallDamage = false;
    275.                 haveTakenFallDamage = true;
    276.  
    277.                 if (haveTakenFallDamage)
    278.                 {
    279.                     haveTakenFallDamage = false;
    280.                     fallDamage = 0;
    281.                 }
    282.             } else if (wallAttached || fallDamageReset)
    283.             {
    284.                 fallDamage = 0;
    285.                 haveTakenFallDamage = false;
    286.              
    287.             }
    288.  
    289.          
    290.            
    291.         }
    292.  
    293.     }
    294.  
    295.  
    296.     void AttackInput ()
    297.     {
    298.         if (Input.GetKeyDown(KeyCode.E))
    299.         {
    300.             attack = true;
    301.         }
    302.     }
    303.  
    304.  
    305.  
    306.     void ResetValues()
    307.     {
    308.         attack = false;
    309.     }
    310.  
    311.    
    312.     private void OnCollisionEnter2D(Collision2D collision)
    313.     {
    314.         if (collision.gameObject.CompareTag("Ground"))
    315.         {
    316.             grounded = true;
    317.             currentJump = maxJump;
    318.         }
    319.  
    320.         if (collision.gameObject.CompareTag("Walls"))
    321.         {
    322.             onWall = true;
    323.             //wallAttached = true;
    324.             currentJump = maxJump;
    325.            
    326.             if (wallAttached && Input.GetKeyDown(KeyCode.Space))
    327.             {
    328.                 currentJump--;
    329.             }
    330.             else
    331.             {
    332.                 currentJump = maxJump;
    333.             }
    334.          
    335.         }
    336.     }
    337.  
    338.     private void OnCollisionExit2D(Collision2D collision)
    339.     {
    340.         if (collision.gameObject.CompareTag("Ground"))
    341.         {
    342.             grounded = false;
    343.         }
    344.  
    345.         if (collision.gameObject.CompareTag("Walls"))
    346.         {
    347.             //wallAttached = false;
    348.             onWall = false;
    349.         }
    350.     }
    351. }

    A second problems I've been facing is that when I make doubleJEnabled true (double jump enabled) during mid-play, say as an upgrade option later in development, my currentJump still only = 1 UNTIL I jump once and hit the ground, then it becomes 2. Does anyone know why this happens?

    Would greatly appreciate any input or help that you guys can give!!

    Thanks.
     
  2. mgear

    mgear

    Joined:
    Aug 3, 2010
    Posts:
    9,448
    not sure, but try adding some debug.logs into those collisionenter and exists,
    could be you are colliding already when you jump, if that then changes the currentJump values.