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. Dismiss Notice

Rigidbody2D and BoxCollider2D Problem

Discussion in '2D' started by kashif789us, Mar 8, 2015.

  1. kashif789us

    kashif789us

    Joined:
    May 2, 2013
    Posts:
    163
    Hello friends, I am developing a 2D game in which 2D vehicle having a boxCollider2D runs on a flat surface having BoxCollider2D.

    Vehicle has Rigidbody2D attached to it while ground surface has only the BoxCollider2D.
    Vehicle moves through rigidbody2D.velocity.

    rigidbody2D sleep mode is set to never sleep.
    interpolate set to interpolate
    and collision detection is set to continuous.

    The problem I am facing is that sometimes, the vehicle stops to move as if its stuck somewhere, If I pick it up a bit and release, it starts to move again. Whats the problem? How to fix this? Thank you
     
  2. superstyle

    superstyle

    Joined:
    Mar 5, 2015
    Posts:
    14
    If your ground is made from more than one piece you maybe didn't set them right. If it's just one piece try adding it Rigidbody and set it to "Is Kinematic".
     
  3. sleekdigital

    sleekdigital

    Joined:
    May 31, 2012
    Posts:
    133
    Yeah it sounds like you might be getting hung up on a corner or something. Since its a vehicle, you might also try attaching wheels. Each wheel would be a rigidbody 2D with Circle Collider.
     
  4. kashif789us

    kashif789us

    Joined:
    May 2, 2013
    Posts:
    163
    Thank you for your reply. I have 3 objects for ground which are rearranged programmatically to make it an infinite path.

    I have added rigidbody and set to kinematic to these ground objects but still the same problem.
     
  5. kashif789us

    kashif789us

    Joined:
    May 2, 2013
    Posts:
    163
    Thank you for the suggestion but I am trying to keep it simple thats why using only one box collider 2D. I dont need accurate vehicle physics thats why I am using only one collider.
     
  6. superstyle

    superstyle

    Joined:
    Mar 5, 2015
    Posts:
    14
    You need to make an offset for your tiles that appear. Make public int offsetX = 2; also while in play mode, click scene view and click on each of your 3 main tiles and take a look at their transform. Maybe something just changes when you hit play.

    And here is how I used it and it worked just fine:

    {
    // Calculate the camera's extend (half the width) of what the camera can see in world coordinates
    float camHorizontalExtend = cam.orthographicSize * Screen.width/Screen.height;

    // Calculate the X position where the camera can see the edge of the sprite
    float edgeVisiblePositionRight = (myTransform.position.x + spriteWidth/2) - camHorizontalExtend;
    float edgeVisiblePositionLeft = (myTransform.position.x - spriteWidth/2) + camHorizontalExtend;

    // Checking if we can see the edge of the element and then calling MakeNewBuddy if we can
    if(cam.transform.position.x >= edgeVisiblePositionRight - offsetX && hasARightBuddy == false)
    {
    MakeNewBuddy (1);
    hasARightBuddy = true;
    }
    else if(cam.transform.position.x <= edgeVisiblePositionLeft + offsetX && hasALeftBuddy == false)
    {
    MakeNewBuddy (-1);
    hasALeftBuddy = true;
    }
     
  7. kashif789us

    kashif789us

    Joined:
    May 2, 2013
    Posts:
    163
    I changed the ground collider from boxCollider2D to EdgeCollider2D and now its working fine :)