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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Create borders in 2D using BoxColliders2D and RigidBody2D

Discussion in '2D' started by pesek23, Feb 21, 2016.

  1. pesek23

    pesek23

    Joined:
    Feb 3, 2016
    Posts:
    5
    Hello there.

    In my 2D tetris like game i want to make borders where tiles (game pieces) can't move. Tiles are moved by mouse.
    On tile i added Box Collider 2D and Rigid Body 2D, same components i added to border.

    But when i drag a tile it doesn't collide with border, it goes through.
    When i add gravity to the tile - so after game is start tiles start falling down - tiles start colliding with borders. But when it all fall down to bottom border and i drag a tile it again doesn't collide with border.

    On a tile i have a script to move tile when it's dragged. So when i drag the tile to border position tile move there even there are colliders.

    Is there any chance how to create borders with the way i tried? Or is there any good practice how to create borders using 2D physics?

    Thank you.
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    how are you moving the tiles ? If something with transform. , then this moving method can ignore physic 2d.
     
  3. pesek23

    pesek23

    Joined:
    Feb 3, 2016
    Posts:
    5
    Tile is moved by mouse (input.mousePosition). So if mouse is over border then tile move there too. But i thought the colliders won't allow move of tile over the border. But it makes sence...

    So is there any good practice how to create borders with using of unity components without scripting?

    Now when i drag the tile i must check if every side (border) of tile is not in the position of the border. It seems to be uneffective a quite difficult.

    Thank you.
     
  4. geek_freek

    geek_freek

    Joined:
    Aug 26, 2014
    Posts:
    51
    I would suggest you to create your borders with EdgeCollider2D.
     
    antsam likes this.
  5. pesek23

    pesek23

    Joined:
    Feb 3, 2016
    Posts:
    5
    My current solution:
    a) border has a box collider
    b) tile has a box collider and a rigid body
    c) tile is moved by intput.mouse, but instead of using transform.position i use Rigidbody2D.MovePosition

    It works well for me but i'm not sure if it's a good solution.

    If there is a better way to solve it reply please.