Hello Guest

Author Topic: 2D Detect layer or tag in a tilemap  (Read 8083 times)

NullReference

  • Newbie
  • *
  • Posts: 5
    • View Profile
2D Detect layer or tag in a tilemap
« on: April 26, 2014, 10:50:22 am »
hi,

I am working on a pokemon clone, just to gain some expirience...
So I am using the tilemap from 2d toolkit.
I want to check if a player can move before he moves.

I have set it up like this:
1 main layer in tilemap. No collision.
1 border layer. with a unity layer named collision.
default physics is set on 2D.

in my code is cast a ray to see if the ray hits layer collision like this:

Code: [Select]
public LayerMask GroundLayer; //ground layer is set to collision

else if (Input.GetKey(KeyCode.DownArrow) && isMoving == false)
        {
            bool disableMove = false;

            if (Physics2D.Raycast(transform.position, -Vector2.up, 1, GroundLayer))
            {
                disableMove = true;
            }
            else
            {
                Debug.Log("nothing");
            }
            if (!disableMove)
            {
                increment = 0;
                isMoving = true;
                startPoint = transform.position;
                endPoint = new Vector2(transform.position.x, transform.position.y - 1);
            }
            disableMove = false;
        }

so I think the problem is that there are no colliders on my chuncks.
even tho I selected the littlebox with "colliders" when making the layer.
When I put a box collider around the chunk it will just create a giant square. And im locked in there.

I hope it's clear. if not ask me anything.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2D Detect layer or tag in a tilemap
« Reply #1 on: April 26, 2014, 07:47:02 pm »
You will either need box colliders around the tiles (which tk2d optimises into edge colliders), or use something else altogether, eg. tk2dTileMap.GetTileIdAtPosition to work out what tile is at a particular position.

If you have box colliders with 2d physics, you should see the outline of the collides when you select the render data object.

NullReference

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: 2D Detect layer or tag in a tilemap
« Reply #2 on: April 26, 2014, 08:08:48 pm »
Could you give me a small sample of checking if the tile next to the player is tagged with something or on a certain layer.
Since i don't know how to check it with the player. I might be able to check the tile next to the tile. But how do i check the tile that my player is standing on.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2D Detect layer or tag in a tilemap
« Reply #3 on: April 27, 2014, 06:48:57 pm »
Get the players position, then
tilemap.GetTileIdAtPosition(playerPosition, 0);
That will return the tile id at the positon. If there is no tile, it will return -1.

NullReference

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: 2D Detect layer or tag in a tilemap
« Reply #4 on: April 27, 2014, 09:18:00 pm »
Hmmm it seems to work half... it always returns 1. if i move it still returns 1. when i change layer in int 1. it will give me -1's so i have set that up right. so no matter where i go it gives me tile id 1.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2D Detect layer or tag in a tilemap
« Reply #5 on: April 28, 2014, 10:59:29 am »
Are you using a perspective camera? If you can't get it to work still - create a simple repro case and post it here. There are so many ways you could have set this up and it would take ages to work it out otherwise. This definitely does work, as the editor uses the same code to work out where you've clicked.

NullReference

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: 2D Detect layer or tag in a tilemap
« Reply #6 on: April 29, 2014, 01:35:57 pm »
Allright so i made a clean project.
What i did:
-I imported tk2d
-Added spritesheet
-added tilemap
-Used spritesheet from earlier for tilemap.
-Created tilemapdata and editor data
-painted some tiles
-added another image for the player
-dropped it on the map
-at left bottom
- added a script to player with the following code
Code: [Select]
using UnityEngine;
using System.Collections;

public class Player : MonoBehaviour {

// Use this for initialization
void Start () {

}

public tk2dTileMap x;
// Update is called once per frame
void Update () {
Debug.Log (x.GetTileIdAtPosition (transform.position, 0));
}
}

I ran the program and it gave me 0's then i placed the player right top but still 0's
Am i missing something?
not using perspective btw game is top down.

NullReference

  • Newbie
  • *
  • Posts: 5
    • View Profile
Re: 2D Detect layer or tag in a tilemap
« Reply #7 on: April 29, 2014, 01:50:09 pm »
Sorry i have got it all wrong... its much more simple than i tought.

It returns the id of a tile not the tile location id.
So how would I know what tile is up,left,down,right before I go there?
« Last Edit: April 29, 2014, 01:53:24 pm by NullReference »

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: 2D Detect layer or tag in a tilemap
« Reply #8 on: April 29, 2014, 11:01:59 pm »
You can get the location by using GetTileAtPosition, then get GetTile at (x-1, y) to get the cell to the left, etc.
http://2dtoolkit.com/docs/latest/html/classtk2d_tile_map.html