2D Toolkit Forum

2D Toolkit => Support => Topic started by: 2x4b on November 25, 2014, 12:22:30 pm

Title: Triggering events when player collides with certain tiles
Post by: 2x4b on November 25, 2014, 12:22:30 pm
Hi All,

I'm just wondering how I can say, for example, that certain tiles are "ground" so I can set a boolean in the player to "grounded" when the player is colliding with at least one "ground" tile. Would each ground tile need to be a prefab to do that?

Thanks a lot :)
Title: Re: Triggering events when player collides with certain tiles
Post by: unikronsoftware on November 28, 2014, 12:34:50 pm
Dont use prefabs for this. There is a much much faster way of doing this than using physics, etc. tilemap.GetTileIdAtPosition returns the tileId at a position. If you know the position of the player feet (maybe a bit under the feet as a sensor), use tilemap.GetTileIdAtPosition, get the tileId, use GetTileInfoForTileId to get the tileinfo for that tile - you can set up tile info string or int as "ground", etc. You can find out what tile is at any point and it'll be incredibly fast.

Use the normal physics for collision response, but for sensor type things this is way faster.
Title: Re: Triggering events when player collides with certain tiles
Post by: 2x4b on December 04, 2014, 02:36:02 pm
Thanks so much for this answer. I'm one step closer to getting this all understood :)

I'm really stuck however on accessing the tilemap from script. Could you please let me know where I'm going wrong?

Code: [Select]
public tk2dTileMap tileMap; // I use unity's inspector to add the tilemap into here


// Update is called once per frame
void Update () {

Vector3 testLoction = new Vector3(0f,0f,0f);
int tileId = tk2dTileMap.GetTileIdAtPosition(testLoction,0);

}

I get this error:

Code: [Select]
An object reference is required to access non-static member `tk2dTileMap.GetTileIdAtPosition(UnityEngine.Vector3, int)'
Thanks so much.
Title: Re: Triggering events when player collides with certain tiles
Post by: unikronsoftware on December 04, 2014, 07:00:04 pm
its not static, it should be -
int tileId = tilemap.GetTileIdAtPosition(testLoction,0);