- Home /
How to find what type of tile player is on in 2D game
I am creating a 2D roguelike game but seem to be struggling to determine what tile the player is currently standing on. I tried to use a 3D raycast as 2D only does up, down, left and right whereas I need to check what tile is below the player. I am trying to avoid adding box colliders to every object as I'm fairly certain it would influence performance in larger levels. I have attached tags to all objects which is the differential I would like to use to determine the tile the player is currently standing on.
Thanks
In order to use a raycast you'll have to have a collider on your tile, 2D raycasts can do a point if you set the direction to Vector2.Zero.
Answer by TheSOULDev · Aug 27, 2017 at 10:33 PM
How are your tiles created? What are they? Because you didn't specify how YOU differentiate the tiles, my approach in this situation would be to build a list of custom classes containing the tile type and its coordinates. Then, I can create a method that searches the coordinate spectrum and returns a tile type.
But that is fairly inefficient. Honestly I would just create a 2-dimensional array and fill it with the tiles as they're instantiated, find the width and height per tile, and then divide the player coordinates by those numbers, truncate the number and get the position of a tile in that array. By reading it and the array being a custom class with tile type and possibly other properties, I can find out what tile it is. Later on, you can actually serialize that array to be your map file.
Your answer
Follow this Question
Related Questions
SphereCast Arguments 1 Answer
My raycast wont function properly 0 Answers
Debug.DrawLine problem 1 Answer
Having a RaycastHit event create a component? 1 Answer
Problem With RayCast 3 Answers