0
\$\begingroup\$

My sprite has a pivot in the bottom left so that it snaps on the grid. Flipping the sprite with flipX almost works, but the sprite also shifts one quare to the left/right when flipping.

Gif of behaviour:

enter image description here As you can see, the sprite flips, but shifts in an odd way due to the pivot point.

This is solved by moving the pivot to bottom center, but then the sprite has some weird collision with the tilemap collider.

Gif of behaviour:

enter image description here

Tilemap collider image:

enter image description here

As you can see, the character gets "stuck" or "caught" on the squares.

Here is the code:

Rigidbody2D rb;
Animator ani;
SpriteRenderer sr;


public float speed;
public float stationaryThreshold;

private float direction = 0f;
private float squaredStationaryThreshold;

private bool isGrounded = false;

void Awake()
{
    squaredStationaryThreshold = stationaryThreshold * stationaryThreshold;
}

void Start()
{
    rb = GetComponent<Rigidbody2D>();
    ani = GetComponent<Animator>();
    sr = GetComponent<SpriteRenderer>();
}

void Update()
{
    direction = Input.GetAxisRaw("Horizontal");

    if (direction > 0f)
    {
        rb.velocity = new Vector2(direction * speed, rb.velocity.y);
        if (rb.velocity.sqrMagnitude > squaredStationaryThreshold)
        {
            sr.flipX = true;
            ani.SetTrigger("Walk");
        }            
    }
    else if (direction < 0f)
    {
        rb.velocity = new Vector2(direction * speed, rb.velocity.y);
        if (rb.velocity.sqrMagnitude > squaredStationaryThreshold)
        {
            sr.flipX = false;
            ani.SetTrigger("Walk");
        }                        
    }
    else
    {
        rb.velocity = new Vector2(0, rb.velocity.y);
        ani.SetTrigger("Idle");
    }
\$\endgroup\$
2
  • 1
    \$\begingroup\$ Have you considered placing the pivot in the middle of the sprite, then nesting the sprite inside a parent whose pivot is in the corner as you originally desired? Then you can flip the sprite about its own pivot without changing its position relative to the parent's pivot, which you use for snapping. \$\endgroup\$
    – DMGregory
    Aug 3 at 17:10
  • 1
    \$\begingroup\$ I think getting caught on the tiles is a completely separate issue from your sprite flipping issue. \$\endgroup\$
    – TenFour04
    Aug 4 at 1:36

2 Answers 2

2
\$\begingroup\$

Try adding a Composite Collider 2D component on your Tilemap gameobject. Set Used By Composite checkbox to true in your "Tilemap Collider 2D" component. Composite Collider 2D also comes with a Rigidbody 2D, make sure to set the Rigidbody 2D's Body Type to either Static or Kinematic.

The Composite Collider 2D will combine each adjacent tile's collider into one combined collider, so you won't have to deal with individual collider's edges.

\$\endgroup\$
1
\$\begingroup\$

Instead, flip your sprite, change the Transform.localScale from (1,1,1) to (-1,1,1) and viceversa

I was to use sprite.flip but then I realized changing the scale is more useful and straightforward because you can flip all the other parts/sub-parts of the Game Object (equipment, UI elements, collision, particles).

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .