Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2d-extras: How do I identify a RuleTile's current sprite at runtime?

Discussion in '2D' started by solar_blitz, Nov 8, 2019.

  1. solar_blitz

    solar_blitz

    Joined:
    Jul 30, 2014
    Posts:
    19
    (using Unity 2018.2.15f1)

    To allow my character to interact with his environment (hang on ledges, wall jump, etc.), I created a script to iterate through the entire TileMap of a level and attribute the correct collision detectors with the correct sprite by its name. Here's an abbreviated look at the method calls I use to handle this

    public bool IsLedgeTile(ref TileBase _Tile)
    {
    //_Tile.name is the name of the sprite, LedgeTiles is a list of sprites that can be ledges
    for(int i = 0; i < LedgeTiles.Length; i++)
    if (_Tile.name.Equals(LedgeTiles[i].name))
    return true;
    return false;
    }


    Currently, this is done with normal Tile Palettes and it works pretty well.

    However, now I want to make map editing easier for iteration by using RuleTiles. The RuleTile itself is fine, but when that code checks _Tile.name, it returns the name of the RuleTile, not the name of the sprite the RuleTile is displaying. I want the name of the sprite the RuleTile is displaying. Is there a way to determine which sprite is currently displayed by a RuleTile, and a way to determine if a tile at position x, y, z is a RuleTile object and not a regular TileBase object?
     
  2. blu3drag0n

    blu3drag0n

    Joined:
    Nov 9, 2018
    Posts:
    94
    Do you know the position in the tilemap of the tile that you are currently checking on ?
    Then you can use
    Code (CSharp):
    1. myTilemap.GetSprite(position)
    Otherwise you can typecast TileBase into Tile.
    Code (CSharp):
    1. Debug.Log(((Tile)_Tile).sprite.name);
     
    tutyhin and solar_blitz like this.
  3. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    Well if you need to grab only a certain type of tile you can always use the
    tilemap.getTile<TypeOfTilesYouWant>(Vector3int position) and if it is null there either no type of that tile or no tile at all at that position.
     
  4. solar_blitz

    solar_blitz

    Joined:
    Jul 30, 2014
    Posts:
    19
    Hello, sorry for the delay, I had a previous task to develop.

    Holy cow, blu3drag0n! This absolutely worked! Thank you so much!
     
  5. Deleted User

    Deleted User

    Guest

    I have my characters jump on ledges, hang and so on but I don't need to create the kind of script you are using. The tilemap just need a composite collider...
     
  6. solar_blitz

    solar_blitz

    Joined:
    Jul 30, 2014
    Posts:
    19
    I use a composite collider for normal traversal, but for hanging onto ledges I only want the player to switch into the ledge-hang-state if their position is relative to the corner of a particular tile. The original method relied on manual calculations to determine the player's proximity from the ledge tile upon contact with said ledge tile. I tried that before, actually, but I didn't really do too well. So I went with 2D circle colliders instead.

    I don't use too many CircleCollider2Ds, though. I keep 16 and move them to other positions when the player-character moves between subdivisions of the map. I was just providing that as a context to my problem. Whether or not I use colliders or run manual calculations (to detect proximity to ledges) wouldn't matter if I don't know which sprite was a ledge tile.