AnswerBun.com

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

Convert a string to a gameobject name

1  Asked on December 1, 2021 by stormzy18

     

Jumping up quicker? (Paper Mario style jump)

1  Asked on December 1, 2021 by spottel

       

A* Pathfinding Finding One of Many Goals

1  Asked on November 29, 2021 by user6256

 

Wheel rotation for a raycast car in Unity?

1  Asked on November 29, 2021 by john-545

       

Unity “scrape away” effect

0  Asked on November 24, 2021 by darren-swords

 

Initial setting of camera and ppu to match playing table

1  Asked on November 21, 2021 by zibelas

 

LibGDX – Tilemap Wall Collision Detection Issues

2  Asked on November 20, 2021 by james-stoddern

     

Tilemaps not showing up on Android build

1  Asked on November 18, 2021 by jcorretjer

   

Unity: Sprite stretch issue

1  Asked on November 14, 2021 by tikeb

     

Unity UI, how to hide on play

2  Asked on November 12, 2021

 

Alternative path not working

1  Asked on November 12, 2021 by dsngl-tibor

     

Unity – Understanding the SRP batcher

2  Asked on November 12, 2021 by majs

 

Ask a Question

Get help from others!

© 2023 AnswerBun.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP