Hello Guest

Author Topic: Triggering events when player collides with certain tiles  (Read 4961 times)

2x4b

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Triggering events when player collides with certain tiles
« 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 :)

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Triggering events when player collides with certain tiles
« Reply #1 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.

2x4b

  • 2D Toolkit
  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Triggering events when player collides with certain tiles
« Reply #2 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.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
Re: Triggering events when player collides with certain tiles
« Reply #3 on: December 04, 2014, 07:00:04 pm »
its not static, it should be -
int tileId = tilemap.GetTileIdAtPosition(testLoction,0);