- Home /
How to detect what tile player is standing on in a tile-based game
I have spent two days on this problem and still cannot figure it out. I am trying to make the character slide across oil but I am having an issue with detecting whether he is on oil or not. It is a 2D game and therefore all game elements are 0 on the z-axis so Raycasts wont work. I have also tried determining by using the player co-ordinates to determine what tile the player is on but due to the movement system, the player isn't exactly always on a tile which is an exact multiple of 1. Any help or advice will be appreciated.
Answer by RyanWelshAFRC · Aug 28, 2017 at 03:08 PM
As there is an issue with a multiple of 1 you would simply round down the players position.
For example: if the players position of x is 25.5 then you know he is on tile 25
tileNumber = MathF.FloorToInt(Player.position.x);
This could be accompanied by an integer array of all the oil tiles
int [] oilTiles= new int[] {25,26,27};
A simple check can then be carried out to decide whether the player is on an oiled tile or not
if(oilTiles.Contains(tileNumber))
{
PlayerSlide();
}
Answer by jawadahmeddogar · Aug 28, 2017 at 04:11 PM
You can add trigger on tiles with oil. Create any static variable in your desired class . Set variable to true when you enter the tile with oil and set it to false when you leave it (Use onTriggerEnter and onTriggerExit ).
Your answer
![](https://koobas.hobune.stream/wayback/20220612133724im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
3D Grid Movement 1 Answer
Getting Valid Moves On A Grid 1 Answer