Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Find player position on a tile map

Discussion in '2D' started by KevG, Feb 20, 2016.

  1. KevG

    KevG

    Joined:
    Oct 4, 2015
    Posts:
    20
    Hi

    I am new to coding I have create some code that ' clones ' a single prefab tile with 2d box colliders in a small 10 x 10 tilemap. ( working, yeah. :D:D , I know I'm reinventing the wheel, but I am just learning. :confused::confused:)

    If I place a player onto the tilemap how can I find out what tile the player is on ???

    i.e. If player if placed on a tile that is on row 3 column 4 and the player moves down ( row , column 4 ) how can I find out what tile the player has moved onto.

    The reason I ask is that I wish to make some tiles that could be destroyed which makes the player only able to move in a certain direction.

    Thanks

    Kev
     
  2. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    If your box collider on the tile is set to isTrigger then you should get a "notice" when the player moves to a new tile (OnTriggerEnter2D). In fact, you'd even get a notice when the player left the previous tile (OnTriggerExit2D).

    Have you tried that? (Maybe I'm misunderstanding what you want to do.)

    Oh, maybe you mean how do you know the player is now on row 4, col 4? Simple -- add that information to the tile when you instantiate it. I do that in some grid code I played with earlier this evening.

    My tile prefab has a script attached with this line at the top:

    Code (CSharp):
    1.     public int2 gridPos;
    (That int2 is a cool way to have an integer-based Vector2. You can find it here: https://gist.github.com/JoachimHolmer/5743d0ad1c09b64cb548 )

    When I create the grid I do this each time through the loop (looping over variables named row and col):

    Code (CSharp):
    1.     int2 gridPos = new int2(row,col);
    2.     cellObj.GetComponent<GridCell>().gridPos = gridPos;
    Now when I trigger the collider with a player, or tapping/clicking one of the cells, every cell know its own position in the grid.

    I hope that helps.

    Jay
     
  3. KevG

    KevG

    Joined:
    Oct 4, 2015
    Posts:
    20
    Thanks for quick reply.

    I take it your int2 gridpos stores the actual position in the grid the tile is set at,
    I.e. The position say for tile 1 set at 0,0 then the script for int2 would hold 0,0 I think that's what's what happening.

    I haven't done anything with the colliders yet as I have only just got the tilemap script working, I did say I'm new

    Is this trigger event something that happens automatically or do I have to set something to turn this on ??

    I know I'll have to write code to be able to read the trigger notice, but if the trigger happens automatically then it should halve the time to write some code.
     
  4. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    For collisions (including triggers) to work both objects have to have a collider component and at least one of them has to have a rigidbody. Satisfy those conditions and yes, the trigger stuff happens automatically. You could add this in a script attached to the tile prefab and it should work:

    Code (CSharp):
    1.     void OnTriggerEnter2D(Collider2D coll)
    2.     {
    3.         Debug.Log("I been triggered by " + coll.name);
    4.     }
    5.  
    By "work" I mean that a message will be printed to the console. For game type stuff you'll have to write more actual code. ;)

    Here's the link to the API docs for OnTriggerEnter2D: http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter2D.html

    Jay

    PS - Make sure isTrigger is checked on the collider components.
     
  5. JayJennings

    JayJennings

    Joined:
    Jun 24, 2013
    Posts:
    184
    Yes, exactly. 0,0 up through however many cells there are.

    Jay
     
  6. KevG

    KevG

    Joined:
    Oct 4, 2015
    Posts:
    20
    Thanks jay

    You been a great help

    A big thumbs up to you.

    Kev
     
    JayJennings likes this.