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. Dismiss Notice

Question How can I trigger an animated tile?

Discussion in '2D' started by Draugrlord, Jul 10, 2023.

  1. Draugrlord

    Draugrlord

    Joined:
    Oct 1, 2021
    Posts:
    22
    Been looking through different sites all with varying degrees of success of finding a rather "straight-forward" answer to this question. As the topic asks, I am wanting to trigger an animated tile in the game I am working on. It is a 2D-RPG and I have some tiles set up to act as traps as the player goes through the level and if they step on the tile, or near it, the trap will trigger. I include near because one tile is a set of wall spikes that will trigger and shoot out when the player steps in front of them and the other trap is a floor trap that shoots up flame jets when the player steps on the tile that has the trap. I have the animated tiles created and they animate with no issues.

    I am only in the beginning steps of setting up the game so I don't have any code written yet which I'm guessing is how one would go about setting up the triggers for the traps, but again I've looked at different sites that all seem to either ask this question in a slightly different fashion or the answers all vary in complexity from simply using a 2d collider on the tile to using a tilemap 2d collider. I'm going to be experimenting soon to see what works but if I could get a head start on at least the direction I should be going in that would be fantastic. :)
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I would make a trap as a separate sprite animation and layer it on top of your tilemap.

    There are also ways to just extend a BaseTile so I suppose you could make the animation in there.

    As far as hooking it up, I suppose either a proximity-detection script or else just use a Collider2D set as trigger.
     
  3. Draugrlord

    Draugrlord

    Joined:
    Oct 1, 2021
    Posts:
    22
    Yeah, I had the same idea to just set to the trap portion as a separate sprite, but I wasn't sure if I could just use the animated sprite. If not it's fine I can just manually place them over the tilemap, no biggie there, just thought I had found a way to lessen my workload and make unity do some of the work for me lol:D.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    I'm kinda glad you asked the question because I haven't looked into ways of animating a particular tile enough to know how it might go into a good workflow if you wanted to stay in the tilemap for all of it.

    Without having a test tilemap project all set up, the first thing that comes to mind would be perhaps a special type of tile that registers itself with a central animator script that looks to see if they are un-fired, firing, or sprung, and that script could be used to connect to whatever RPG variables like disarm rolls, etc.

    Alternately perhaps something where you define an area in the tilemap and specify the sequence to play out all at once in all X tiles, such that you could animate traps bigger than a single tile / cell...
     
  5. karderos

    karderos

    Joined:
    Mar 28, 2023
    Posts:
    376
    there is a type of tile called animation tile

    as its name states it loops an animation on a tile

    when you want the spikes to trigger you can just set that tile and it will play

    when the animation finishes you can set another static tile over it