Search Unity

Invisible Walls in 2D Platformer

Discussion in '2D' started by Nimeode, Nov 26, 2022.

  1. Nimeode

    Nimeode

    Joined:
    May 21, 2018
    Posts:
    10
    I am working on a 2D platformer and wanted to put a couple of invisible walls on a scene. All I did was add an empty game object and a box collider 2D. It stops the player, HOWEVER the player will "stick" to the wall as long as the horizontal button is pressed. I would like the character to fall at the speed of gravity. I do not know if this is setting that I would need to set up on a component or not.

    I tried to set up something in my script to fix this but that did not work. What I had was:

    Code (CSharp):
    1.     public Transform groundCheckPoint;
    2.     public Transform wallGrabPoint;
    3.     private bool canGrab, isGrabbing;
    4.     private float gravityStore;
    5.     //  Layer Masks
    6.     public LayerMask whatIsGround;
    7.     public LayerMask whatIsWall;
    8.  
    9. ...
    10.  
    11.     private void Start()
    12.     {
    13.         rigidBody2d = transform.GetComponent<Rigidbody2D>();
    14.         gravityStore = rigidBody2d.gravityScale;
    15.     }
    16.  
    17. void Update
    18. {
    19. ...
    20.             canGrab = Physics2D.OverlapCircle(wallGrabPoint.position, .2f, whatIsWall);
    21. ...
    22.         if (!canGrab)
    23.         {
    24.             rigidBody2d.gravityScale = gravityStore;
    25.         }
    26. }
    27.  
    The layers for the invisible walls were not set to 'whatIsWall' on the player character. The thought was if the user cannot grab, then the gravity that is applied would be that of the gravity scale at the start.

    upload_2022-11-25_20-29-54.png

    upload_2022-11-25_20-31-1.png
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,745
    I don't think you need any scripting... try just making a super-slippery PhysicMaterial2D and put it on the inviso brick's collider.

    Zoop, down you go.
     
  3. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,499
    You don't say how you're moving the Rigidbody2D which is the most important part. Adding forces? Setting velocity directly?

    Note that you're changing the Rigidbody2D in the Update (per-frame) callback. Physics doesn't run per-frame unless you've asked it to so you might get many Update callbacks before the simulation runs. Lots of tutorials discuss this though.

    Note that you don't use another component to get a component on the GameObject:
    Code (CSharp):
    1. rigidBody2d = transform.GetComponent<Rigidbody2D>();
    You can simply do this because your script is a component, just like the Transform:
    Code (CSharp):
    1. rigidBody2d = GetComponent<Rigidbody2D>();
    To prove this, even this crazy code works:
    Code (CSharp):
    1. rigidBody2d = GetComponent<Rigidbody2D>().GetComponent<Rigidbody2D>().GetComponent<Rigidbody2D>();