- Home /
How to detect if there is a floor in front of a character?
I have some simple jelly characters moving on a defined game area, like a game board with squares. Jelly steps are fixed to snap on the grid one square at a time, and they turn 90 at time, so I'm not using free movement or physics collisions.
Obviusly the floor is limited, and I need the character to move only if there is the floor in front of it.
I already have a collider attached to every jelly, but it's used to detect other jellys when they overlap in the same position. If I attach another collider in front of a jelly, all collisions with other players are messed up.
So, how can I detect if there is a floor in front of the jelly?
Answer by Bampf · Sep 10, 2010 at 02:32 PM
If the game layout is being arranged in the Unity scene editor, then AliAzin's solution of raycasting is probably the best method. It also handles with moving floors, obstacles, and irregularly shaped floors. In fact, you can also use it to detect other jellies before actually going and bumping into them.
But I just thought I'd mention an alternative that sometimes makes sense. You could instead keep a map in memory of where the squares are. This works for levels generated from a file of some kind, or from some kind of formula, rather than levels arranged in the Unity scene editor, because that gives you the map. (If the level is built in the scene editor it is still possible to build a map at runtime, e.g. by casting an NxN array of rays into the scene.)
Thanks Bampf, your method looks less CPU expensive but actually I don't know well how to create arrays and this kinds of data structures... :) Luckyly I need to check with the Raycast every precise time step and not every frame, so it will do. ;)