Issue with monsters collision when scale x -1

I’m having a weird issue, and I noticed it in Glitch Garden as well – where objects will go one unit too far before registering collisions. When moving right (x = 1) everything works as it should, but when moving left (x=-1) it goes one tile too far. Screenshots below. Here’s the code:

public class EnemyMovement : MonoBehaviour
{
    [SerializeField] float moveSpeed = 3f;

    [Header("Collision Detection")]
    [SerializeField] Rigidbody2D myRigidbody;
    [Tooltip("Collider for dealing with player collisions")]
    [SerializeField] Collider2D myPlayerCollider;
    [Tooltip("Collider for finding platform end")]
    [SerializeField] Collider2D myEdgeCollider;

    Vector2 originalPos;

    void Start()
    {
        originalPos = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        ProcessMove();
    }

    private void OnTriggerExit2D(Collider2D collision)
    { 
        transform.localScale = new Vector2(-(Mathf.Sign(myRigidbody.velocity.x)), 1f);
    }

    private void ProcessMove()
    {
        if (IsFacingRight())
        {
            myRigidbody.velocity = new Vector2(moveSpeed, 0f);
        }
        else
        {
            myRigidbody.velocity = new Vector2(-moveSpeed, 0f);
        }

        transform.position = myRigidbody.position;
    }

    private bool IsFacingRight()
    {
        return transform.localScale.x > 0;
    }
}

Hi SpoonyPanda,

Maybe you could increase some values slightly in the Physics 2D settings. Maybe Position Iterations and Velocity Iterations.

Hmm, I tried changing each value from 8->10 and 3->5 but no difference.

Here is an actual gif so you can see it taking place (this is after the phys changes)

scale neg x issue

I’m wondering if this might be an issue with the tiles or with the tilemap collider. Your gif clearly shows that the enemy collides with the wall on the right side as supposed. The issue is on the left side only.

First of all, disable and enable the tilemap collider. That will make Unity recalculate the edge collider. If you see any gaps or single box-shaped edges even though there is no single tile, that usually indicates that there is a problem with the tilemap collider.

Secondly, disable all “layer” game objects (background, foreground, and whatever you have). Enable them separately so you can see whether the tile into which the enemy moves was painted on the wrong layer.

It looks like there was some weird overlap or gap in the composite – but just on the edge tiles. I ended up changing extrusion factor to 1 and it fixed the issue. Thanks for helping me resolve this! It was throwing me off, because my player wasn’t having the same issue – but also it was kinetic.

It looks like the collider is super buggy, maybe it’s just for bigger levels, but now whenever I add tiles to the foreground I have to Adjust extrusion back and forth then everything snaps into place.

I found out what the issue is – Rick has us change the sprite ppu to 31 from 32 – when he does that, it ruins the ability for the collider to properly calculate the shape. If you change the sprites back to 32, do the calculations, then change the sprites to 31 again, it calculates properly. The overlap from the edge pieces causes bugginess with processing collisions, so the slime wouldn’t process it left a collider until after it passed through the block to a solid wall completely.

This might be something for the team to look into to address the course when upgrading to newer, or put in a note about it.

Thanks a lot for sharing what you figured out. To be honest, you are the first student who brought this specific issue up. May I ask which version of Unity you are using? Perhaps Rick’s solution does not work well in newer versions anymore.

Yeah that’s my guess too. I’m on 2021.1 with the latest version of the 2d toolset from github.

Even with the tiles at 32ppu the collider gets a little funky when placing tiles but at least I can adjust the setting and it snaps it back.

My solution is to build out everything and before building to publish I adjust the tile sizes.

I think that’s a good solution for now. If necessary, you could improve your game at a later juncture when a new Unity version got released.

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

Privacy & Terms