Search Unity

Question inconsistant wall jump

Discussion in '2D' started by CC_1759, Apr 1, 2021.

  1. CC_1759

    CC_1759

    Joined:
    Aug 23, 2019
    Posts:
    4
    i am trying to make a wall jump, but when on the wall it does not always jump, only sometimes. please help
    it is inconsistant on both sides





    Code (CSharp):
    1. public class WallJump : MonoBehaviour
    2. {
    3.  
    4.     [SerializeField] private LayerMask groundLayerMask;
    5.     public BoxCollider2D boxCollider2D;
    6.     public Rigidbody2D rb;
    7.     public GameObject playerController;
    8.  
    9.     // for checking if wall jumped so you wont wall slide right after
    10.     bool wallJumpedFromRight = false;
    11.     bool wallJumpedFromLeft = false;
    12.  
    13.     //i right and i left
    14.     int iR = 0;
    15.     int iL = 0;
    16.  
    17.  
    18.     // Update is called once per frame
    19.     void FixedUpdate()
    20.     {
    21.         if (CheckIfOnWallToRight() == true)
    22.         {
    23.             // make you jump of the wall
    24.             if ((Input.GetButtonDown("Jump")) && (Input.GetButton("d")))
    25.             {
    26.                 playerController.GetComponent<PlayerMovement>().enabled = false;
    27.                 rb.velocity = new Vector2(-5, 10);
    28.                 wallJumpedFromRight = true;
    29.             }
    30.             // makes you wall slide
    31.             else if ((Input.GetKey("d")) && (wallJumpedFromRight == false))
    32.             {
    33.                 rb.velocity = new Vector2(0, -1);
    34.                 Debug.Log(wallJumpedFromRight);
    35.             }
    36.         }
    37.  
    38.         if (wallJumpedFromRight == true)
    39.         {
    40.             iR += 1;
    41.         }
    42.         if (iR >= 25)
    43.         {
    44.             iR = 0;
    45.             wallJumpedFromRight = false;
    46.             playerController.GetComponent<PlayerMovement>().enabled = true;
    47.         }
    48.  
    49.  
    50.  
    51.         else if (CheckIfOnWallToLeft() == true)
    52.         {
    53.             if (Input.GetButtonDown("Jump"))
    54.             {
    55.                 Debug.Log("wd");
    56.             }
    57.             // make you jump of the wall
    58.             if ((Input.GetButtonDown("Jump")) && (Input.GetButton("a")))
    59.             {
    60.                 playerController.GetComponent<PlayerMovement>().enabled = false;
    61.                 rb.velocity = new Vector2(5, 10);
    62.                 wallJumpedFromLeft = true;
    63.                 Debug.Log("wall jump l");
    64.             }
    65.             // makes you wall slide
    66.             else if (Input.GetKey("a") && (wallJumpedFromLeft == false))
    67.             {
    68.                 rb.velocity = new Vector2(0, -1);
    69.                 Debug.Log(wallJumpedFromLeft);
    70.             }
    71.         }
    72.         // to give the player back control
    73.         if (wallJumpedFromLeft == true)
    74.         {
    75.             iL += 1;
    76.         }
    77.         if (iL >= 25)
    78.         {
    79.             iL = 0;
    80.             wallJumpedFromLeft = false;
    81.             playerController.GetComponent<PlayerMovement>().enabled = true;
    82.         }
    83.  
    84.     }


    here is the raycast for the checking of being next to a wall. just incase, but it is says it is green the whole time so i doubt it



    Code (CSharp):
    1. public bool CheckIfOnWallToRight()
    2.     {
    3.         // send a ray to tell if you are touching/near a wall
    4.         RaycastHit2D onWallRight = Physics2D.Raycast(new Vector3(boxCollider2D.bounds.max.x + .1f, boxCollider2D.bounds.max.y, 0), Vector2.down, .55f, groundLayerMask);
    5.  
    6.         // sets ray color depending on if you are near a wall or not
    7.         Color rayColor;
    8.         if (onWallRight != true)
    9.         {
    10.             rayColor = Color.red;
    11.         }
    12.         else
    13.         {
    14.             rayColor = Color.green;
    15.         }
    16.  
    17.         // shows the ray... as close as i can, why cant they just make it so there is a thing to show the ray? that          //would make it much easier.
    18.  
    19.         Debug.DrawLine(new Vector3(boxCollider2D.bounds.max.x, boxCollider2D.bounds.max.y - .02f, 0), new Vector3(boxCollider2D.bounds.max.x, boxCollider2D.bounds.min.y + .02f, 0), rayColor);
    20.  
    21.         return onWallRight;
    22.     }
    23.  
    24.  
    25.     // check to the left
    26.     public bool CheckIfOnWallToLeft()
    27.     {
    28.         // send a ray to tell if you are touching/near a wall
    29.         RaycastHit2D onWallLeft = Physics2D.Raycast(new Vector3(boxCollider2D.bounds.min.x + -.1f, boxCollider2D.bounds.max.y, 0), Vector2.down, .55f, groundLayerMask);
    30.    
    31.         // sets ray color depending on if you are near a wall or not
    32.         Color rayColor;
    33.         if (onWallLeft != true)
    34.         {
    35.             rayColor = Color.red;
    36.         }
    37.         else
    38.         {
    39.             rayColor = Color.green;
    40.         }
    41.  
    42.         // shows the ray... as close as i can, why cant they just make it so there is a thing to show the ray? that          //would make it much easier.
    43.  
    44.         Debug.DrawLine(new Vector3(boxCollider2D.bounds.min.x, boxCollider2D.bounds.max.y - .02f, 0), new Vector3(boxCollider2D.bounds.min.x, boxCollider2D.bounds.min.y + .02f, 0), rayColor);
    45.  
    46.         return onWallLeft;
    47.     }
    48. }
    49.