Sneaky Collider Issue :(

Hello there!

I have a small issue on the “TileVania” project ([Complete C# Unity Game Developer 2D] - Lesson N° ≃ 80/82).

My warrior is falling between two floor tiles, exactly in a stair angle, when it change direction (i.e. looking from it right side to left side).

→ See attached picture. Video here : Discord

Any code/unity tricks ?
Thanks!

Hi @Lithem,

Welcome to our community! :slight_smile:

If the character falls between tiles, the problem is almost always the tilemap collider. Sometimes, Unity does not generate it correctly. In that case, you would see rectangles and/or gaps in the scene window.

The ‘correct’ collider shape must match the overall shape of the ground.

To make the TilemapCollider2D be recalculated, disable the component, and enable it again.

Did this fix it?


See also:

Hello Nina,
Thanks for your superfast feedback!

I just tried generating a new tile map collider, but it seems that the collider shape is matching the ground shape pretty well (see white lines in the pictures attached). Unfortunately, it doesn’t help.

Have you been able to see the short video posted on discord?
I have the feeling that the issue is related to the character being a bit “off centered” when switching from looking to his right side, then to his left side (or vice versa).

Would it be possible that by being a bit “off centered”, he actually cross the collider frontier?

Related code in the course :

void Run()
    {
        Vector2 playerVelocity = new Vector2(moveInput.x * runSpeed, myRigidbody.velocity.y);
        myRigidbody.velocity = playerVelocity;

        bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;
        myAnimator.SetBool("IsRunning", playerHasHorizontalSpeed);
    }
    void FlipSprite()
    {
       bool playerHasHorizontalSpeed = Mathf.Abs(myRigidbody.velocity.x) > Mathf.Epsilon;

       if(playerHasHorizontalSpeed)
       {
            transform.localScale = new Vector2(Mathf.Sign(myRigidbody.velocity.x),1f);
       }
        
    }

I don’t have a Discord account, so I cannot watch it.

Do you mean that the character teleports if you move him left and/or right? If so, the problem might indeed be caused by the offset. Since the tilemap collider is just an edge, the character might get stuck if he accidentally moves beyond the edge. This can happen if he teleports. A little offset is sufficient to cause this.

If your character has got children, check and adjust the children.

If there are no children, click the sprite in your Assets folder to open the Texture Import Settings. Then click ‘Edit Sprite’. The pivot point of the sprite must be in the centre of the sprite. Like this:

image

The red box is supposed to represent the bounding box in the Sprite Editor. Ideally, the sprite is aligned with the bottom edge, and centred. The pivot point is where I drew the circle. The arrow points to it in case the scribble is hard to interpret. :wink:

And this is the ‘wrong’ position of the pivot point:

image

That is brilliant, you solve it Nina !!
Thank you very much for your support :slight_smile:

Thomas

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms