Search Unity

[Help]Need advice on dynamically drawing a path,..

Discussion in 'Scripting' started by welby, Mar 9, 2020.

  1. welby

    welby

    Joined:
    Mar 22, 2011
    Posts:
    549
    Need some advice before I delve into what sounds like a mess.

    I want to be able for the player to do this.

    Dynamically draw a path where the sprites align and change accordingly on the fly.

    I got the drawing and erasing part down fine,.but the only way I can figure out how to align stuff feels so messy and big. I am wondering if there is a more elegant solution. I am not looking for code,..but a general explanation on how it could be done. Arrays, lists,.etc?

    It seemed easy but once you find out these things need to react to what's diagonally around them it got huge!

    Thanks in advance.

    Crazy!! this is my mind right now.
     
  2. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    You'll need to explain it in a bit more detail!
     
  3. welby

    welby

    Joined:
    Mar 22, 2011
    Posts:
    549
    Kk,sorry.

    The video link I posted is a near perfect example.

    I am making a sim game. The player can draw their own paths for the npc's to walk on.

    My stumbling block is , I can draw a path of tiles fine, but i want the sprite in it to react and change depending on the angle and tiles next to it.

    A typical rpg style path tileset would include a turn sprite, a strait path sprite, a T intersection sprite, etc..


    Like it would know to put an L band path sprite, or a 4 way path sprite, witha border.

    Or, the trickier part for me, to know if there are no borders and it's just an open dirt tile with no borders. Hope it makes sense more. No?
     
    Last edited: Mar 10, 2020
  4. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    I've never used Unity for 2D, but shouldn't you create a matrix of positions when you put these objects down

    eg:
    Code (CSharp):
    1. 0 0 0 0 1 0 0
    2. 0 2 0 0 1 0 0
    3. 0 0 0 0 1 0 0
    4. 1 1 1 1 1 1 1
    5. 0 1 0 0 0 0 0
    6. 0 1 0 0 2 0 0
    7. 0 1 1 1 1 1 1
    each 8x8 grid would contain a number referring to the type of tile, and if the player's location equalled the number position on the grid, it would act accordingly?

    I know there are tutorials for this on YouTube though. Good luck!
     
  5. welby

    welby

    Joined:
    Mar 22, 2011
    Posts:
    549
    I dont that's what I am getting at, sorry.

    Did you watch the video in my OP? I want draw a path at runtime. But the tiles need to understand what sprite they should use so the "road" makes sense.

    Here is a link to my webGL.

    http://www.wenlissgames.com//Unity/SimParkTest/index.html


    After creating and loading a new game you can go to the construction icon and see that you can draw a path of solid tiles.

    I want those tiles to eventually be a road or open clearing depending on how the player draws and fills things in.

    Like this. But without the pavement, it would be a dirt path. How would the game know which ones to use? Especially if titles are bunched to create an open area.

    Image

    https://images.app.goo.gl/bsxT9pGtEJFrRo1B6
     
  6. SparrowGS

    SparrowGS

    Joined:
    Apr 6, 2017
    Posts:
    2,536
    Like @davidnibi I don't do 2D (besides UI) and was extremely unclear on the OP, but don't you just set "terrain type" (like grass, dirt, road, whatever) and unity takes care of that with the tile maps thingy? (again - never used this)
    https://docs.unity3d.com/Manual/class-Tilemap.html

    I think I heard someone say something about it working with hex tiles..
     
  7. davidnibi

    davidnibi

    Joined:
    Dec 19, 2012
    Posts:
    426
    I looked at the video and tried the game, (I wasn't sure what I was meant to be doing :) ).
    How have you created those tiles in the first place, assuming they are all tiles?
    I've got a feeling this is really simple and can be perfomed in a function as soon as you click on a tile. You're just placing a square/hex of graphic on a screen that is added to a matrix that has a global property (icon type, etc) and singular property (transform position).
     
  8. kdgalla

    kdgalla

    Joined:
    Mar 15, 2013
    Posts:
    4,635
    I think this example in the docs might do what you want. The explanation on the page is not very good.

    https://docs.unity3d.com/Manual/Tilemap-ScriptableTiles-Example.html

    Basically, you just have to go through every combination of surrounding tiles to figure out which one to draw. For this logic, though, you will want to separate the type of tile from which tile is actually drawn. You just want to know whether adjacent tiles are part of the path or not.