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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Animated tiles

Discussion in '2D' started by Marscaleb, Oct 14, 2015.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    992
    Is there a practical way to create animated tiles?

    Let's say for example I have an overhead scene of an island. The island will be surrounded by water. The water needs to be animated, so I would create an animated water tile. I'm picking this as an example because that would require a LOT of tiles to be animated, but truthfully there are MANY situations where I would want animated tiles in my levels.

    Now to my understanding, in order to animate that water, I would need to drop in a sprite to be a tile, add an animator to the sprite, create an animation controller for the water tile, create an animation for that animation controller of the water animating which will be set as the default animation, and finally apply that animation controller to the animator I made for that tile's game object.
    Then every single water tile in the scene will need to posses an animator with that same animation controller.

    This seems like a tremendous waste. First of all I need to create a whole animation controller for an object that does nothing but loop a single animation. That right there is a pretty big waste that is cluttering up my folders with extra files. On top of that, I am creating a screen filled with many many game objects using a somewhat complex system; at least more complex than I need for something that's been done in 2D games for over 30 years.

    Is there a better way to do this that I am missing?
    I've got a lot of levels where I'm going to need animated tiles, and I'd greatly appreciate a method that is less taxing on the system and less time-consuming for development. For that matter I'd like a method to create animated sprites that only ever play a single animation that doesn't take up so much time.
     
  2. BinaryX

    BinaryX

    Joined:
    Aug 4, 2014
    Posts:
    55
    The way we went about it was to make the tiles to be static and have a water texture/color and animate only the things that we placed on top of the water (boats, waves, fish coming to the surface of the water with alpha). I don't know if it's the best method. Our game was a bit cartoonish so this approach suited us. If you want realistic water, then you can place some ground tiles and add the default water prefab on top of them.
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    Hi there,

    you mention that it's "something that's been done in 2D games for over 30 years" - so I would head to that direction and search archives as solution is already there. I've seen many tutorials about this in Youtube and game dev sites and these can be non-Unity specific.

    There is possibility to create mesh every frame or less often, that way you could chunk levels to have less game objects as many tiles are in one gameobject. I'd abandon Animator for this use maybe.
    Create code to create quads and UVs there are many examples available. Then create area of tiles that fill one chunk.
    What you could try after this, when is to animate UVs to create animation by moving UV region of tile to different "frame" in your bitmap. There are sprite animation scripts in Unity Wiki too to get started.

    I've tried to create a tilemap system once or twice but find it quite complicated for amateur programmer, I did get thousands of tiles moving on screen with quite decent pace and didn't even use this method and tiles were not animated, and structure of code was quite horrible - I'm just learning this stuff.

    I bet new 2D tilemap features of upcoming Unity version are going to helpful.
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    SpriteTile has some animation functions (and you can add your own if you want); basically it flips through a defined range of tiles, for any tiles that are visible on-screen. It's lightweight and there aren't any animation controllers etc., just fairly straightforward timing + array code.

    --Eric
     
    eses likes this.