0
\$\begingroup\$

I am trying to get reference of the object underneath my Player.

I don't know if I am approaching this correctly. Right now, I have a player simply put in the scene, like this:

Player normally in scene

Then I am trying to get reference of it like this (does not work)

public void CheckTileUnder()
{
    Vector3 halfExtents = new Vector3(0f, -1 / 2.0f, 0f);
    Collider[] colliders = Physics.OverlapBox(transform.position, halfExtents);

    foreach (Collider item in colliders)
    {
        Tile tile = item.GetComponent<Tile>();
        if (tile != null)
        {
            RaycastHit hit;

            if (Physics.Raycast(tile.transform.position, Vector3.down, out hit, 1))
            {
                currentTile = tile;
            }
        }
    }
}

Another approach that I thought of would be to put the player under the Tile like this: Then I guess I would have to change parents like suggested in one of the answers here? Player under Tile

How should I select the Tile under the Player?

\$\endgroup\$
1
  • \$\begingroup\$ How does your player character reach that tile in the first place? That might be a better place to put this determination. For instance, in chess if on my turn I say "Knight to F3" then I know from that moment until I change it that this piece is on tile F3 - I don't ever have to run a physics query against the tile or check parenting relationships, because this isn't the outcome of physics or parenting. That's just a side channel that you're using to recover information that a previous step in your game knew. So, skip the side channel and record that knowledge directly at the time you make it \$\endgroup\$
    – DMGregory
    Commented Oct 28, 2019 at 16:54

1 Answer 1

1
\$\begingroup\$

It depends on how your game works. Both of your leads are correct if you just need to know what tile is under the player. Between the two, again depending on your type of game and gameplay, I'd use the one using a Raycast.


TL;DR: if you organize your grid objects in a linear way (from first to last) you could easily find the cell your player is on with a simple calculus.

However, on a grid based game, there is something less consuming than a Raycast based system or a hierarchy swap system. You could simply use Maths :) if you know the size of your grid cells, the origin of your grid, and the world position of your player... then it'd be easy to know what tile your Player is on.

Let's say that your grid is a width 5 and height 6 grid of 2 units cells. Your player is on, let's say, x: 1.2 and y:3.5. The origin (bottom left) of your grid is on 0,0.

cell_x = player_x / cell_size = 1.2 / 2 = 0.12 = 0
cell_y = player_y / cell_size = 4.5 / 2 = 2.25 = 2 

In this example your player is on cell x 0 and y 2. So now... "I know the cell coordinate, but I still do have the reference of it". Now let's say you have a GameObject named grid that contains all the cell GameObject as child. The first one would be cell id 0, the second one would be id 1, ... and the last one would be id width * height - 1, in our case id 29 (5 * 6 - 1).

How to convert a x,y cell position into and id? That's pretty simple: id = y * width + x. In our example above, position x 0 and y 2 would give us id = y * width + x = 2 * 5 + 0 = 10.

To get your GameObject reference, you can get the cell child you want on your grid GameObject by: GameObject cell = grid.transform.GetChild(id).gameObject;. Et voila.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .