Search Unity

Question Stop Sticking To walls

Discussion in 'Physics' started by speedlight1221, Dec 18, 2023.

  1. speedlight1221

    speedlight1221

    Joined:
    Jul 4, 2022
    Posts:
    3
    Hey guys, i am making a platformer, but my player character keeps either jumping off of walls or sticking to them. No tuttorial i found helped. Right now i stick to walls if i just keep pressing the movement.

    My colliders:
    upload_2023-12-18_19-44-29.png
    upload_2023-12-18_19-45-8.png

    My movement Code:
    Code (CSharp):
    1. void Update()
    2.     {
    3.      
    4.         inputValue = Input.GetAxisRaw("Horizontal");
    5.  
    6.  
    7.  
    8.         if (Input.GetKeyDown(KeyCode.Space) && feetCollider.IsTouchingLayers(GroundMask))
    9.         {
    10.             rb.AddForce(new Vector2(0, jumpValue), ForceMode2D.Impulse);
    11.             isGrounded = false;
    12.         }
    13.  
    14.         //animation
    15.         if (Input.GetMouseButtonDown(0))
    16.         {
    17.             animator.SetBool("isAttacking", true);
    18.             isAttacking = true;
    19.         }
    20.  
    21.         if (Input.GetMouseButtonUp(0))
    22.         {
    23.             animator.SetBool("isAttacking", false);
    24.             isAttacking = false;
    25.         }
    26.  
    27.         if (Input.GetKeyDown(KeyCode.LeftShift))
    28.         {
    29.             isRun = true;
    30.         }
    31.  
    32.         if (Input.GetKeyUp(KeyCode.LeftShift))
    33.         {
    34.             isRun = false;
    35.         }
    36.  
    37.     }
    38.  
    39.  
    40.  
    41.  
    42.     private void FixedUpdate()
    43.     {
    44.      
    45.         transform.position = new Vector2(transform.position.x + (speed * inputValue * Time.deltaTime), transform.position.y);
    46.         Flip();
    47.         Debug.Log(isGrounded);
    48.     }
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,490
    You don't move a Rigidbody2D by modifying the Transform. You use its API as shown in all the tutorials on the subject. Whilst you've set zero friction on one collider, clearly the other one doesn't You can set the material on the Rigidbody2D so that its colliders use it if they're not setting their own material.

    If those boxes are a tilemap, I would suggest adding a CompositeCollider2D and setting its mode to Outline too; assuming you're not making lots of tilemap changes during the runtime.