Search Unity

[Help] 2D sidescroller character gets stuck midair instead of falling down if it hits a wall

Discussion in '2D' started by unity_HNkGxaHuaeblYw, Aug 2, 2019.

  1. unity_HNkGxaHuaeblYw

    unity_HNkGxaHuaeblYw

    Joined:
    Jul 24, 2019
    Posts:
    2
    Hello there, I'm somewhat new to unity, am working on my first indie metroidvania alike game.
    I'm trying to make wall grip and wall jump skills of my game, but with my movement script every time the character jump and hit a wall mid air, it get stucks while the player keeps entering the horizontal axis, I've tried a lot of stuf but nothing seemed to work.

    this is my Player Control class.

    Code (CSharp):
    1. [code=CSharp]using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class PlayerControl : MonoBehaviour
    6. {
    7.     public float maxSpd;
    8.     public float jumpSpd;
    9.     public float glideMass;
    10.     public bool doubleJump, glide, wallJump, wallGrip;
    11.     public Transform groundChecker;
    12.     public Transform wallCheckR, wallCheckL;
    13.  
    14.     private float spd;
    15.     private Rigidbody2D rb2d;
    16.     private bool facingRight = true;
    17.     private bool grounded, onWallR, onWallL;
    18.  
    19.  
    20.  
    21.     private float y, oldY, h;
    22.  
    23.  
    24.     void Start ()
    25.     {
    26.         rb2d = GetComponent<Rigidbody2D> ();
    27.         spd = maxSpd;
    28.     }
    29.  
    30.     private void Update ()
    31.     {
    32.         //Setting Grounded Status
    33.         grounded = Physics2D.Linecast (transform.position, groundChecker.position, 1 << LayerMask.NameToLayer ("Ground"));
    34.         onWallR = Physics.Linecast (transform.position, wallCheckR.position, 1 << LayerMask.NameToLayer ("Wall"));
    35.         onWallL = Physics.Linecast (transform.position, wallCheckL.position, 1 << LayerMask.NameToLayer ("Wall"));
    36.  
    37.         if (grounded)
    38.         {
    39.             doubleJump = true;
    40.         }
    41.      
    42.  
    43.         oldY = y;
    44.         y = GetComponent<Transform> ().position.y;
    45.  
    46.         Jump ();
    47.     }
    48.  
    49.     private void FixedUpdate ()
    50.     {
    51.         //Horizontal Move
    52.         h = Input.GetAxisRaw("Horizontal");
    53.  
    54.         if((!onWallR || !onWallL))
    55.         {
    56.             rb2d.velocity = new Vector2 (h * spd, rb2d.velocity.y);
    57.  
    58.             //Flipping Sprite
    59.             if (h > 0 && !facingRight)
    60.                 FliSprite ();
    61.             else if (h < 0 && facingRight)
    62.                 FliSprite ();
    63.         }
    64.         else if (onWallR || onWallL)
    65.         {
    66.             if (!wallGrip)
    67.             {
    68.                 rb2d.velocity = Vector2.zero;              
    69.             }
    70.             else
    71.             {
    72.                 rb2d.velocity = new Vector2 (h * spd, rb2d.velocity.y);
    73.                 if (!facingRight)
    74.                     FliSprite ();
    75.                 else if (facingRight)
    76.                     FliSprite ();
    77.             }
    78.         }
    79.  
    80.          
    81.     }
    82.  
    83.     void Jump ()
    84.     {
    85.         //Jump
    86.         if (Input.GetButtonDown ("Jump") && grounded)
    87.         {
    88.             rb2d.velocity = Vector2.zero;
    89.             rb2d.AddForce (Vector2.up * jumpSpd);
    90.             rb2d.gravityScale = 1;
    91.         } //Set Gravity to normal
    92.         else if (Input.GetButtonUp ("Jump"))
    93.         {
    94.             rb2d.gravityScale = 1;
    95.         } //Double Jump
    96.         else if (Input.GetButtonDown ("Jump") && doubleJump && !grounded)
    97.         {
    98.             doubleJump = false;
    99.             rb2d.velocity = Vector2.zero;
    100.             rb2d.AddForce (Vector2.up * jumpSpd);
    101.         } // Glide
    102.         else if (Input.GetButton ("Jump"))
    103.         {
    104.             if (glide && grounded == false && doubleJump == false && oldY > y)
    105.             {
    106.                 rb2d.gravityScale = glideMass;
    107.             }
    108.         } // Wall jump
    109.         else if (Input.GetButtonDown ("Jump") && (onWallR || onWallL) && wallJump)
    110.         {
    111.             rb2d.AddForce (Vector2.up * jumpSpd);
    112.             if (onWallR)
    113.             {
    114.                 rb2d.AddForce (Vector2.up * jumpSpd);
    115.                 rb2d.velocity = new Vector2 (-1 * spd, rb2d.velocity.y);
    116.             }
    117.             else if (onWallL)
    118.             {
    119.                 rb2d.AddForce (Vector2.up * jumpSpd);
    120.                 rb2d.velocity = new Vector2 (1 * spd, rb2d.velocity.y);
    121.             }
    122.         }
    123.     }
    124.  
    125.     void FliSprite ()
    126.     {
    127.         facingRight = !facingRight;
    128.  
    129.         Vector3 scale = transform.localScale;
    130.         scale.x *= -1;
    131.         transform.localScale = scale;
    132.     }
    [/code]
     
  2. unity_HNkGxaHuaeblYw

    unity_HNkGxaHuaeblYw

    Joined:
    Jul 24, 2019
    Posts:
    2
    Upddate: I managed to make the player to fall instead of getting stuck by changing the Collider material with less Friction, but now my problem is that I can't make it hold to the wall anymore.