Search Unity

Question Bobbing water like in Forager game

Discussion in '2D' started by Dark_Seth, Jun 22, 2021.

  1. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    140
    Hi. All. Quick questions. Does anybody have any tips or know how to animate/create water like in forager?

    Thanks
     
  2. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    You could have two tiles: (1) a normal tile for most of the water, (2) an animated tile for those tiles where the water needs to bob up and down. The animated tile would just have a series of sprites that show water bobbing up and down.

    See this video for a tutorial:

    Note: the video is a little bit out of date: in later Unity versions, Tilemap Extras are a package available through Package Manager.

    You could also try one of these alternative strategies discussed over here:
    https://forum.unity.com/threads/runtime-access-to-unity-2d-tiles.1130446/#post-7264165
    But the sprite animation is probably the simplest.
     
  3. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    its just a sprite animation frame by frame drawn by hand
     
  4. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    140
    @rarac @Lo-renzo

    Thank you both. I actually went that way before viewing this.

    Mine looks like this. But all the water goes up and down the same time. Not like a rolling wave over the screen. Wonder how to approach this.



    Should do rolling wave from one sidenof the screen.

     
  5. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    They might apply an offset (using x position) to change the sprite index so when it animates it does so a bit differently as you go left to right.
     
  6. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    140
    @Lo-renzo

    So making some changes to the Unity Tilemap Extras. Also If this gets resolved I am willing to share the code here!

    Normal



    CheckBox ( Random Start )That per Tile checks frame length and picks a starting frame.

    Amazing. ( This I will use for Fires and Flags! )





    Now to get the wave effect ( Wave CheckBox )

    My Idea is to do the following Let say the the follwoing
    Frame Count = 4 frames per animation. ( 0 to 3 in Code)
    X position of all tiles example above

    Each Flag on X axis
    -2,-1,0,1,2,3

    So now I have to go. Lets say always use x=0 as starting frame 0 ?
    -2,-1,0,1,2,3
    F2,F3,F0,F1,F2,F3

    How the hell do I calculate that???

    Pseudocode

    totalframes = rule.m_AnimatedSprites.Length; ( example 4 ( 0 to 3 ) )
    locationvar = location.x ; ( example 2 )

    starttileindex = ??????? ( should be 2)

    Example 2

    totalframes = rule.m_AnimatedSprites.Length; ( example 4 ( 0 to 3 ) )
    locationvar = location.x ; ( example 13 )

    starttileindex = ??????? ( should be 1)

    -2,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
    F2,F3,F0,F1,F2,F3,F0,F1,F2,F3,F0,F1,F2,F3,F0,F1,F2,F3
     
  7. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    the ppl above already gave the solution, basically make a different tile where the animation starts at frame 2 instead of frame 1, etc
     
  8. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    140
    Please read my last post.

    Using diff tiles is slow. Not really good development to do it that way.

    I use rule tile ( 1 Tile with 6 frames in my screenshot) as you can See I can randomly choose a starting frame.
    The Wave tile I need to calculate ?? Yeah
    Each tile I will check the x to start on a diff frame. Thing is how to calculate position.x = 13? A new tile is created on x=8? Which frame to choose then

    Rule Tile with Random Frame Start. See not doing a wave....

     
    Last edited: Jun 24, 2021
  9. rarac

    rarac

    Joined:
    Feb 14, 2021
    Posts:
    570
    if you have 4 frames in the tile you should make 6 tiles to make it easier (t5 is duplicate of t3 and t6 is duplicate of t2)

    then this is the position

    Code (CSharp):
    1.  
    2. X =  -2    -1   0     1    2    3    4           5        6
    3.      t3    t2   t1   t2   t3   t4   t3(t5)    t2(6)       t1
    4.  

    to get the tile number is the formula
    Code (CSharp):
    1. 1 + (x % 6)
     
  10. Dark_Seth

    Dark_Seth

    Joined:
    May 28, 2014
    Posts:
    140
    Got it working. Grade School maths. Feel silly now

    is C# just had to make own Formula

    int Mod(int a, int n) => (a % n + n) % n;


    Tilemap Rule Tile Working as Follow

    Waves!!!! Beautiful waves. Will Post my Water here later.

     
    rarac likes this.
  11. Lo-renzo

    Lo-renzo

    Joined:
    Apr 8, 2018
    Posts:
    1,514
    Looking good. :D