Search Unity

Rigid bodies behaving unexpectedly

Discussion in '2D' started by prattfall, Oct 9, 2015.

  1. prattfall

    prattfall

    Joined:
    Mar 12, 2014
    Posts:
    9
    I am still learning the basics and I've obviously misunderstood something about the way the physics works. In order to test the way my main sprite moves, I set up a temporary box to contain it, made of four rectangular sprites. (This is top-down 2d.) I stuck on a rigid body and a box collider on each, set the gravity scales to 0 and bouncy box material to the colliders. The mass on all objects is 1. The first thing was the box came apart. My sprite doesn't move unless I add force to it, so why are the box sides moving? There is no force acting on any of them. (Or, if there is, why isn't it also acting on the sprite?) And, incidentally, the same thing happens if I set gravity scale to 1 on each of them - e.g. the top side flies upwards, regardless of the gravity setting. The same thing still happens if friction is set to 1. (I've also tried changing the physic material for the ground - made no difference.) I solved this pro tem by removing the rigid body component on each of the box sides, but clearly I'm not understanding something very basic about the physics. Second, my sprite (which has the same settings on its rigid body and colliders) sails right through the box. (This happens whether I have the rigid body on the box or not.) Haven't yet found a solution for the sprite passing through the box, but I'm really looking for explanations for what is happening rather than ad hoc work-arounds. I know these are basic questions.Thanks for your patience!

    Update: my sprite is now unaccountably moving also (which is at least consistent, but still confusing. And in my main scene, I have no apparent problems with it). I have removed all script from the sprite and from everything else in the scene. Everything with a rigid body and a collider, so long as the "is trigger" checkbox is not checked, slides away in a direction away from the origin.
     
    Last edited: Oct 9, 2015
  2. prattfall

    prattfall

    Joined:
    Mar 12, 2014
    Posts:
    9
    Surely someone must know the answer to this very basic question? I don't think it's a glitch. I can't very well continue working with physics forces I plainly don't understand.

    Completely fresh top-down 2d scene: four rectangular sprites arranged to form the sides of a box, each with a rigid body and collider, gravity scale set to 0. No script anywhere. Gravity even set to zero under the general physics 2d prefs. The moment you hit play, the boxes partially collapse. What force is making them do this? Or what am I not getting please?


    Update: from the original scene - picture, if you will, one solitary sprite with no script attached gliding nonsensically across the plane. It turns out I had a collider on the image serving as a background. Removing this stops the sprite. Fine! But why? Why would a collider attached to a background sprite make the foreground sprite move away from the origin?

    Still doesn't explain the shifting sprites on the blank scene however - no collider on that background. Does no one have any insight on this?
     
    Last edited: Oct 10, 2015
  3. luniac

    luniac

    Joined:
    Jan 12, 2011
    Posts:
    614
    background and foreground is for visuals. The physics engine doesn't know the difference, in your 2D project physics settings you can set "ignore layer" settings. If you set you background sprites into one layer and foreground sprites into another layer and uncheck the 2 layers from each other in the "ignore layers" settings, then there won't be any interaction.

    Also maybe you're getting "partial collapse" due to overlapping colliders when you created the sides of a box. When u start the game the physics engine resolves overlaps by pushing the colliders apart. Try snapping the sprite colliders to each other using the "V" key and dragging the sprite, this way they'll be exactly right next to each other but not overlapping.


    Also your descriptions are VERY VERY vague and hard to understand, clear pictures would help. Also look at more tutorials, such as the individual 2D physics tutorials and some example projects, those resources really help a lot.
     
  4. tedthebug

    tedthebug

    Joined:
    May 6, 2015
    Posts:
    2,570
    If the colliders are overlapping a force will be applied to them to seperate them. Also, why are you making your Sprite from 4 separate sprites? If this is what you want create an empty object & child them to it. Put the collider onto the parent so it surrounds them, or use a square with a rigid body, collider etc, child the sprites to it & turn off the mesh renderer in the square.
    As mentioned above, it's really hard to understand what you are wanting. Check out the tower bridge tutorial as they used a player made up of a couple of sprites, it should help with the collider issues etc you are having.
     
  5. prattfall

    prattfall

    Joined:
    Mar 12, 2014
    Posts:
    9
    Thank you. Both of your comments have helped understand this. The origin thing is still a bit of a mystery, but I suppose it is some sort of interaction of colliders. Apologies for vagueness - I actually tried to be as precise as I could! I will persevere and maybe update with a capture if I get no further. Thanks again.
     
    tedthebug likes this.