Not understanding how the colliders are working

My game is working as per the lecture. However something I noticed on the lecture was initially Rick hadn’t ticked ‘is trigger’ on the Goobers box collider. This meant that the trigger code didn’t work. However the Goober was colliding with and moving the player - presumably because of either its box collider or its capsule collider. However despite having a hard collision with the player it was still able to move through Rick’s wall. The wall forms part of the platform tilemap and I thought we had also added a tilemap and composite collider there (hence the wall stops the player). My game is behaving in the exact same way. In my game when I set untick the ‘is trigger’ box on the Goober I get the same ‘walk through walls’ effect as Rick. Why is the wall stopping the player but not the enemy?

Thanks,

K

Hi kmamor,

Without knowing basically nothing about your project, it is impossible to tell why the enemy is able to walk into the wall.

The enemy needs two colliders: One ‘solid’ collider for its body, and one trigger collider, which acts as a periscope. The ‘solid’ collider, which is a non-trigger collider, is supposed to prevent the enemy from being able to walk into other ‘solid’ colliders.

If the enemy crosses the edge collider in your game, pause the game and click the third button (not the Play button, not the Pause button). This will allow you to flip through the single frames. If you keep the scene window open and zoom in the ‘broken’ wall, you could observe the enemy’s behaviour. Maybe the enemy flips in one frame but flips in the next frame again. If that’s the case, your periscope collider is very likely too close to the body collider.

Did this help? :slight_smile:


See also:

No frame by frame flipping. I’ve watched the lecture again. A few points:
a) At 4.09 Rick is explaining the periscope. He acknowledges that there is collision with the ground (so the Goober doesn’t pass through it) and collision with the wall - which the Goober does pass through! Indeed the code assumes it does hence uses OnCollisionExit and not OnColisionEnter. This is my point - how can Rick’s (and my) Goober be stopped by the ground but not the wall? Both are part of the same element (the platform tilemap which has the ground layer ) using the same collider (tilemap + composite).
b) At 09.39 you see exactly the same effect I would like to understand - Rick’s Goober (with ‘is trigger’ unticked) is held up by the ground (as expected) but walks through the wall, even though the ground and the wall (I thought) are the same element - both are part of the same platform tilemap. I do not understand why the Goober is treated one way by one part of the platform tilemap (a horizontal bit we would normally refer to as ground) and a vertical bit (wall).

K

There is no definition for wall and ground. There is just one edge: the Tilemap collider. The periscope collider is supposed to touch the ground (horizontal edge) the entire time until it reaches a wall (vertical edge). When the enemy reaches a wall, there is no horizontal edge inside the wall, and the periscope collider exists that edge. In that moment, a ‘collision/trigger exit’ event gets triggered, and our OnTriggerExit2D method gets called. In that method, we flip the enemy. If the colliders are set up incorrectly, the enemy flips twice and walks through the wall.

If you want to implement a different logic in your game, you’ll have to define in your code what ‘wall’ and what ‘ground’ is. Then you will be able to use if-conditions to check for ‘wall’ and ‘ground’.

But the double flipping will only occur if the is trigger box is ticked - that is what calls the method. My enemy only walks through the wall when there is no flipping possible (is trigger is ‘unticked’ so the flip method cannot be called) .

Whilst trying to solve this I now have a player that falls though the ground even though I haven’t removed or otherwise altered its colliders. The version seems to have been saved - though I deliberately didn’t save whilst I was checking different elements. How do I stop the player falling through the platform - the platform’s tilemap collider is active.

K

The periscope collider must have ‘Is Trigger’ enabled. Otherwise, Rick’s algorithm will not work.

If the player falls through the ground, check the tilemap collider and the composite collider. The ground must not be a trigger collider.

I know that the trigger needs to be enabled - but I’m asking why , before Rick realized he hadn’t enabled the trigger, the enemy is able to pass through the platform - see 09.39 in the lecture for what I mean.

BTW - I know why the enemy doesn’t fall downwards through the ground - it is because it’s rigidbody is set to kinematic and so not subject to gravity and we do not give it any y-velocity.

If ‘Is Kinematic’ is enabled, the collider will not be affected by any collisions. This means that the enemy is able to walk into other colliders.

If Rick flips the enemy in the OnTrigger*2D method, he must enable ‘Is Trigger’ in the Rigidbody2D component. Otherwise, that method will not get called.

Ah! That’s it. Thanks,

K

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

Privacy & Terms