#How to handle tilemap walls?

1 messages · Page 1 of 1 (latest)

iron skiff
#

ive been using several individual gameobjects for building levels (floors + walls. etc), but recently have been looking into tilemaps, as it would be a lot more efficient.

I followed tutorials that say to put a tilemap collider, then a composite collider, but I've run into an issue where OnCollisionEnter2D does not trigger when hitting a wall while grounded. this is a problem for things like my enemy scripts that rotate upon colliding into a wall, even when using Collision2D.GetContacts to see if the collision Vector2.left or right. i can see that it is because it is all the same collider, it doesn't register this new collision.

how would I go about seperating wall collisions with floor collisions?

#

would i just use OnCollisionStay? I'm not sure if that would be better

wet thorn
#

Do you have a screenshot to have a bit of Visual around it ?

iron skiff
#

sure, let me do that

wet thorn
#

Just to be sure "While grounded" => You mean your ennemies are grounded to the floor (Not jumping basically).

iron skiff
wet thorn
#

I guess your character has Rigidoby2D ?

iron skiff
#
void OnCollisionEnter2D(Collision2D collision)
    {
        if (collision.gameObject.CompareTag("Ground"))
        {
            ContactPoint2D[] contacts = new ContactPoint2D[5];

            int cnt = collision.GetContacts(contacts);
            for (int i = 0; i < cnt; i++)
            {
                ContactPoint2D contact = contacts[i];
                if (contact.normal == Vector2.left || contact.normal == Vector2.right)
                {
                    FlipEnemy();
                    break;
                }
            }
        }
    }
#

yes

#

the enemy is dynamic, and the wall is static

#

ill try changing that first actually

#

oh, it's kinematic already

wet thorn
#

Let me think a little bit, can you try removing the Kinematic ?

iron skiff
#

on the tilemap?

wet thorn
#

I guess so.

iron skiff
#

if i switch it to static, it still does not register. and i can't remove it due to composite collider2D relying on it

wet thorn
#

Sorry, not removing it but untoggling it.

iron skiff
#

i can't toggle the rigidbody, and when i toggle the composite collider it stays the same

wet thorn
#

Dumb assumption, can you just try to log before your if the gameobject, do you at least get anything ?

iron skiff
#

i put a log on collisionenter, it works when it lands on the ground, but if it walks into a wall, nothing

wet thorn
#

Does your ground and wall are the same elements ?

iron skiff
#

yes, that's what i wanted to ask, what way to seperate walls and floors in tilemaps

#

i tried giving the enemy no gravity, so it wouldn't touch the floor, and it does behave like intended, so it's just the fact that it is on the ground, so it doesn't register new collisions

wet thorn
#

You can separate them into different tiles group, but I'm not sure that would solve the issue.

My assumption is that, Unity is thinking that you're already into a collider, so maybe not triggering the other one.
If you jump and then hit the wall, does something occur ?

iron skiff
#

yes, as long as it isn't touching the ground when it hits the wall, it works

#

but if it's touching the ground, the collision doesn't occur

#

i'm assuming because the tilemap is just one big gameobject

#

it doesn't count as a "new" collision

wet thorn
#

Ohhhh !!!

#

Yes that's exactly that

#

Sorry I tought your tilemap was small elements.