Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

I use colliders and rigidbodies for my player and a wall but my player can move through the wall

Discussion in '2D' started by LOLZER123, May 3, 2020.

  1. LOLZER123

    LOLZER123

    Joined:
    Mar 21, 2020
    Posts:
    5
    I have a basic top-down movement script running on my player and nothing else except for a sprite renderer, Rigidbody 2d set to kinematic and box collider 2d. On my wall, I have a box collider 2d and a Rigidbody set to kinematic. Yet, my player travels through my box. (Sprite goes over the box) Any ideas why?
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    The point of Kinematic bodies is that they don't respond to external forces such as gravity or forces you apply but also this means they don't have a collision response so you should expect the above.

    If you want a collision response then use a Dynamic body. For stuff that doesn't move you should use a Static body (Rigidbody2D set to Static BodyType or don't add a Rigidbody2D which sets it as implicitly Static).

    Don't go setting the Transform on a GameObject with physics components on it. When you add a Rigidbody2D you're asking for it to control the Transform; use its API to move either with its velocity, adding forces, using MovePosition/MoveRotatione etc.

    Note: There is a Physics forum for this stuff which might help. There's also a 2D physics basics tutorial here but there are lots online.
     
    LOLZER123 likes this.
  3. LOLZER123

    LOLZER123

    Joined:
    Mar 21, 2020
    Posts:
    5
    I get what you mean and I have tried dynamic rigidboy2D and set gravity scale to 0 but it still doesn't work :(. If I do so, they move with my player when I push against them.
     
  4. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    You should provide some information on what you're doing and your set-up otherwise it's impossible to tell you what you're doing wrong.
     
  5. LOLZER123

    LOLZER123

    Joined:
    Mar 21, 2020
    Posts:
    5
  6. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Those images show you making a wall as dynamic (movable) and the player as Kinematic which isn't what I suggested above.

    I would suggest you take a look at the physics tutorial I posted to get you started.