- Home /
How do I stop characters from standing on top of each other
I have been making a 2D multiplayer game, within which my players handle collisions using a box collider. But with this, comes the problem of them being able to stand on top of each other. I still want my characters to be able to push one-another horizontally with these box colliders, but don’t want them to stand on top of each other. Would anyone happen to have a solution for this?
Answer by peacemakers1980 · Apr 19, 2021 at 07:21 PM
When I did 2D games in plain C/C++ with SDL, I usually had a array that held all objects, like all the players data or npcs or everything, if Player A wants to move left, it first runs threw the array, and checks if anything is already there, if so dont move, if its open then move. like public struct charlocs { int x,y; }
public charlocs[] MyCharsLocs = new charlocks[1024]; ...... .......
when Player wants to move something like
for(int x = 0; x < 1024; x++)
{
if(myNewX == MyCharsLocs[x].x) return; // dont move something already there
}