Search Unity

Sprites sink without physics

Discussion in 'Physics' started by Denisowator, Dec 16, 2017.

  1. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Here's my problem, I have a 2D object which I don't want to do anything with physics wise. But without the physics, the sprite sinks slightly, while the collider (set as trigger) stays in place.

    I don't want this objects to be movable, or move on its own, I just need it to be in a static position in the world. Right now this is what it looks like when before and after I hit "Play", while it has no non-trigger collider and/or rigidbody2D components.

    Before:
    upload_2017-12-16_23-0-2.png
    That is the bottom of the object (the grey) and the green around it is the trigger collider. That is how it's supposed to look.

    After:
    upload_2017-12-16_23-0-45.png
    When I hit "Play" the object's sprite sinks slightly.

    I have checked the transform component, and the actual object doesn't change position at all. Any ideas on how to fix this, or work around it? And more importantly, why it's happening in the first place?

    I was able to fix this by giving the object a non-trigger collider, as well as a rigidbody2D. But that causes the object to be affected by gravity, and interact physically with other objects and the player.

    Furthermore, this only seems to be happening on this object, and there are no scripts that control its position or anything like that. It does have an idle animation playing, but when I checked the sprite image (for that animation), the boundaries for each frame are correctly positioned and scaled.

    P.S. Don't mind the stuff around it changing between the two images, that's just parallaxing and image effects happening when the game runs.
     
  2. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Just did some testing, and found that removing the Animator component from the object fixes it. But now I need to figure out why the animator sinks the sprite. :/
     
  3. BoogieD

    BoogieD

    Joined:
    Jun 8, 2016
    Posts:
    236
    Is it because the object with collider sinks below a surface but only the collider is re-positioned leaving the sprite where it fell? Try repositioning the sprite according to the collider or set the rigidbody to kinematic so it does not fall.
    "Game objects which don’t have a RigidBody component are considered static colliders. This is important to be aware of because it’s extremely inefficient to attempt to move static colliders, as it forces the physics engine to recalculate the physical world all over again." Read the part on rigidbody in the ink: https://unity3d.com/learn/tutorials...1.1582548463.1513407358-1079206380.1493014688
     
  4. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Please read my last post, it turns out it's not the physics (I think at least).
    Besides, your answer seems to be backwards. As I stated that this happens while there is no rigidbody, and putting a rigidbody in, fixes it.
     
    Last edited: Dec 17, 2017
  5. Denisowator

    Denisowator

    Joined:
    Apr 22, 2014
    Posts:
    918
    Fixed it (at least partly). Found this reddit post, and changing the pivot of the animations to "Bottom" made them stop sinking by half pixel. But the sides float by half a pixel. I moved all the colliders (for walls) by half-pixel so they sink into the walls slightly, and now the player sprite actually makes contact with the wall sprites. It'd still be nice if anyone ever found a way to properly fix this.

    Also, like before, the rigidbody makes it float up by half-pixel, so I either have to make the bottom collider (for the ground or player) be in by half-pixel vertically, or set the pivot of the animations (for physics enabled objects) to center. Which is really annoying.
     
    Last edited: Jan 23, 2018