Unity get reference of GameObject under another GameObject

Game Development Asked on January 5, 2022

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

I don’t know if I am aproaching 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 aproach 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?

One Answer

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.

Answered by lvictorino on January 5, 2022

Add your own answers!

Related Questions

How do i draw a ray in unity

1  Asked on January 7, 2022 by levent


testPoint not working – libgdx

1  Asked on January 5, 2022 by ngc-qu-nguyn


C# Increase move speed for spawned objects

1  Asked on January 5, 2022 by jenkatan29


Rotate vector by matrix?

2  Asked on December 29, 2021


Finding all possible paths from one hexagon to another

3  Asked on December 25, 2021 by vitozz_rdx


Unity : apk vs aab for the same scene

0  Asked on December 23, 2021


Unreal Engine custom camera

0  Asked on December 23, 2021


(Tiled) Background stops being rendered in libGDX

1  Asked on December 21, 2021 by xmac


Ask a Question

Get help from others!

© 2022 All rights reserved. Sites we Love: PCI Database, MenuIva, UKBizDB, Menu Kuliner, Sharing RPP, SolveDir