2D Roguelike Tutorial Collision Issues

Hello all,

I am working with the 2D Roguelike Tutorial hoping to use it as a base for my own game, but I'm running into an issue with collisions. Sometimes the player will end up in the same square as an enemy and get stuck, dying due to the enemies' attacks. This happens in the complete example game as well. Does anyone know how this might be fixed?

Sounds like Unity put a bug in for us to find!

Debugging is how these things are fixed.

The first place to look is to see what keeps the player out of enemies, and what keeps enemies out of the player's square.

When I say "see what keeps" I mean what actual mechanism is used: colliders? coordinate checks? a master grid??

Once you understand those mechanisms you will be in a good place to reason about the issue.

By debugging you can find out exactly what your program is doing so you can fix it.

First figure out what mechanism is used, then see if it is working properly by debugging it.

https://docs.unity3d.com/Manual/ManagedCodeDebugging.html

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don't assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Ugh... I just looked at it and it really is a buggy mess: I am able to go anywhere, pass through anything at all.

A quick check led me to the answer and I fixed it.

I am gonna put the answer here but I'm gonna mark it spoiler in case you wanna try your hand at it!!

Spoiler on fixing Roguelike2D collision

[spoiler]

This game was written a long time ago, like 2016 or 2017 I think. Assuming it worked back then, investigating now it appears that upon upgrading to current Unity, something happened to the BlockingLayer field in the Enemy1, Enemy2 and Player prefabs: those LayerMasks are set to Nothing, which is incorrect and causes collision checks to fail.

The fix is to select each of the above prefabs and set their BlockingLayer to be the BlockingLayer. It looks something like this:

9859665--1420212--Screenshot 2024-05-28 at 9.35.46 AM.png

Give that a whirl and things should start working again.

Personally this type of game should ALWAYS be done with all items placed into a logic grid so you can easily just check adjacent cells, rather than doing all the silly Unity-specific layer checking and 2D raycasting.

If you want an example of moving around a grid for a game like this, I have one online. Search for "MoveGrid" in my ProximityButtons project.

proximity_buttons is presently hosted at these locations:

https://bitbucket.org/kurtdekker/proximity_buttons

https://github.com/kurtdekker/proximity_buttons

https://gitlab.com/kurtdekker/proximity_buttons

https://sourceforge.net/projects/proximity-buttons/

[/spoiler]

Thanks for the resource, I'll check it out! The problem is all of these are on the blocking layer. I just double checked.

[quote=“NerdAlex”, post:4, topic: 948667]
The problem is all of these are on the blocking layer. I just double checked.
[/quote]

That’s only half of the solution… they must BE on the blocking layer but the property called BlockingLayer (look at the screenshot above) also has to be check marked with what you want it to check against.

If both of these things are true then yes, well, we have ANOTHER bug. But you will have to use your own debugging skills for that one because I have it working fine right now.

PS - when I was debugging I put a breakpoint in the Move() method contained in the MovingObject class and observed that yes, the blockingLayer was incorrect. I would begin your investigation there…

PPS - here’s more relevant reading:

The difference between Layers vs LayerMasks:

https://discussions.unity.com/t/802897/2

“There are 10 types of people in this world: those who understand binary, and those who don’t.”