- Home /
Tile-Based Movement - Collision
Hi,
I am working on a game where I need my main character to have tile based movements. I think I can do this, but am still working on it at the moment.
The problem I face now, is that I want my main character not to walk into walls, or walk off the ground into water etc. I was thinking of making this happen by tagging the floor and walls, so if below the main character, there is an object tagged as "floor" and if towards the direction it's travelling there is not one tagged as "wall" only then would the character move.
Would this be the most efficient way to do it, or is there a much simpler way? Thanks!
It's probably not a good idea to have each tile a separate GameObject if you can avoid it, it may cause problems if you have large maps. The standard way of dealing with this is similar to what you suggested, though - give each tile a "type", and test that type before you move into it. It's efficient as it's just a string or integer comparison, as opposed to actual collision detection.
Yes, I also believe the approach you're taking may be the wrong way to do it. For the best and most efficient way to make a tile based game, I HIGHLY reccomend watching this youtube playlist. It covers everything you need to know in depth, and will greatly help you in the future. It may be a bit long, but for everything you'll learn it's worth it. He also explains ALL the concepts extremely well, so you will hardly need to worry about not understanding anything.
Here is the link, and if you have the time it is definitely worth your while.
Hi, Sorry, but its not that sort of randomly generated terrain that I want. I want more of for example, a maze which is different on every level, and the player needs to get to the end. $$anonymous$$aybe I'm missing something further into the videos so I'll still watch them. Thanks.
Answer by SubatomicHero · Jul 01, 2013 at 01:12 PM
If you are talking about, for example, a simple 4 x 4 grid you need to setup the grid once it has been instantiated.
You could have a class that holds properties for each tile:
public class tile
{
bool hasWater;
bool isOccupied;
// other stuff
// important bit here
public tile north;
public tile south;
public tile east;
public tile west;
}
and when you create each piece of the grid, instantiate a new object of type tile. Then you need to setup where the north, south etc tiles are.
Then your movement would check like so:
void Move() // for exmaple
{
if(!tile.north.isOccupied) // if the north tile is free
{
// you can move there
}
}
Hope this helps..
I mainly use JavaScript. Is there an equivilant of a class in JavaScript?, although I'm probably going to learn c# soon.