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

Farming mechanic

Discussion in '2D' started by RKSrep, Mar 28, 2020.

  1. RKSrep

    RKSrep

    Joined:
    Jul 9, 2019
    Posts:
    1
    My question is based around my own personal 2D project. So in my game I wish to implement a way to till the land, plant, grow and harvest plants. My question comes with how would I make the normal ground(grass tile) change to tilled land tile. My theory was that I would make layers to the ground and whenever the player used the hoe it would then delete the tile on the layer. Is this the way to do this or is there a better way of implementing this idea. I have looked through the 2D Farming kit idea and the problem is that does not showcase what I am looking for.
     
  2. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    10,623
    Why not simple set the tile to a different tile asset dynamically as you "farm" it? A little bit like the way textures on a block in Minecraft change as you hit them.
     
  3. eses

    eses

    Joined:
    Feb 26, 2013
    Posts:
    2,637
    @RKSrep

    You could do what @MelvMay said - just set the tiles to new ones where something happens. It is visual change only, but it might be enough, as you can always check what tile you have at specific map cell. It would work for a Stardew Valley like game. You can also store additional data to some dictionary / list, if you need to store for example how much your plants / crops have grown.

    https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.SetTile.html
    https://docs.unity3d.com/ScriptReference/Tilemaps.Tilemap.SetTiles.html

    Code (CSharp):
    1. if (Input.GetMouseButtonDown(0))
    2. {
    3.     // works with ortho camera
    4.     var wpos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
    5.  
    6.     // get tile pos
    7.     var tilePos = tilemap.WorldToCell(wpos);
    8.  
    9.     // Set new tile to location
    10.     tilemap.SetTile(tilePos, myNewTile);
    11. }
     
  4. sikozonbatata

    sikozonbatata

    Joined:
    May 28, 2020
    Posts:
    1
  5. PuppyPolice

    PuppyPolice

    Joined:
    Oct 27, 2017
    Posts:
    116
    What I recommend would be a secondary tilemap that sits on top of the ground tilemap, that way you do not need to have a perfect square tile sprite for tiling and the second reason if you need to check if the ground is tiled you just check if the tilemap has tile