1

I'm making a 2D platformer with player mechanics that depend on the type of tile the Player lands on. I have two types of tiles white and black - if Player lands on a white tile, he has ability A, and when he lands on a black tile, he has ability B. The white and black tiles have their own collision layer each. I'm doing an "OnCollisionEnter2D" which then gives Player the abilities. You can activate or deactivate (make them visible or not) the white and black tile objects. Things go smooth when you land on a white or a black tile, but there are certain places where both tile objects overlap. In the hierarchy black is underneath white and when I run the game I first get collision with black and then I get collision with white (which overrides the black). This is my original intention as I want the white tiles to override the black tiles, but every time Player deactivates and then reactivates the white tile object, the collion order changes - I first get collision with white and then collision with black tile object, so even if the white tile object is on top, Player gets abilities for black tiles. I need to keep both tile objects active. My question is how is the order of collisions decided in Unity and do I have any control over it? I need to be able to tell my game which collision to prefer if there are "simultaneous" (I know they are in fact consecutive) collisions with two overlapping objects.

I tried changing the order of the collision object within the collision layer to no avail. Tried changing the hierarchy of the objects in the Inspector - again to no avail.

3
  • I highly recommend making your overrides in the code rather than making it depend on the order of events. You can make a list of all platform types your character is touching. And set your skill or any other logic based on the list, rather than "current" platform. Commented Jul 10, 2024 at 16:27
  • Thanks, Morion, I was afraid you'd say that... sigh, I was looking for an easy solution, because I didn't want to restructure the collision logic module... but you are absolutely right. Thanks :) Commented Jul 11, 2024 at 6:38
  • Such things are very bad to rely on. With some of the internal things (not physics, but still), I even got situations when the order was different on different platforms. So, moving such prioritization to the code level is much more stable and predictable. Commented Jul 11, 2024 at 12:38

1 Answer 1

0

When they overlap they trigger each others onCollisionEnter2D functions before player hit any of it, right? Then you can use this approach:


public class CollisionManager : MonoBehaviour
{
    enum TileType
    {
        Black, White
    }
    [SerializeField] TileType tileType;

    //Don't forget to make this variable "false" after overlapped white tile leave or destroy or disable or idk anything like that
    bool IsThereWhiteTileOverlappedAndThisGameObjectIsABlackTile = false;

    private void OnCollisionEnter2D(Collision2D collision)
    {
        IsThereWhiteTileOverlappedAndThisGameObjectIsABlackTile = collision.gameObject.tag == "White Tile" && tileType == TileType.Black;

        if (collision.gameObject.tag == "Player")
        {
            switch (tileType)
            {
                case TileType.White:
                    // Player collided with white tile
                    // Do what you want here
                    break;
                case TileType.Black:
                    if (IsThereWhiteTileOverlappedAndThisGameObjectIsABlackTile)
                        return;
                    // Player collided with black tile and there is no overlapped object
                    // Do what you want here
                    break;
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.